From df6abe636c42a33736fb448a40c939709d6a1f61 Mon Sep 17 00:00:00 2001 From: Amine Ben hammou Date: Sat, 26 Sep 2015 00:57:27 +0100 Subject: [PATCH] wn:route command added and wn:controller command modified --- formats/schemas.json | 24 ++++++++ lumen-test/app/Http/routes.php | 2 +- .../acceptance/ControllerCommandCept.php | 29 ++++++++- .../tests/acceptance/RouteCommandCept.php | 61 +++++++++++++++++++ src/Commands/ControllerCommand.php | 18 ++++-- src/Commands/RouteCommand.php | 39 ++++++++++++ src/CommandsServiceProvider.php | 2 +- templates/controller.wnt | 2 +- templates/routes.wnt | 10 +-- 9 files changed, 172 insertions(+), 15 deletions(-) create mode 100644 formats/schemas.json create mode 100644 lumen-test/tests/acceptance/RouteCommandCept.php diff --git a/formats/schemas.json b/formats/schemas.json new file mode 100644 index 0000000..bc24777 --- /dev/null +++ b/formats/schemas.json @@ -0,0 +1,24 @@ +{ + "type": "array", + "separator": " ", + "fields": { + "type": "array", + "separator": ":", + "fields": { + "type": "object", + "separator": ".", + "fields": [ + "name", + { + "name": "args", + "type": "array", + "separator": ",", + "default": [], + "fields": { + "type": "string" + } + } + ] + } + } +} \ No newline at end of file diff --git a/lumen-test/app/Http/routes.php b/lumen-test/app/Http/routes.php index 5ecfcd1..632ded3 100644 --- a/lumen-test/app/Http/routes.php +++ b/lumen-test/app/Http/routes.php @@ -11,6 +11,6 @@ | */ -$app->get('/', function () use ($app) { +$app->get("/", function () use ($app) { return $app->welcome(); }); diff --git a/lumen-test/tests/acceptance/ControllerCommandCept.php b/lumen-test/tests/acceptance/ControllerCommandCept.php index 84f2c86..2bbb66e 100644 --- a/lumen-test/tests/acceptance/ControllerCommandCept.php +++ b/lumen-test/tests/acceptance/ControllerCommandCept.php @@ -2,7 +2,7 @@ $I = new AcceptanceTester($scenario); $I->wantTo('generate a RESTful controller with short model name'); -$I->runShellCommand('php artisan wn:controller Test'); +$I->runShellCommand('php artisan wn:controller Test --no-routes'); $I->seeInShellOutput('TestsController generated'); $I->seeFileFound('./app/Http/Controllers/TestsController.php'); $I->openFile('./app/Http/Controllers/TestsController.php'); @@ -19,7 +19,7 @@ class TestsController extends Controller { '); $I->deleteFile('./app/Http/Controllers/TestsController.php'); -$I->wantTo('generate a RESTful controller with full model name'); +$I->wantTo('generate a RESTful controller with full model name and routes'); $I->runShellCommand('php artisan wn:controller "App\Models\Category"'); $I->seeInShellOutput('CategoriesController generated'); $I->seeFileFound('./app/Http/Controllers/CategoriesController.php'); @@ -36,3 +36,28 @@ class CategoriesController extends Controller { } '); $I->deleteFile('./app/Http/Controllers/CategoriesController.php'); +$I->openFile('./app/Http/routes.php'); +$I->seeInThisFile(" +\$app->get('category', 'CategoriesController@all'); +\$app->get('category/{id}', 'CategoriesController@get'); +\$app->post('category', 'CategoriesController@add'); +\$app->put('category/{id}', 'CategoriesController@put'); +\$app->delete('category/{id}', 'CategoriesController@remove'); +"); +$I->writeToFile('./app/Http/routes.php', 'get("/", function () use ($app) { + return $app->welcome(); +}); +'); \ No newline at end of file diff --git a/lumen-test/tests/acceptance/RouteCommandCept.php b/lumen-test/tests/acceptance/RouteCommandCept.php new file mode 100644 index 0000000..82a828f --- /dev/null +++ b/lumen-test/tests/acceptance/RouteCommandCept.php @@ -0,0 +1,61 @@ +wantTo('generate RESTful routes for a resource with default controller'); +$I->runShellCommand('php artisan wn:route project-type'); +$I->seeInShellOutput('project-type routes generated'); +$I->openFile('./app/Http/routes.php'); +$I->seeInThisFile(" +\$app->get('project-type', 'ProjectTypesController@all'); +\$app->get('project-type/{id}', 'ProjectTypesController@get'); +\$app->post('project-type', 'ProjectTypesController@add'); +\$app->put('project-type/{id}', 'ProjectTypesController@put'); +\$app->delete('project-type/{id}', 'ProjectTypesController@remove'); +"); +$I->writeToFile('./app/Http/routes.php', 'get("/", function () use ($app) { + return $app->welcome(); +}); +'); + + +$I->wantTo('generate RESTful routes for a resource with custom controller'); +$I->runShellCommand('php artisan wn:route foo --controller=customController'); +$I->seeInShellOutput('foo routes generated'); +$I->openFile('./app/Http/routes.php'); +$I->seeInThisFile(" +\$app->get('foo', 'customController@all'); +\$app->get('foo/{id}', 'customController@get'); +\$app->post('foo', 'customController@add'); +\$app->put('foo/{id}', 'customController@put'); +\$app->delete('foo/{id}', 'customController@remove'); +"); +$I->writeToFile('./app/Http/routes.php', 'get("/", function () use ($app) { + return $app->welcome(); +}); +'); \ No newline at end of file diff --git a/src/Commands/ControllerCommand.php b/src/Commands/ControllerCommand.php index bb893f8..d046320 100644 --- a/src/Commands/ControllerCommand.php +++ b/src/Commands/ControllerCommand.php @@ -4,7 +4,8 @@ class ControllerCommand extends BaseCommand { protected $signature = 'wn:controller - {model : Name of the model (with namespace if not App;}'; + {model : Name of the model (with namespace if not App;)} + {--no-routes= : without routes}'; protected $description = 'Generates RESTful controller using the RESTActions trait'; @@ -19,17 +20,24 @@ class ControllerCommand extends BaseCommand { $name = explode("\\", $model); $name = $name[count($name) - 1]; } - $name = ucwords(str_plural($name)); + $controller = ucwords(str_plural($name)) . 'Controller'; $content = $this->getTemplate('controller') ->with([ - 'name' => $name, + 'name' => $controller, 'model' => $model ]) ->get(); - $this->save($content, "./app/Http/Controllers/{$name}Controller.php"); + $this->save($content, "./app/Http/Controllers/{$controller}.php"); - $this->info("{$name}Controller generated !"); + $this->info("{$controller} generated !"); + + if(! $this->option('no-routes')){ + $this->call('wn:route', [ + 'resource' => snake_case($name, '-'), + '--controller' => $controller + ]); + } } } \ No newline at end of file diff --git a/src/Commands/RouteCommand.php b/src/Commands/RouteCommand.php index e69de29..2d80ab0 100644 --- a/src/Commands/RouteCommand.php +++ b/src/Commands/RouteCommand.php @@ -0,0 +1,39 @@ +argument('resource'); + + $content = $this->fs->get('./app/Http/routes.php'); + + $content .= PHP_EOL . $this->getTemplate('routes') + ->with([ + 'resource' => $resource, + 'controller' => $this->getController() + ]) + ->get(); + + $this->save($content, './app/Http/routes.php'); + + $this->info("{$resource} routes generated !"); + } + + protected function getController() + { + $controller = $this->option('controller'); + if(! $controller){ + $controller = ucwords(str_plural(camel_case($this->argument('resource')))) . 'Controller'; + } + return $controller; + } + +} \ No newline at end of file diff --git a/src/CommandsServiceProvider.php b/src/CommandsServiceProvider.php index b893ae3..75f62e2 100644 --- a/src/CommandsServiceProvider.php +++ b/src/CommandsServiceProvider.php @@ -10,9 +10,9 @@ class CommandsServiceProvider extends ServiceProvider $this->registerModelCommand(); $this->registerControllerRestActionsCommand(); $this->registerControllerCommand(); + $this->registerRouteCommand(); // $this->registerMigrationCommand(); // $this->registerSeedCommand(); - // $this->registerRouteCommand(); // $this->registerTestCommand(); // $this->registerResourceCommand(); } diff --git a/templates/controller.wnt b/templates/controller.wnt index 3a8d74f..fe057b8 100644 --- a/templates/controller.wnt +++ b/templates/controller.wnt @@ -1,7 +1,7 @@ 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'); +$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');