From db0f6e8033d1f882b5b1e13a1332149c04cd2abd Mon Sep 17 00:00:00 2001 From: Amine Ben hammou Date: Thu, 24 Sep 2015 01:25:55 +0100 Subject: [PATCH] wn:controller:rest-actions command added --- .../app/Http/Controllers/RESTActions.php | 68 +++++++++++++++++++ .../ControllerRestActionsCommandCept.php | 9 +++ .../tests/acceptance/ModelCommandCept.php | 10 +-- lumen-test/tests/tmp/TestingModel.php | 2 +- src/Commands/ControllerRestActionsCommand.php | 19 ++++++ src/Commands/ModelCommand.php | 2 +- src/CommandsServiceProvider.php | 9 +++ src/Template/Template.php | 2 +- templates/controller.wnt | 10 +++ templates/controller/rest-actions.wnt | 68 +++++++++++++++++++ templates/model.wnt | 2 +- templates/route.wnt | 0 templates/routes.wnt | 8 +++ 13 files changed, 198 insertions(+), 11 deletions(-) create mode 100644 lumen-test/app/Http/Controllers/RESTActions.php create mode 100644 lumen-test/tests/acceptance/ControllerRestActionsCommandCept.php create mode 100644 src/Commands/ControllerRestActionsCommand.php create mode 100644 templates/controller/rest-actions.wnt delete mode 100644 templates/route.wnt create mode 100644 templates/routes.wnt diff --git a/lumen-test/app/Http/Controllers/RESTActions.php b/lumen-test/app/Http/Controllers/RESTActions.php new file mode 100644 index 0000000..2bf1425 --- /dev/null +++ b/lumen-test/app/Http/Controllers/RESTActions.php @@ -0,0 +1,68 @@ + 200, + 'created' => 201, + 'removed' => 204, + 'not_valid' => 400, + 'not_found' => 404, + 'conflict' => 409, + 'permissions' => 401 + ]; + + public function all() + { + $m = self::MODEL; + return $this->respond('done', $m::all()); + } + + public function get($id) + { + $m = self::MODEL; + $model = $m::find($id); + if(is_null($model)){ + return $this->respond('not_found'); + } + return $this->respond('done', $model); + } + + public function add(Request $request) + { + $m = self::MODEL; + $this->validate($request, $m::$rules); + return $this->respond('created', $m::create($request->all())); + } + + public function put(Request $request, $id) + { + $m = self::MODEL; + $this->validate($request, $m::$rules); + $model = $m::find($id); + if(is_null($model)){ + return $this->respond('not_found'); + } + $model->update($request->all()); + return $this->respond('done', $model); + } + + public function remove($id) + { + $m = self::MODEL; + if(is_null($m::find($id))){ + return $this->respond('not_found'); + } + $m::destroy($id); + return $this->respond('removed'); + } + + protected function respond($status, $data = []) + { + return response()->json($data, $this->statusCodes[$status]); + } + +} \ No newline at end of file diff --git a/lumen-test/tests/acceptance/ControllerRestActionsCommandCept.php b/lumen-test/tests/acceptance/ControllerRestActionsCommandCept.php new file mode 100644 index 0000000..75e4edf --- /dev/null +++ b/lumen-test/tests/acceptance/ControllerRestActionsCommandCept.php @@ -0,0 +1,9 @@ +wantTo('generate the REST actions trait'); +$I->runShellCommand('php artisan wn:controller:rest-actions'); +$I->seeInShellOutput('REST actions trait generated'); +$I->seeFileFound('./app/Http/Controllers/RESTActions.php'); +$I->openFile('./app/Http/Controllers/RESTActions.php'); +$I->seeInThisFile('trait RESTActions {'); diff --git a/lumen-test/tests/acceptance/ModelCommandCept.php b/lumen-test/tests/acceptance/ModelCommandCept.php index 04795d8..a64b52c 100644 --- a/lumen-test/tests/acceptance/ModelCommandCept.php +++ b/lumen-test/tests/acceptance/ModelCommandCept.php @@ -3,7 +3,7 @@ $I = new AcceptanceTester($scenario); $I->wantTo('generate a model without fillable fields or dates'); $I->runShellCommand('php artisan wn:model TestingModel --path=tests/tmp'); -$I->seeInShellOutput('Model TestingModel Generated'); +$I->seeInShellOutput('TestingModel model generated'); $I->seeFileFound('./tests/tmp/TestingModel.php'); $I->openFile('./tests/tmp/TestingModel.php'); @@ -17,7 +17,7 @@ class TestingModel extends Model { protected $dates = []; - public $rules = [ + public static $rules = [ // Validation rules ]; @@ -28,21 +28,18 @@ class TestingModel extends Model { $I->wantTo('generate a model with fillable fields'); $I->runShellCommand('php artisan wn:model TestingModel --fillable=name,title --path=tests/tmp'); -$I->seeInShellOutput('Model TestingModel Generated'); $I->seeFileFound('./tests/tmp/TestingModel.php'); $I->openFile('./tests/tmp/TestingModel.php'); $I->seeInThisFile('protected $fillable = ["name", "title"];'); $I->wantTo('generate a model with dates fields'); $I->runShellCommand('php artisan wn:model TestingModel --dates=started_at --path=tests/tmp'); -$I->seeInShellOutput('Model TestingModel Generated'); $I->seeFileFound('./tests/tmp/TestingModel.php'); $I->openFile('./tests/tmp/TestingModel.php'); $I->seeInThisFile('protected $dates = ["started_at"];'); $I->wantTo('generate a model with relations'); $I->runShellCommand('php artisan wn:model TestingModel --has-many=accounts --belongs-to="owner:App\User" --has-one=number:Phone --path=tests/tmp'); -$I->seeInShellOutput('Model TestingModel Generated'); $I->seeFileFound('./tests/tmp/TestingModel.php'); $I->openFile('./tests/tmp/TestingModel.php'); $I->seeInThisFile(' @@ -66,11 +63,10 @@ $I->seeInThisFile(' $I->wantTo('generate a model with validation rules'); $I->runShellCommand('php artisan wn:model TestingModel --rules="name=required age=integer|min:13 email=email|unique:users,email_address" --path=tests/tmp'); -$I->seeInShellOutput('Model TestingModel Generated'); $I->seeFileFound('./tests/tmp/TestingModel.php'); $I->openFile('./tests/tmp/TestingModel.php'); $I->seeInThisFile(' - public $rules = [ + public static $rules = [ "name" => "required", "age" => "integer|min:13", "email" => "email|unique:users,email_address", diff --git a/lumen-test/tests/tmp/TestingModel.php b/lumen-test/tests/tmp/TestingModel.php index a1cabee..37ff83e 100644 --- a/lumen-test/tests/tmp/TestingModel.php +++ b/lumen-test/tests/tmp/TestingModel.php @@ -8,7 +8,7 @@ class TestingModel extends Model { protected $dates = []; - public $rules = [ + public static $rules = [ "name" => "required", "age" => "integer|min:13", "email" => "email|unique:users,email_address", diff --git a/src/Commands/ControllerRestActionsCommand.php b/src/Commands/ControllerRestActionsCommand.php new file mode 100644 index 0000000..da7ca7c --- /dev/null +++ b/src/Commands/ControllerRestActionsCommand.php @@ -0,0 +1,19 @@ +getTemplate('controller/rest-actions')->get(); + + $this->save($content, "./app/Http/Controllers/RESTActions.php"); + + $this->info("REST actions trait generated !"); + } + +} \ No newline at end of file diff --git a/src/Commands/ModelCommand.php b/src/Commands/ModelCommand.php index 82703dc..2b113f7 100644 --- a/src/Commands/ModelCommand.php +++ b/src/Commands/ModelCommand.php @@ -35,7 +35,7 @@ class ModelCommand extends BaseCommand { $this->save($content, "./{$path}/{$name}.php"); - $this->info("Model {$name} Generated !"); + $this->info("{$name} model generated !"); } protected function getAsArrayFields($arg, $isOption = true) diff --git a/src/CommandsServiceProvider.php b/src/CommandsServiceProvider.php index 789bdff..637e8e5 100644 --- a/src/CommandsServiceProvider.php +++ b/src/CommandsServiceProvider.php @@ -9,6 +9,7 @@ class CommandsServiceProvider extends ServiceProvider { // $this->register $this->registerModelCommand(); + $this->registerControllerRestActionsCommand(); // $this->registerControllerCommand(); // $this->registerMigrationCommand(); // $this->registerSeedCommand(); @@ -25,6 +26,14 @@ class CommandsServiceProvider extends ServiceProvider } + protected function registerControllerRestActionsCommand(){ + $this->app->singleton('command.wn.controller.rest-actions', function($app){ + return $app['Wn\Generators\Commands\ControllerRestActionsCommand']; + }); + $this->commands('command.wn.controller.rest-actions'); + + } + protected function registerControllerCommand(){ $this->app->singleton('command.wn.controller', function($app){ return $app['Wn\Generators\Commands\ControllerCommand']; diff --git a/src/Template/Template.php b/src/Template/Template.php index 9bc9d3e..039b248 100644 --- a/src/Template/Template.php +++ b/src/Template/Template.php @@ -21,7 +21,7 @@ class Template { $this->text = $text; $this->compiled = ''; $this->data = []; - $this->dirty = false; + $this->dirty = true; } public function clean() diff --git a/templates/controller.wnt b/templates/controller.wnt index e69de29..d96b627 100644 --- a/templates/controller.wnt +++ b/templates/controller.wnt @@ -0,0 +1,10 @@ + 200, + 'created' => 201, + 'removed' => 204, + 'not_valid' => 400, + 'not_found' => 404, + 'conflict' => 409, + 'permissions' => 401 + ]; + + public function all() + { + $m = self::MODEL; + return $this->respond('done', $m::all()); + } + + public function get($id) + { + $m = self::MODEL; + $model = $m::find($id); + if(is_null($model)){ + return $this->respond('not_found'); + } + return $this->respond('done', $model); + } + + public function add(Request $request) + { + $m = self::MODEL; + $this->validate($request, $m::$rules); + return $this->respond('created', $m::create($request->all())); + } + + public function put(Request $request, $id) + { + $m = self::MODEL; + $this->validate($request, $m::$rules); + $model = $m::find($id); + if(is_null($model)){ + return $this->respond('not_found'); + } + $model->update($request->all()); + return $this->respond('done', $model); + } + + public function remove($id) + { + $m = self::MODEL; + if(is_null($m::find($id))){ + return $this->respond('not_found'); + } + $m::destroy($id); + return $this->respond('removed'); + } + + protected function respond($status, $data = []) + { + return response()->json($data, $this->statusCodes[$status]); + } + +} \ No newline at end of file diff --git a/templates/model.wnt b/templates/model.wnt index 0d1dc43..a20f3ac 100644 --- a/templates/model.wnt +++ b/templates/model.wnt @@ -8,7 +8,7 @@ class {{name}} extends Model { protected $dates = [{{dates}}]; - public $rules = [ + public static $rules = [ {{rules}} ]; diff --git a/templates/route.wnt b/templates/route.wnt deleted file mode 100644 index e69de29..0000000 diff --git a/templates/routes.wnt b/templates/routes.wnt new file mode 100644 index 0000000..560aab1 --- /dev/null +++ b/templates/routes.wnt @@ -0,0 +1,8 @@ +/** + * Routes for resource {{resource}} + */ +$app->get({{resource}}, '{{controller}}@all'); +$app->get({{resource}}.'/{id}', '{{controller}}@get'); +$app->post({{resource}}, '{{controller}}@add'); +$app->put({{resource}}.'/{id}', '{{controller}}@put'); +$app->delete({{resource}}.'/{id}', '{{controller}}@remove');