wn:controller:rest-actions command added

This commit is contained in:
Amine Ben hammou 2015-09-24 01:25:55 +01:00
parent 96b1866e35
commit db0f6e8033
13 changed files with 198 additions and 11 deletions

View File

@ -0,0 +1,68 @@
<?php namespace App\Http\Controllers;
use Illuminate\Http\Request;
trait RESTActions {
protected $statusCodes = [
'done' => 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]);
}
}

View File

@ -0,0 +1,9 @@
<?php
$I = new AcceptanceTester($scenario);
$I->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 {');

View File

@ -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",

View File

@ -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",

View File

@ -0,0 +1,19 @@
<?php namespace Wn\Generators\Commands;
class ControllerRestActionsCommand extends BaseCommand {
protected $signature = 'wn:controller:rest-actions';
protected $description = 'Generates REST actions trait to use into controllers';
public function handle()
{
$content = $this->getTemplate('controller/rest-actions')->get();
$this->save($content, "./app/Http/Controllers/RESTActions.php");
$this->info("REST actions trait generated !");
}
}

View File

@ -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)

View File

@ -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'];

View File

@ -21,7 +21,7 @@ class Template {
$this->text = $text;
$this->compiled = '';
$this->data = [];
$this->dirty = false;
$this->dirty = true;
}
public function clean()

View File

@ -0,0 +1,10 @@
<?php namespace App\Http\Controllers;
class {{name}}Controller extends Controller {
const MODEL = '{{model}}';
use RESTActions;
}

View File

@ -0,0 +1,68 @@
<?php namespace App\Http\Controllers;
use Illuminate\Http\Request;
trait RESTActions {
protected $statusCodes = [
'done' => 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]);
}
}

View File

@ -8,7 +8,7 @@ class {{name}} extends Model {
protected $dates = [{{dates}}];
public $rules = [
public static $rules = [
{{rules}}
];

View File

8
templates/routes.wnt Normal file
View File

@ -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');