mirror of
https://github.com/ZorgCC/lumen-generators.git
synced 2024-12-27 22:45:27 +03:00
wn:controller:rest-actions command added
This commit is contained in:
parent
96b1866e35
commit
db0f6e8033
68
lumen-test/app/Http/Controllers/RESTActions.php
Normal file
68
lumen-test/app/Http/Controllers/RESTActions.php
Normal 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]);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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 {');
|
@ -3,7 +3,7 @@ $I = new AcceptanceTester($scenario);
|
|||||||
|
|
||||||
$I->wantTo('generate a model without fillable fields or dates');
|
$I->wantTo('generate a model without fillable fields or dates');
|
||||||
$I->runShellCommand('php artisan wn:model TestingModel --path=tests/tmp');
|
$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->seeFileFound('./tests/tmp/TestingModel.php');
|
||||||
$I->openFile('./tests/tmp/TestingModel.php');
|
$I->openFile('./tests/tmp/TestingModel.php');
|
||||||
|
|
||||||
@ -17,7 +17,7 @@ class TestingModel extends Model {
|
|||||||
|
|
||||||
protected $dates = [];
|
protected $dates = [];
|
||||||
|
|
||||||
public $rules = [
|
public static $rules = [
|
||||||
// Validation rules
|
// Validation rules
|
||||||
];
|
];
|
||||||
|
|
||||||
@ -28,21 +28,18 @@ class TestingModel extends Model {
|
|||||||
|
|
||||||
$I->wantTo('generate a model with fillable fields');
|
$I->wantTo('generate a model with fillable fields');
|
||||||
$I->runShellCommand('php artisan wn:model TestingModel --fillable=name,title --path=tests/tmp');
|
$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->seeFileFound('./tests/tmp/TestingModel.php');
|
||||||
$I->openFile('./tests/tmp/TestingModel.php');
|
$I->openFile('./tests/tmp/TestingModel.php');
|
||||||
$I->seeInThisFile('protected $fillable = ["name", "title"];');
|
$I->seeInThisFile('protected $fillable = ["name", "title"];');
|
||||||
|
|
||||||
$I->wantTo('generate a model with dates fields');
|
$I->wantTo('generate a model with dates fields');
|
||||||
$I->runShellCommand('php artisan wn:model TestingModel --dates=started_at --path=tests/tmp');
|
$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->seeFileFound('./tests/tmp/TestingModel.php');
|
||||||
$I->openFile('./tests/tmp/TestingModel.php');
|
$I->openFile('./tests/tmp/TestingModel.php');
|
||||||
$I->seeInThisFile('protected $dates = ["started_at"];');
|
$I->seeInThisFile('protected $dates = ["started_at"];');
|
||||||
|
|
||||||
$I->wantTo('generate a model with relations');
|
$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->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->seeFileFound('./tests/tmp/TestingModel.php');
|
||||||
$I->openFile('./tests/tmp/TestingModel.php');
|
$I->openFile('./tests/tmp/TestingModel.php');
|
||||||
$I->seeInThisFile('
|
$I->seeInThisFile('
|
||||||
@ -66,11 +63,10 @@ $I->seeInThisFile('
|
|||||||
|
|
||||||
$I->wantTo('generate a model with validation rules');
|
$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->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->seeFileFound('./tests/tmp/TestingModel.php');
|
||||||
$I->openFile('./tests/tmp/TestingModel.php');
|
$I->openFile('./tests/tmp/TestingModel.php');
|
||||||
$I->seeInThisFile('
|
$I->seeInThisFile('
|
||||||
public $rules = [
|
public static $rules = [
|
||||||
"name" => "required",
|
"name" => "required",
|
||||||
"age" => "integer|min:13",
|
"age" => "integer|min:13",
|
||||||
"email" => "email|unique:users,email_address",
|
"email" => "email|unique:users,email_address",
|
||||||
|
@ -8,7 +8,7 @@ class TestingModel extends Model {
|
|||||||
|
|
||||||
protected $dates = [];
|
protected $dates = [];
|
||||||
|
|
||||||
public $rules = [
|
public static $rules = [
|
||||||
"name" => "required",
|
"name" => "required",
|
||||||
"age" => "integer|min:13",
|
"age" => "integer|min:13",
|
||||||
"email" => "email|unique:users,email_address",
|
"email" => "email|unique:users,email_address",
|
||||||
|
19
src/Commands/ControllerRestActionsCommand.php
Normal file
19
src/Commands/ControllerRestActionsCommand.php
Normal 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 !");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -35,7 +35,7 @@ class ModelCommand extends BaseCommand {
|
|||||||
|
|
||||||
$this->save($content, "./{$path}/{$name}.php");
|
$this->save($content, "./{$path}/{$name}.php");
|
||||||
|
|
||||||
$this->info("Model {$name} Generated !");
|
$this->info("{$name} model generated !");
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getAsArrayFields($arg, $isOption = true)
|
protected function getAsArrayFields($arg, $isOption = true)
|
||||||
|
@ -9,6 +9,7 @@ class CommandsServiceProvider extends ServiceProvider
|
|||||||
{
|
{
|
||||||
// $this->register
|
// $this->register
|
||||||
$this->registerModelCommand();
|
$this->registerModelCommand();
|
||||||
|
$this->registerControllerRestActionsCommand();
|
||||||
// $this->registerControllerCommand();
|
// $this->registerControllerCommand();
|
||||||
// $this->registerMigrationCommand();
|
// $this->registerMigrationCommand();
|
||||||
// $this->registerSeedCommand();
|
// $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(){
|
protected function registerControllerCommand(){
|
||||||
$this->app->singleton('command.wn.controller', function($app){
|
$this->app->singleton('command.wn.controller', function($app){
|
||||||
return $app['Wn\Generators\Commands\ControllerCommand'];
|
return $app['Wn\Generators\Commands\ControllerCommand'];
|
||||||
|
@ -21,7 +21,7 @@ class Template {
|
|||||||
$this->text = $text;
|
$this->text = $text;
|
||||||
$this->compiled = '';
|
$this->compiled = '';
|
||||||
$this->data = [];
|
$this->data = [];
|
||||||
$this->dirty = false;
|
$this->dirty = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function clean()
|
public function clean()
|
||||||
|
@ -0,0 +1,10 @@
|
|||||||
|
<?php namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
|
||||||
|
class {{name}}Controller extends Controller {
|
||||||
|
|
||||||
|
const MODEL = '{{model}}';
|
||||||
|
|
||||||
|
use RESTActions;
|
||||||
|
|
||||||
|
}
|
68
templates/controller/rest-actions.wnt
Normal file
68
templates/controller/rest-actions.wnt
Normal 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]);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -8,7 +8,7 @@ class {{name}} extends Model {
|
|||||||
|
|
||||||
protected $dates = [{{dates}}];
|
protected $dates = [{{dates}}];
|
||||||
|
|
||||||
public $rules = [
|
public static $rules = [
|
||||||
{{rules}}
|
{{rules}}
|
||||||
];
|
];
|
||||||
|
|
||||||
|
8
templates/routes.wnt
Normal file
8
templates/routes.wnt
Normal 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');
|
Loading…
Reference in New Issue
Block a user