wn:controller command added

This commit is contained in:
Amine Ben hammou 2015-09-24 03:04:44 +01:00
parent dc3351a8a6
commit 6528a95f74
8 changed files with 79 additions and 91 deletions

View File

@ -1,68 +0,0 @@
<?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,38 @@
<?php
$I = new AcceptanceTester($scenario);
$I->wantTo('generate a RESTful controller with short model name');
$I->runShellCommand('php artisan wn:controller Test');
$I->seeInShellOutput('TestsController generated');
$I->seeFileFound('./app/Http/Controllers/TestsController.php');
$I->openFile('./app/Http/Controllers/TestsController.php');
$I->seeFileContentsEqual('<?php namespace App\Http\Controllers;
class TestsController extends Controller {
const MODEL = "App\\Test";
use RESTActions;
}
');
$I->deleteFile('./app/Http/Controllers/TestsController.php');
$I->wantTo('generate a RESTful controller with full model name');
$I->runShellCommand('php artisan wn:controller "App\Models\Category"');
$I->seeInShellOutput('CategoriesController generated');
$I->seeFileFound('./app/Http/Controllers/CategoriesController.php');
$I->openFile('./app/Http/Controllers/CategoriesController.php');
$I->seeFileContentsEqual('<?php namespace App\Http\Controllers;
class CategoriesController extends Controller {
const MODEL = "App\\Models\\Category";
use RESTActions;
}
');
$I->deleteFile('./app/Http/Controllers/CategoriesController.php');

View File

@ -7,3 +7,4 @@ $I->seeInShellOutput('REST actions trait generated');
$I->seeFileFound('./app/Http/Controllers/RESTActions.php');
$I->openFile('./app/Http/Controllers/RESTActions.php');
$I->seeInThisFile('trait RESTActions {');
$I->deleteFile('./app/Http/Controllers/RESTActions.php');

View File

@ -72,3 +72,5 @@ $I->seeInThisFile('
"email" => "email|unique:users,email_address",
];
');
$I->deleteFile('./tests/tmp/TestingModel.php');

View File

@ -1,19 +0,0 @@
<?php namespace Tests\Tmp;
use Illuminate\Database\Eloquent\Model;
class TestingModel extends Model {
protected $fillable = [];
protected $dates = [];
public static $rules = [
"name" => "required",
"age" => "integer|min:13",
"email" => "email|unique:users,email_address",
];
// Relationships
}

View File

@ -0,0 +1,35 @@
<?php namespace Wn\Generators\Commands;
class ControllerCommand extends BaseCommand {
protected $signature = 'wn:controller
{model : Name of the model (with namespace if not App;}';
protected $description = 'Generates RESTful controller using the RESTActions trait';
public function handle()
{
$model = $this->argument('model');
$name = '';
if(strrpos($model, "\\") === false){
$name = $model;
$model = "App\\" . $model;
} else {
$name = explode("\\", $model);
$name = $name[count($name) - 1];
}
$name = ucwords(str_plural($name));
$content = $this->getTemplate('controller')
->with([
'name' => $name,
'model' => $model
])
->get();
$this->save($content, "./app/Http/Controllers/{$name}Controller.php");
$this->info("{$name}Controller generated !");
}
}

View File

@ -7,10 +7,9 @@ class CommandsServiceProvider extends ServiceProvider
public function register()
{
// $this->register
$this->registerModelCommand();
$this->registerControllerRestActionsCommand();
// $this->registerControllerCommand();
$this->registerControllerCommand();
// $this->registerMigrationCommand();
// $this->registerSeedCommand();
// $this->registerRouteCommand();

View File

@ -3,8 +3,8 @@
class {{name}}Controller extends Controller {
const MODEL = '{{model}}';
const MODEL = "{{model}}";
use RESTActions;
}
}