wn:route command added and wn:controller command modified

This commit is contained in:
Amine Ben hammou 2015-09-26 00:57:27 +01:00
parent 6528a95f74
commit df6abe636c
9 changed files with 172 additions and 15 deletions

24
formats/schemas.json Normal file
View File

@ -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"
}
}
]
}
}
}

View File

@ -11,6 +11,6 @@
|
*/
$app->get('/', function () use ($app) {
$app->get("/", function () use ($app) {
return $app->welcome();
});

View File

@ -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', '<?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It is a breeze. Simply tell Lumen the URIs it should respond to
| and give it the Closure to call when that URI is requested.
|
*/
$app->get("/", function () use ($app) {
return $app->welcome();
});
');

View File

@ -0,0 +1,61 @@
<?php
$I = new AcceptanceTester($scenario);
$I->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', '<?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It is a breeze. Simply tell Lumen the URIs it should respond to
| and give it the Closure to call when that URI is requested.
|
*/
$app->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', '<?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It is a breeze. Simply tell Lumen the URIs it should respond to
| and give it the Closure to call when that URI is requested.
|
*/
$app->get("/", function () use ($app) {
return $app->welcome();
});
');

View File

@ -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
]);
}
}
}

View File

@ -0,0 +1,39 @@
<?php namespace Wn\Generators\Commands;
class RouteCommand extends BaseCommand {
protected $signature = 'wn:route
{resource : Name of the resource.}
{--controller= : Name of the RESTful controller.}';
protected $description = 'Generates RESTful routes.';
public function handle()
{
$resource = $this->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;
}
}

View File

@ -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();
}

View File

@ -1,7 +1,7 @@
<?php namespace App\Http\Controllers;
class {{name}}Controller extends Controller {
class {{name}} extends Controller {
const MODEL = "{{model}}";

View File

@ -1,8 +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');
$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');