diff --git a/README.md b/README.md index dc053af..d3214a3 100644 --- a/README.md +++ b/README.md @@ -79,7 +79,7 @@ wn:route Generates RESTful routes. To generate a RESTful resource for your application (model, migration, controller and RESTful routes), you simply need to run one single command. For example: ``` -php artisan wn:resource task "name;string;required;fillable project_id;integer:unsigned;numeric;fillable,key due;date;;date" --belongs-to=project +php artisan wn:resource task "name;string;required;fillable project_id;integer:unsigned;numeric;fillable,key due;date;;date" --add=timestamps --belongs-to=project ``` will generate these files: @@ -257,7 +257,7 @@ More then that, you can generate multiple resources with only one command ! [Cli The `wn:model` command is used to generate a model class based on Eloquent. It has the following syntax: ``` -wn:model name [--fillable=...] [--dates=...] [--has-many=...] [--has-one=...] [--belongs-to=...] [--belongs-to-many=...] [--rules=...] [--path=...] [--force=true] +wn:model name [--fillable=...] [--dates=...] [--has-many=...] [--has-one=...] [--belongs-to=...] [--belongs-to-many=...] [--rules=...] [--timestamps=false] [--path=...] [--force=true] ``` - **name**: the name of the model. @@ -357,6 +357,8 @@ gives: ]; ``` +- **--timestamps**: Enables timestamps on the model. Giving `--timestamps=false` will add `public $timestamps = false;` to the generated model. The default value is `true`. + - **--force**: tells the generator to override the existing file. By default, if the model file already exists, it will not be overriden and the output will be something like: ``` @@ -368,7 +370,7 @@ TestingModel model already exists; use --force option to override it ! The `wn:migration` command is used to generate a migration to create a table with schema. It has the following syntax: ``` -wn:migration table [--schema=...] [--keys=...] [--force=true] [--file=...] +wn:migration table [--schema=...] [--add=...] [--keys=...] [--force=true] [--file=...] ``` - **table**: the name of the table to create. @@ -398,7 +400,7 @@ class CreateTasksMigration extends Migration $table->decimal('amount', 5, 2)->after('size')->default(8); $table->string('title')->nullable(); // Constraints declaration - $table->timestamps(); + }); } @@ -409,6 +411,8 @@ class CreateTasksMigration extends Migration } ``` +- **--add**: Specifies additional columns like `timestamps`, `softDeletes`, `rememberToken` and `nullableTimestamps`. + - **--keys**: the foreign keys of the table following the syntax `field:column:table:on_delete:on_update ...`. The `column` is optional ("id" by default). The `table` is optional if the field follows the naming convention `singular_table_name_id`. `on_delete` and `on_update` are optional too. ``` @@ -433,15 +437,17 @@ $table->foreign('user_id') The `wn:pivot-table` command is used to generate a migration to create a pivot table between two models. It has the following syntax: ``` -wn:pivot-table model1 model2 [--force=true] [--file=...] +wn:pivot-table model1 model2 [--add=...] [--force=true] [--file=...] ``` - **model1** and **model2**: names of the two models (or the two tables if the models don't follow the naming conventions) +- **--add**: Specifies additional columns like `timestamps`, `softDeletes`, `rememberToken` and `nullableTimestamps`. + - **--file**: The migration file name. By default the name follows the patern `date_time_create_table_name.php`. ``` -php artisan wn:pivot-table Tag Project +php artisan wn:pivot-table Tag Project --add=timestamps ``` gives: @@ -541,7 +547,7 @@ $app->delete('project-type/{id}', 'ProjectTypesController@remove'); ### Resource Generator -The `wn:resource` command makes it very easy to generate a RESTful resource. It generates a model, migration, controller and routes. The syntax is : `wn:resource name fields [--has-many=...] [--has-one=...] [--belongs-to=...] [--migration-file=...] [--path=...] [--force=true]` +The `wn:resource` command makes it very easy to generate a RESTful resource. It generates a model, migration, controller and routes. The syntax is : `wn:resource name fields [--add=...] [--has-many=...] [--has-one=...] [--belongs-to=...] [--migration-file=...] [--path=...] [--force=true]` - **name**: the name of the resource used in the URLs and to determine the model, table and controller names. @@ -561,6 +567,8 @@ The `wn:resource` command makes it very easy to generate a RESTful resource. It - `key`: this field is a foreign key. +- **--add**: Specifies additional columns like `timestamps`, `softDeletes`, `rememberToken` and `nullableTimestamps` of the migration and if the list contains no timestamps, the model with contain `public $timestamps = false;`. + - **--has-one**, **--has-many** and **--belongs-to** are the same as for the `wn:model` command. - **--migration-file**: passed to the `wn:migration` as the `--file` option. @@ -623,13 +631,17 @@ To test the generators, I included a fresh lumen installation under the folder ` ## Development Notes - **Comming versions** - + - **Seeder and Test generators** - - Requested Feature: [Disabling timestamps](https://github.com/webNeat/lumen-generators/issues/15) - - Requested Feature: [Custom Templates](https://github.com/webNeat/lumen-generators/issues/13) +- **Version 1.3.0** + + - Requested Feature: [Disabling timestamps](https://github.com/webNeat/lumen-generators/issues/15) + + - Requested Feature: [Lumen 5.3 routes support](https://github.com/webNeat/lumen-generators/issues/21) + - **Version 1.2.0** - Tests fixed. diff --git a/lumen-test/tests/acceptance/ControllerRestActionsCommandCept.php b/lumen-test/tests/acceptance/ControllerRestActionsCommandCept.php index 92fe1bc..d936df5 100644 --- a/lumen-test/tests/acceptance/ControllerRestActionsCommandCept.php +++ b/lumen-test/tests/acceptance/ControllerRestActionsCommandCept.php @@ -1,10 +1,10 @@ -wantTo('generate the REST actions trait'); -$I->runShellCommand('php artisan wn:controller:rest-actions'); +$I->runShellCommand('php artisan wn:controller:rest-actions --force=true'); $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'); \ No newline at end of file +$I->deleteFile('./app/Http/Controllers/RESTActions.php'); diff --git a/lumen-test/tests/acceptance/MigrationCommandCept.php b/lumen-test/tests/acceptance/MigrationCommandCept.php index f90a353..5d3a01b 100644 --- a/lumen-test/tests/acceptance/MigrationCommandCept.php +++ b/lumen-test/tests/acceptance/MigrationCommandCept.php @@ -2,7 +2,7 @@ $I = new AcceptanceTester($scenario); $I->wantTo('generate a migration without schema'); -$I->runShellCommand('php artisan wn:migration tasks --file=create_tasks'); +$I->runShellCommand('php artisan wn:migration tasks --add=timestamps --file=create_tasks'); $I->seeInShellOutput('tasks migration generated'); $I->seeFileFound('./database/migrations/create_tasks.php'); $I->openFile('./database/migrations/create_tasks.php'); @@ -32,8 +32,39 @@ class CreateTasksTable extends Migration '); $I->deleteFile('./database/migrations/create_tasks.php'); +$I->wantTo('generate a migration without schema or timestamps'); +$I->runShellCommand('php artisan wn:migration tasks --file=create_tasks'); +$I->seeInShellOutput('tasks migration generated'); +$I->seeFileFound('./database/migrations/create_tasks.php'); +$I->openFile('./database/migrations/create_tasks.php'); +$I->seeFileContentsEqual('increments(\'id\'); + // Schema declaration + // Constraints declaration + + }); + } + + public function down() + { + Schema::drop(\'tasks\'); + } +} +'); +$I->deleteFile('./database/migrations/create_tasks.php'); + $I->wantTo('generate a migration with schema'); -$I->runShellCommand('php artisan wn:migration tasks --file=create_tasks --schema="amount:decimal.5,2:after.\'size\':default.8 title:string:nullable"'); +$I->runShellCommand('php artisan wn:migration tasks --add=timestamps --file=create_tasks --schema="amount:decimal.5,2:after.\'size\':default.8 title:string:nullable"'); $I->seeInShellOutput('tasks migration generated'); $I->seeFileFound('./database/migrations/create_tasks.php'); $I->openFile('./database/migrations/create_tasks.php'); diff --git a/lumen-test/tests/acceptance/ModelCommandCept.php b/lumen-test/tests/acceptance/ModelCommandCept.php index 5398b44..4c0918e 100644 --- a/lumen-test/tests/acceptance/ModelCommandCept.php +++ b/lumen-test/tests/acceptance/ModelCommandCept.php @@ -27,6 +27,34 @@ class TestingModel extends Model { '); $I->deleteFile('./tests/tmp/TestingModel.php'); +$I->wantTo('generate a model without fillable fields, dates or timestamps'); +$I->runShellCommand('php artisan wn:model TestingModel --path=tests/tmp --force=true --timestamps=false'); +$I->seeInShellOutput('TestingModel model generated'); +$I->seeFileFound('./tests/tmp/TestingModel.php'); +$I->openFile('./tests/tmp/TestingModel.php'); + +$I->seeFileContentsEqual('deleteFile('./tests/tmp/TestingModel.php'); + $I->wantTo('generate a model with fillable fields'); $I->runShellCommand('php artisan wn:model TestingModel --fillable=name,title --path=tests/tmp'); $I->seeFileFound('./tests/tmp/TestingModel.php'); @@ -77,4 +105,4 @@ $I->seeInThisFile( " ];" ); -$I->deleteFile('./tests/tmp/TestingModel.php'); \ No newline at end of file +$I->deleteFile('./tests/tmp/TestingModel.php'); diff --git a/lumen-test/tests/acceptance/PivotTableCommandCept.php b/lumen-test/tests/acceptance/PivotTableCommandCept.php index 37e58ab..a9c0351 100644 --- a/lumen-test/tests/acceptance/PivotTableCommandCept.php +++ b/lumen-test/tests/acceptance/PivotTableCommandCept.php @@ -2,7 +2,7 @@ $I = new AcceptanceTester($scenario); $I->wantTo('generate a pivot table'); -$I->runShellCommand('php artisan wn:pivot-table Tag Project --file=pivot_table'); +$I->runShellCommand('php artisan wn:pivot-table Tag Project --add=timestamps --file=pivot_table'); $I->seeInShellOutput('project_tag migration generated'); $I->seeFileFound('./database/migrations/pivot_table.php'); $I->openFile('./database/migrations/pivot_table.php'); @@ -36,4 +36,4 @@ class CreateProjectTagTable extends Migration } } '); -$I->deleteFile('./database/migrations/pivot_table.php'); \ No newline at end of file +$I->deleteFile('./database/migrations/pivot_table.php'); diff --git a/lumen-test/tests/acceptance/ResourceCommandCept.php b/lumen-test/tests/acceptance/ResourceCommandCept.php index ee13d65..af4ec89 100644 --- a/lumen-test/tests/acceptance/ResourceCommandCept.php +++ b/lumen-test/tests/acceptance/ResourceCommandCept.php @@ -2,7 +2,7 @@ $I = new AcceptanceTester($scenario); $I->wantTo('generate a RESTful resource'); -$I->runShellCommand('php artisan wn:resource task_category "name;string:unique;requied;fillable;word descr;text:nullable;;fillable;paragraph due;timestamp;;fillable,date;date" --has-many="tags,tasks" --belongs-to="project,creator:User" --migration-file=create_task_categories'); +$I->runShellCommand('php artisan wn:resource task_category "name;string:unique;requied;fillable;word descr;text:nullable;;fillable;paragraph due;timestamp;;fillable,date;date" --has-many="tags,tasks" --belongs-to="project,creator:User" --add=timestamps --migration-file=create_task_categories'); // Checking the model $I->seeInShellOutput('TaskCategory model generated'); @@ -157,4 +157,4 @@ $I->writeToFile('./database/factories/ModelFactory.php', "create(); // } // }'); -// $I->deleteFile('./database/seeds/TaskCategoriesTableSeeder.php'); \ No newline at end of file +// $I->deleteFile('./database/seeds/TaskCategoriesTableSeeder.php'); diff --git a/src/Commands/MigrationCommand.php b/src/Commands/MigrationCommand.php index d04b54c..cb5b23f 100644 --- a/src/Commands/MigrationCommand.php +++ b/src/Commands/MigrationCommand.php @@ -6,7 +6,7 @@ class MigrationCommand extends BaseCommand { protected $signature = 'wn:migration {table : The table name.} {--schema= : the schema.} - {--add= : specifies additional columns like softDeletes, rememberToken and nullableTimestamps.} + {--add= : specifies additional columns like timestamps, softDeletes, rememberToken and nullableTimestamps.} {--keys= : foreign keys.} {--file= : name of the migration file (to use only for testing purpose).} {--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} @@ -64,7 +64,7 @@ class MigrationCommand extends BaseCommand { { $additionals = $this->option('add'); if (empty($additionals)) { - $additionals = 'timestamps'; + return ''; } $additionals = explode(',', $additionals); @@ -146,4 +146,4 @@ class MigrationCommand extends BaseCommand { return $constraint . ';'; } -} \ No newline at end of file +} diff --git a/src/Commands/ModelCommand.php b/src/Commands/ModelCommand.php index 021b39b..29895e4 100644 --- a/src/Commands/ModelCommand.php +++ b/src/Commands/ModelCommand.php @@ -12,6 +12,7 @@ class ModelCommand extends BaseCommand { {--belongs-to= : belongsTo relationships.} {--belongs-to-many= : belongsToMany relationships.} {--rules= : fields validation rules.} + {--timestamps=true : enables timestamps on the model.} {--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} {--force= : override the existing files} @@ -31,7 +32,8 @@ class ModelCommand extends BaseCommand { 'fillable' => $this->getAsArrayFields('fillable'), 'dates' => $this->getAsArrayFields('dates'), 'relations' => $this->getRelations(), - 'rules' => $this->getRules() + 'rules' => $this->getRules(), + 'additional' => $this->getAdditional() ]) ->get(); @@ -114,4 +116,11 @@ class ModelCommand extends BaseCommand { return implode(PHP_EOL, $rules); } -} \ No newline at end of file + protected function getAdditional() + { + return $this->option('timestamps') == 'false' + ? " public \$timestamps = false;" . PHP_EOL . PHP_EOL + : ''; + } + +} diff --git a/src/Commands/PivotTableCommand.php b/src/Commands/PivotTableCommand.php index 6fc8ad4..b614e02 100644 --- a/src/Commands/PivotTableCommand.php +++ b/src/Commands/PivotTableCommand.php @@ -6,6 +6,7 @@ class PivotTableCommand extends BaseCommand { protected $signature = 'wn:pivot-table {model1 : Name of the first model or table} {model2 : Name of the second model or table} + {--add= : specifies additional columns like timestamps, softDeletes, rememberToken and nullableTimestamps.} {--file= : name of the migration file (to use only for testing purpose).} {--force= : override the existing files} '; @@ -24,7 +25,8 @@ class PivotTableCommand extends BaseCommand { '--keys' => $this->keys(), '--file' => $this->option('file'), '--parsed' => false, - '--force' => $this->option('force') + '--force' => $this->option('force'), + '--add' => $this->option('add') ]); } @@ -51,4 +53,4 @@ class PivotTableCommand extends BaseCommand { }, $this->tables)); } -} \ No newline at end of file +} diff --git a/src/Commands/ResourceCommand.php b/src/Commands/ResourceCommand.php index 904c5eb..12b0a04 100644 --- a/src/Commands/ResourceCommand.php +++ b/src/Commands/ResourceCommand.php @@ -11,6 +11,7 @@ class ResourceCommand extends BaseCommand { {--belongs-to= : belongsTo relationships.} {--belongs-to-many= : belongsToMany relationships.} {--migration-file= : the migration file name.} + {--add= : specifies additional columns like timestamps, softDeletes, rememberToken and nullableTimestamps.} {--path=app : where to store the model 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} {--force= : override the existing files} @@ -40,6 +41,7 @@ class ResourceCommand extends BaseCommand { '--rules' => $this->rules(), '--path' => $this->option('path'), '--force' => $this->option('force'), + '--timestamps' => $this->hasTimestamps() ? 'true' : 'false', '--parsed' => true ]); @@ -50,6 +52,7 @@ class ResourceCommand extends BaseCommand { '--keys' => $this->migrationKeys(), '--file' => $this->option('migration-file'), '--force' => $this->option('force'), + '--add' => $this->option('add'), '--parsed' => true ]); @@ -180,4 +183,12 @@ class ResourceCommand extends BaseCommand { })); } + protected function hasTimestamps() + { + $additionals = explode(',', $this->option('add')); + return in_array('nullableTimestamps', $additionals) + || in_array('timestamps', $additionals) + || in_array('timestampsTz', $additionals); + } + } diff --git a/templates/model.wnt b/templates/model.wnt index 58a4369..f0cfd3c 100644 --- a/templates/model.wnt +++ b/templates/model.wnt @@ -12,6 +12,6 @@ class {{name}} extends Model { {{rules}} ]; -{{relations}} +{{additional}}{{relations}} }