mirror of
https://github.com/ZorgCC/lumen-generators.git
synced 2025-02-28 14:21:02 +03:00
wn:resource command added
This commit is contained in:
parent
b0e163eb09
commit
0b4f299142
@ -0,0 +1,34 @@
|
||||
{
|
||||
"type": "array",
|
||||
"separator": " ",
|
||||
"fields": {
|
||||
"type": "object",
|
||||
"separator": ";",
|
||||
"fields": [
|
||||
"name",
|
||||
{
|
||||
"name": "schema",
|
||||
"type": "array",
|
||||
"separator": ":",
|
||||
"fields": {
|
||||
"type": "object",
|
||||
"separator": ".",
|
||||
"fields": [
|
||||
"name",
|
||||
{
|
||||
"name": "args",
|
||||
"type": "array",
|
||||
"separator": ",",
|
||||
"default": [],
|
||||
"fields": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"rules",
|
||||
"tags[]"
|
||||
]
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
|
||||
class CreateTaskCategoriesMigration extends Migration
|
||||
{
|
||||
|
||||
public function up()
|
||||
{
|
||||
Schema::create('task_categories', function(Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$table->string('name')->unique();
|
||||
$table->text('descr')->nullable();
|
||||
$table->integer('project_id');
|
||||
$table->timestamp('due');
|
||||
$table->foreign('project_id')
|
||||
->references('id')
|
||||
->on('projects');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
public function down()
|
||||
{
|
||||
Schema::drop('task_categories');
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
|
||||
class CreateTaskCategoriesMigration extends Migration
|
||||
{
|
||||
|
||||
public function up()
|
||||
{
|
||||
Schema::create('task_categories', function(Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$table->string('name')->unique();
|
||||
$table->text('descr')->nullable();
|
||||
$table->integer('project_id');
|
||||
$table->timestamp('due');
|
||||
$table->foreign('project_id')
|
||||
->references('id')
|
||||
->on('projects');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
public function down()
|
||||
{
|
||||
Schema::drop('task_categories');
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
|
||||
class CreateTaskCategoriesMigration extends Migration
|
||||
{
|
||||
|
||||
public function up()
|
||||
{
|
||||
Schema::create('task_categories', function(Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$table->string('name')->unique();
|
||||
$table->text('descr')->nullable();
|
||||
$table->integer('project_id');
|
||||
$table->timestamp('due');
|
||||
$table->foreign('project_id')
|
||||
->references('id')
|
||||
->on('projects');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
public function down()
|
||||
{
|
||||
Schema::drop('task_categories');
|
||||
}
|
||||
}
|
105
lumen-test/tests/acceptance/ResourceCommandCept.php
Normal file
105
lumen-test/tests/acceptance/ResourceCommandCept.php
Normal file
@ -0,0 +1,105 @@
|
||||
<?php
|
||||
$I = new AcceptanceTester($scenario);
|
||||
|
||||
$I->wantTo('generate a RESTful resource');
|
||||
$I->runShellCommand('php artisan wn:resource task_category "name;string:unique;requied;fillable descr;text:nullable;;fillable project_id;integer;required;key,fillable due;timestamp;;fillable,date" --has-many="tags,tasks" --belongs-to="project,creator:User" --migration-file=create_task_categories');
|
||||
|
||||
// Checking the model
|
||||
$I->seeInShellOutput('TaskCategory model generated');
|
||||
$I->seeFileFound('./app/TaskCategory.php');
|
||||
$I->openFile('./app/TaskCategory.php');
|
||||
|
||||
$I->seeInThisFile('namespace App;');
|
||||
$I->seeInThisFile('class TaskCategory extends Model');
|
||||
$I->seeInThisFile('protected $fillable = ["name", "descr", "project_id", "due"];');
|
||||
$I->seeInThisFile('protected $dates = ["due"];');
|
||||
$I->seeInThisFile('public static $rules = [
|
||||
"name" => "requied",
|
||||
"project_id" => "required",
|
||||
];');
|
||||
$I->seeInThisFile('
|
||||
public function tags()
|
||||
{
|
||||
return $this->hasMany("App\Tag");
|
||||
}
|
||||
|
||||
public function tasks()
|
||||
{
|
||||
return $this->hasMany("App\Task");
|
||||
}
|
||||
|
||||
public function project()
|
||||
{
|
||||
return $this->belongsTo("App\Project");
|
||||
}
|
||||
|
||||
public function creator()
|
||||
{
|
||||
return $this->belongsTo("App\User");
|
||||
}');
|
||||
$I->deleteFile('./app/TaskCategory.php');
|
||||
|
||||
// Checking the migration
|
||||
$I->seeInShellOutput('task_categories migration generated');
|
||||
$I->seeFileFound('./database/migrations/create_task_categories.php');
|
||||
$I->openFile('./database/migrations/create_task_categories.php');
|
||||
|
||||
$I->seeInThisFile('class CreateTaskCategoriesMigration extends Migration');
|
||||
$I->seeInThisFile('Schema::create(\'task_categories\', function(Blueprint $table) {
|
||||
$table->increments(\'id\');
|
||||
$table->string(\'name\')->unique();
|
||||
$table->text(\'descr\')->nullable();
|
||||
$table->integer(\'project_id\');
|
||||
$table->timestamp(\'due\');
|
||||
$table->foreign(\'project_id\')
|
||||
->references(\'id\')
|
||||
->on(\'projects\');
|
||||
$table->timestamps();
|
||||
});');
|
||||
|
||||
$I->deleteFile('./database/migrations/create_task_categories.php');
|
||||
|
||||
// Checking the RESTActions trait
|
||||
$I->seeFileFound('./app/Http/Controllers/RESTActions.php');
|
||||
$I->deleteFile('./app/Http/Controllers/RESTActions.php');
|
||||
|
||||
// Checking the controller
|
||||
$I->seeInShellOutput('TaskCategoriesController generated');
|
||||
$I->seeFileFound('./app/Http/Controllers/TaskCategoriesController.php');
|
||||
$I->openFile('./app/Http/Controllers/TaskCategoriesController.php');
|
||||
|
||||
$I->seeInThisFile('class TaskCategoriesController extends Controller {
|
||||
|
||||
const MODEL = "App\TaskCategory";
|
||||
|
||||
use RESTActions;
|
||||
|
||||
}');
|
||||
|
||||
$I->deleteFile('./app/Http/Controllers/TaskCategoriesController.php');
|
||||
|
||||
// Checking routes
|
||||
$I->openFile('./app/Http/routes.php');
|
||||
$I->seeInThisFile('
|
||||
$app->get(\'task-category\', \'TaskCategoriesController@all\');
|
||||
$app->get(\'task-category/{id}\', \'TaskCategoriesController@get\');
|
||||
$app->post(\'task-category\', \'TaskCategoriesController@add\');
|
||||
$app->put(\'task-category/{id}\', \'TaskCategoriesController@put\');
|
||||
$app->delete(\'task-category/{id}\', \'TaskCategoriesController@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();
|
||||
});
|
||||
');
|
@ -8,6 +8,7 @@ class MigrationCommand extends BaseCommand {
|
||||
{--schema= : the schema.}
|
||||
{--keys= : foreign keys.}
|
||||
{--file= : name of the migration file.}
|
||||
{--parsed : tells the command that arguments have been already parsed. To use when calling the command from an other command and passing the parsed arguments and options}
|
||||
';
|
||||
// {action : One of create, add, remove or drop options.}
|
||||
// The action is only create for the moment
|
||||
@ -45,7 +46,10 @@ class MigrationCommand extends BaseCommand {
|
||||
return " // Schema declaration";
|
||||
}
|
||||
|
||||
$items = $this->getArgumentParser('schema')->parse($schema);
|
||||
$items = $schema;
|
||||
if( ! $this->option('parsed')){
|
||||
$items = $this->getArgumentParser('schema')->parse($schema);
|
||||
}
|
||||
|
||||
$fields = [];
|
||||
foreach ($items as $item) {
|
||||
@ -73,7 +77,10 @@ class MigrationCommand extends BaseCommand {
|
||||
return " // Constraints declaration";
|
||||
}
|
||||
|
||||
$items = $this->getArgumentParser('foreign-keys')->parse($keys);
|
||||
$items = $keys;
|
||||
if(! $this->option('parsed')){
|
||||
$items = $this->getArgumentParser('foreign-keys')->parse($keys);
|
||||
}
|
||||
|
||||
$constraints = [];
|
||||
foreach ($items as $item) {
|
||||
|
@ -11,7 +11,8 @@ class ModelCommand extends BaseCommand {
|
||||
{--has-one= : hasOne relationships.}
|
||||
{--belongs-to= : belongsTo relationships.}
|
||||
{--rules= : fields validation rules.}
|
||||
{--path=app : where to store the model php file.}';
|
||||
{--path=app : where to store the model php file.}
|
||||
{--parsed : tells the command that arguments have been already parsed. To use when calling the command from an other command and passing the parsed arguments and options}';
|
||||
|
||||
protected $description = 'Generates a model class for a RESTfull resource';
|
||||
|
||||
@ -41,7 +42,7 @@ class ModelCommand extends BaseCommand {
|
||||
$arg = ($isOption) ? $this->option($arg) : $this->argument($arg);
|
||||
if(is_string($arg)){
|
||||
$arg = explode(',', $arg);
|
||||
} else {
|
||||
} else if(! is_array($arg)) {
|
||||
$arg = [];
|
||||
}
|
||||
return implode(', ', array_map(function($item){
|
||||
@ -74,9 +75,10 @@ class ModelCommand extends BaseCommand {
|
||||
$relations = [];
|
||||
$option = $this->option($option);
|
||||
if($option){
|
||||
$parser = $this->getArgumentParser('relations');
|
||||
|
||||
$items = $this->getArgumentParser('relations')->parse($option);
|
||||
|
||||
$template = $this->getTemplate('model/relation');
|
||||
$items = $parser->parse($option);
|
||||
foreach ($items as $item) {
|
||||
$item['type'] = $type;
|
||||
if(! $item['model']){
|
||||
@ -96,10 +98,12 @@ class ModelCommand extends BaseCommand {
|
||||
if(! $rules){
|
||||
return "\t\t// Validation rules";
|
||||
}
|
||||
$parser = $this->getArgumentParser('rules');
|
||||
$template = $this->getTemplate('model/rule');
|
||||
$items = $parser->parse($rules);
|
||||
$items = $rules;
|
||||
if(! $this->option('parsed')){
|
||||
$items = $this->getArgumentParser('rules')->parse($rules);
|
||||
}
|
||||
$rules = [];
|
||||
$template = $this->getTemplate('model/rule');
|
||||
foreach ($items as $item) {
|
||||
$rules[] = $template->with($item)->get();
|
||||
}
|
||||
|
@ -1,33 +1,119 @@
|
||||
<?php
|
||||
/*
|
||||
wn:resource
|
||||
{name : Name of the resource.}
|
||||
{--fields= : fields description.}
|
||||
name
|
||||
schema
|
||||
attrs: fillable, date
|
||||
rules
|
||||
<?php namespace Wn\Generators\Commands;
|
||||
|
||||
|
||||
class ResourceCommand extends BaseCommand {
|
||||
|
||||
protected $signature = 'wn:resource
|
||||
{name : Name of the resource.}
|
||||
{fields : fields description.}
|
||||
{--has-many= : hasMany relationships.}
|
||||
{--has-one= : hasOne relationships.}
|
||||
{--belongs-to= : belongsTo relationships.}
|
||||
{--migration-file= : the migration file name.}
|
||||
';
|
||||
|
||||
wn:migration
|
||||
table => str_plural(name)
|
||||
--schema =>
|
||||
wn:route
|
||||
resource => name
|
||||
protected $description = 'Generates a model, migration, controller and routes for RESTful resource';
|
||||
|
||||
wn:controller
|
||||
model => (namespace from --model-path) ucwords(camel_case(name))
|
||||
--no-routes => true
|
||||
protected $fields;
|
||||
|
||||
wn:model
|
||||
name => ucwords(camel_case(name))
|
||||
--fillable => having_fillable_attr(fields)
|
||||
--dates => having_date_attr(fields)
|
||||
--rules => rules_of(fields)
|
||||
--path => --model-path
|
||||
--has-many => --has-many
|
||||
--has-one => --has-one
|
||||
--belongs-to => --belongs-to
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$this->parseFields();
|
||||
|
||||
$resourceName = $this->argument('name');
|
||||
$modelName = ucwords(camel_case($resourceName));
|
||||
$tableName = str_plural($resourceName);
|
||||
|
||||
// generating the model
|
||||
$this->call('wn:model', [
|
||||
'name' => $modelName,
|
||||
'--fillable' => $this->fieldsHavingTag('fillable'),
|
||||
'--dates' => $this->fieldsHavingTag('date'),
|
||||
'--has-many' => $this->option('has-many'),
|
||||
'--has-one' => $this->option('has-one'),
|
||||
'--belongs-to' => $this->option('belongs-to'),
|
||||
'--rules' => $this->rules(),
|
||||
'--path' => 'app',
|
||||
'--parsed' => true
|
||||
]);
|
||||
|
||||
// generating the migration
|
||||
$this->call('wn:migration', [
|
||||
'table' => $tableName,
|
||||
'--schema' => $this->schema(),
|
||||
'--keys' => $this->foreignKeys(),
|
||||
'--file' => $this->option('migration-file'),
|
||||
'--parsed' => true
|
||||
]);
|
||||
|
||||
// generation REST actions trait if doesn't exist
|
||||
if(! $this->fs->exists('./app/Http/Controllers/RESTActions.php')){
|
||||
$this->call('wn:controller:rest-actions');
|
||||
}
|
||||
|
||||
// generating the controller and routes
|
||||
$this->call('wn:controller', [
|
||||
'model' => $modelName,
|
||||
'--no-routes' => false
|
||||
]);
|
||||
|
||||
}
|
||||
|
||||
protected function parseFields()
|
||||
{
|
||||
$fields = $this->argument('fields');
|
||||
if(! $fields){
|
||||
$this->fields = [];
|
||||
} else {
|
||||
$this->fields = $this->getArgumentParser('fields')
|
||||
->parse($fields);
|
||||
}
|
||||
}
|
||||
|
||||
protected function fieldsHavingTag($tag)
|
||||
{
|
||||
return array_map(function($field){
|
||||
return $field['name'];
|
||||
}, array_filter($this->fields, function($field) use($tag){
|
||||
return in_array($tag, $field['tags']);
|
||||
}));
|
||||
}
|
||||
|
||||
protected function rules()
|
||||
{
|
||||
return array_map(function($field){
|
||||
return [
|
||||
'name' => $field['name'],
|
||||
'rule' => $field['rules']
|
||||
];
|
||||
}, array_filter($this->fields, function($field){
|
||||
return !empty($field['rules']);
|
||||
}));
|
||||
}
|
||||
|
||||
protected function schema()
|
||||
{
|
||||
return array_map(function($field){
|
||||
return array_merge([[
|
||||
'name' => $field['name'],
|
||||
'args' => []
|
||||
]], $field['schema']);
|
||||
}, $this->fields);
|
||||
}
|
||||
|
||||
protected function foreignKeys()
|
||||
{
|
||||
return array_map(function($field){
|
||||
return [
|
||||
'name' => $field['name'],
|
||||
'column' => '',
|
||||
'table' => '',
|
||||
'on_delete' => '',
|
||||
'on_update' => ''
|
||||
];
|
||||
}, array_filter($this->fields, function($field){
|
||||
return in_array('key', $field['tags']);
|
||||
}));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,4 @@
|
||||
<?php
|
||||
/**
|
||||
* Comming Soon
|
||||
*/
|
@ -0,0 +1,4 @@
|
||||
<?php
|
||||
/**
|
||||
* Comming Soon
|
||||
*/
|
@ -12,7 +12,7 @@ class CommandsServiceProvider extends ServiceProvider
|
||||
$this->registerControllerCommand();
|
||||
$this->registerRouteCommand();
|
||||
$this->registerMigrationCommand();
|
||||
// $this->registerResourceCommand();
|
||||
$this->registerResourceCommand();
|
||||
// $this->registerSeedCommand();
|
||||
// $this->registerTestCommand();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user