diff --git a/.gitignore b/.gitignore index 7beb027..c669ab9 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,7 @@ /about/ *.sublime-* -tests/_output/* \ No newline at end of file +tests/_output/* + +lumen-test/app/ +lumen-test/database/ \ No newline at end of file diff --git a/formats/fields.json b/formats/fields.json index 1ece462..eeb8f11 100644 --- a/formats/fields.json +++ b/formats/fields.json @@ -28,7 +28,12 @@ } }, "rules", - "tags[]" + "tags[]", + { + "name": "factory", + "type": "string", + "default": false + } ] } } \ No newline at end of file diff --git a/lumen-test/.gitignore b/lumen-test/.gitignore index 37201af..02b03f3 100644 --- a/lumen-test/.gitignore +++ b/lumen-test/.gitignore @@ -3,4 +3,7 @@ tests/_output/* -composer.lock \ No newline at end of file +composer.lock + +lumen-test/app +lumen-test/database \ No newline at end of file diff --git a/lumen-test/app/Console/Commands/.gitkeep b/lumen-test/app/Console/Commands/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/lumen-test/app/Console/Kernel.php b/lumen-test/app/Console/Kernel.php deleted file mode 100644 index ad6e311..0000000 --- a/lumen-test/app/Console/Kernel.php +++ /dev/null @@ -1,29 +0,0 @@ -get("/", function () use ($app) { - return $app->welcome(); -}); diff --git a/lumen-test/app/Jobs/Job.php b/lumen-test/app/Jobs/Job.php deleted file mode 100644 index 2bc4975..0000000 --- a/lumen-test/app/Jobs/Job.php +++ /dev/null @@ -1,25 +0,0 @@ -app->register('Wn\Generators\CommandsServiceProvider'); - } -} diff --git a/lumen-test/app/Providers/EventServiceProvider.php b/lumen-test/app/Providers/EventServiceProvider.php deleted file mode 100644 index ff72210..0000000 --- a/lumen-test/app/Providers/EventServiceProvider.php +++ /dev/null @@ -1,19 +0,0 @@ - [ - 'App\Listeners\EventListener', - ], - ]; -} diff --git a/lumen-test/bootstrap/app.php b/lumen-test/bootstrap/app.php index 1b0d1e1..52b393f 100644 --- a/lumen-test/bootstrap/app.php +++ b/lumen-test/bootstrap/app.php @@ -2,7 +2,7 @@ require_once __DIR__.'/../vendor/autoload.php'; -// Dotenv::load(__DIR__.'/../'); +Dotenv::load(__DIR__.'/../'); /* |-------------------------------------------------------------------------- diff --git a/lumen-test/database/factories/ModelFactory.php b/lumen-test/database/factories/ModelFactory.php deleted file mode 100644 index ae7165b..0000000 --- a/lumen-test/database/factories/ModelFactory.php +++ /dev/null @@ -1,21 +0,0 @@ -define(App\User::class, function ($faker) { - return [ - 'name' => $faker->name, - 'email' => $faker->email, - 'password' => str_random(10), - 'remember_token' => str_random(10), - ]; -}); diff --git a/lumen-test/database/migrations/.gitkeep b/lumen-test/database/migrations/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/lumen-test/tests/acceptance/ResourceCommandCept.php b/lumen-test/tests/acceptance/ResourceCommandCept.php index 034e44d..7a56935 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 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'); +$I->runShellCommand('php artisan wn:resource task_category "name;string:unique;requied;fillable;word descr;text:nullable;;fillable;paragraph project_id;integer;required;key,fillable due;timestamp;;fillable,date;date" --has-many="tags,tasks" --belongs-to="project,creator:User" --migration-file=create_task_categories'); // Checking the model $I->seeInShellOutput('TaskCategory model generated'); @@ -103,3 +103,39 @@ $app->get("/", function () use ($app) { return $app->welcome(); }); '); + +// Checking model factory +$I->openFile('./database/factories/ModelFactory.php'); +$I->seeInThisFile(" +/** + * Factory definition for model App\TaskCategory. + */ +\$factory->define(App\TaskCategory::class, function (\$faker) { + return [ + 'name' => \$faker->word, + 'descr' => \$faker->paragraph, + 'due' => \$faker->date, + ]; +});"); +$I->writeToFile('./database/factories/ModelFactory.php', "define(App\User::class, function (\$faker) { + return [ + 'name' => \$faker->name, + 'email' => \$faker->email, + 'password' => str_random(10), + 'remember_token' => str_random(10), + ]; +}); +"); \ No newline at end of file diff --git a/src/Commands/FactoryCommand.php b/src/Commands/FactoryCommand.php index 5eab985..19a2c6b 100644 --- a/src/Commands/FactoryCommand.php +++ b/src/Commands/FactoryCommand.php @@ -47,7 +47,9 @@ class FactoryCommand extends BaseCommand { $fields = $this->option('fields'); if($fields){ - $fields = $this->getArgumentParser('factory-fields')->parse($fields); + if(! $this->option('parsed')){ + $fields = $this->getArgumentParser('factory-fields')->parse($fields); + } $template = $this->getTemplate('factory/field'); foreach($fields as $field){ $content[] = $template->with($field)->get(); diff --git a/src/Commands/ResourceCommand.php b/src/Commands/ResourceCommand.php index 3e459da..d777e01 100644 --- a/src/Commands/ResourceCommand.php +++ b/src/Commands/ResourceCommand.php @@ -49,7 +49,7 @@ class ResourceCommand extends BaseCommand { '--parsed' => true ]); - // generation REST actions trait if doesn't exist + // generating REST actions trait if doesn't exist if(! $this->fs->exists('./app/Http/Controllers/RESTActions.php')){ $this->call('wn:controller:rest-actions'); } @@ -60,6 +60,13 @@ class ResourceCommand extends BaseCommand { '--no-routes' => false ]); + // generating model factory + $this->call('wn:factory', [ + 'model' => 'App\\' . $modelName, + '--fields' => $this->factoryFields(), + '--parsed' => true + ]); + } protected function parseFields() @@ -121,4 +128,16 @@ class ResourceCommand extends BaseCommand { })); } + protected function factoryFields() + { + return array_map(function($field){ + return [ + 'name' => $field['name'], + 'type' => $field['factory'] + ]; + }, array_filter($this->fields, function($field){ + return isset($field['factory']) && $field['factory']; + })); + } + } diff --git a/src/Commands/ResourcesCommand.php b/src/Commands/ResourcesCommand.php index 1045e95..318c63c 100644 --- a/src/Commands/ResourcesCommand.php +++ b/src/Commands/ResourcesCommand.php @@ -30,19 +30,21 @@ class ResourcesCommand extends BaseCommand { ]); } + $this->call('migrate'); + $this->pivotTables = array_map( 'unserialize', array_unique(array_map('serialize', $this->pivotTables)) ); - dd($this->pivotTables); - foreach ($this->pivotTables as $tables) { $this->call('wn:pivot-table', [ 'model1' => $tables[0], 'model2' => $tables[1] ]); } + + $this->call('migrate'); } protected function getResourceParams($modelName, $i) @@ -111,7 +113,13 @@ class ResourcesCommand extends BaseCommand { $rules = (isset($field['rules'])) ? trim($field['rules']) : ''; $tags = $this->convertArray($field['tags'], ' ', ','); - return "{$name};{$schema};{$rules};{$tags}"; + $string = "{$name};{$schema};{$rules};{$tags}"; + + if($field['factory']){ + $string .= ';' . $field['factory']; + } + + return $string; } protected function convertArray($list, $old, $new) diff --git a/src/Commands/SeedCommand.php b/src/Commands/SeedCommand.php deleted file mode 100644 index 17c7632..0000000 --- a/src/Commands/SeedCommand.php +++ /dev/null @@ -1,4 +0,0 @@ -argument('model'); + + $file = $this->getFile(); + + $content = $this->fs->get($file); + + $content .= $this->getTemplate('factory') + ->with([ + 'model' => $model, + 'factory_fields' => $this->getFieldsContent() + ]) + ->get(); + + $this->save($content, $file); + + $this->info("{$model} factory generated !"); + } + + protected function getFile() + { + $file = $this->option('file'); + if(! $file){ + $file = './database/factories/ModelFactory.php'; + } + return $file; + } + + protected function getFieldsContent() + { + $content = []; + + $fields = $this->option('fields'); + + if($fields){ + if(! $this->option('parsed')){ + $fields = $this->getArgumentParser('factory-fields')->parse($fields); + } + $template = $this->getTemplate('factory/field'); + foreach($fields as $field){ + $content[] = $template->with($field)->get(); + } + $content = implode(PHP_EOL, $content); + } else { + $content = "\t\t// Fields here"; + } + + return $content; + } + +} \ No newline at end of file diff --git a/src/CommandsServiceProvider.php b/src/CommandsServiceProvider.php index e2835b9..dbe07db 100644 --- a/src/CommandsServiceProvider.php +++ b/src/CommandsServiceProvider.php @@ -15,7 +15,7 @@ class CommandsServiceProvider extends ServiceProvider $this->registerResourceCommand(); $this->registerResourcesCommand(); $this->registerPivotTableCommand(); - $this->registerFactoryCommand(); + // $this->registerFactoryCommand(); // $this->registerSeedCommand(); // $this->registerTestCommand(); } diff --git a/lumen-test/database/seeds/DatabaseSeeder.php b/templates/db-seeder.wnt similarity index 86% rename from lumen-test/database/seeds/DatabaseSeeder.php rename to templates/db-seeder.wnt index fb9e600..305d76b 100644 --- a/lumen-test/database/seeds/DatabaseSeeder.php +++ b/templates/db-seeder.wnt @@ -13,9 +13,9 @@ class DatabaseSeeder extends Seeder public function run() { Model::unguard(); - - // $this->call('UserTableSeeder'); - + +{{seeders}} + Model::reguard(); } } diff --git a/templates/db-seeder/seeder.wnt b/templates/db-seeder/seeder.wnt new file mode 100644 index 0000000..192625f --- /dev/null +++ b/templates/db-seeder/seeder.wnt @@ -0,0 +1 @@ +$this->call({{name}}::class); \ No newline at end of file diff --git a/templates/seeder.wnt b/templates/seeder.wnt new file mode 100644 index 0000000..ccd9075 --- /dev/null +++ b/templates/seeder.wnt @@ -0,0 +1,11 @@ +create(); + } +}