wn:resources not generating pivot tables bug fixed

This commit is contained in:
Amine Ben hammou 2015-12-05 20:40:55 +01:00
parent 714d11efd8
commit b743670470
27 changed files with 166 additions and 242 deletions

5
.gitignore vendored
View File

@ -2,4 +2,7 @@
/about/
*.sublime-*
tests/_output/*
tests/_output/*
lumen-test/app/
lumen-test/database/

View File

@ -28,7 +28,12 @@
}
},
"rules",
"tags[]"
"tags[]",
{
"name": "factory",
"type": "string",
"default": false
}
]
}
}

View File

@ -3,4 +3,7 @@
tests/_output/*
composer.lock
composer.lock
lumen-test/app
lumen-test/database

View File

@ -1,29 +0,0 @@
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Laravel\Lumen\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
//
];
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
//
}
}

View File

@ -1,10 +0,0 @@
<?php
namespace App\Events;
use Illuminate\Queue\SerializesModels;
abstract class Event
{
use SerializesModels;
}

View File

@ -1,44 +0,0 @@
<?php
namespace App\Exceptions;
use Exception;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Laravel\Lumen\Exceptions\Handler as ExceptionHandler;
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that should not be reported.
*
* @var array
*/
protected $dontReport = [
HttpException::class,
];
/**
* Report or log an exception.
*
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
*
* @param \Exception $e
* @return void
*/
public function report(Exception $e)
{
return parent::report($e);
}
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $e
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $e)
{
return parent::render($request, $e);
}
}

View File

@ -1,10 +0,0 @@
<?php
namespace App\Http\Controllers;
use Laravel\Lumen\Routing\Controller as BaseController;
class Controller extends BaseController
{
//
}

View File

@ -1,20 +0,0 @@
<?php
namespace App\Http\Middleware;
use Closure;
class ExampleMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
return $next($request);
}
}

View File

@ -1,16 +0,0 @@
<?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();
});

View File

@ -1,25 +0,0 @@
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Contracts\Queue\ShouldQueue;
abstract class Job implements SelfHandling, ShouldQueue
{
/*
|--------------------------------------------------------------------------
| Queueable Jobs
|--------------------------------------------------------------------------
|
| This job base class provides a central location to place any logic that
| is shared across all of your jobs. The trait included with the class
| provides access to the "queueOn" and "delay" queue helper methods.
|
*/
use InteractsWithQueue, Queueable, SerializesModels;
}

View File

@ -1,11 +0,0 @@
<?php
namespace App\Listeners;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
abstract class Listener
{
//
}

View File

@ -1,19 +0,0 @@
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
// Adding providers for local development
$this->app->register('Wn\Generators\CommandsServiceProvider');
}
}

View File

@ -1,19 +0,0 @@
<?php
namespace App\Providers;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
class EventServiceProvider extends ServiceProvider
{
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
'App\Events\SomeEvent' => [
'App\Listeners\EventListener',
],
];
}

View File

@ -2,7 +2,7 @@
require_once __DIR__.'/../vendor/autoload.php';
// Dotenv::load(__DIR__.'/../');
Dotenv::load(__DIR__.'/../');
/*
|--------------------------------------------------------------------------

View File

@ -1,21 +0,0 @@
<?php
/*
|--------------------------------------------------------------------------
| Model Factories
|--------------------------------------------------------------------------
|
| Here you may define all of your model factories. Model factories give
| you a convenient way to create models for testing and seeding your
| database. Just tell the factory how a default model should look.
|
*/
$factory->define(App\User::class, function ($faker) {
return [
'name' => $faker->name,
'email' => $faker->email,
'password' => str_random(10),
'remember_token' => str_random(10),
];
});

View File

@ -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', "<?php
/*
|--------------------------------------------------------------------------
| Model Factories
|--------------------------------------------------------------------------
|
| Here you may define all of your model factories. Model factories give
| you a convenient way to create models for testing and seeding your
| database. Just tell the factory how a default model should look.
|
*/
\$factory->define(App\User::class, function (\$faker) {
return [
'name' => \$faker->name,
'email' => \$faker->email,
'password' => str_random(10),
'remember_token' => str_random(10),
];
});
");

View File

@ -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();

View File

@ -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'];
}));
}
}

View File

@ -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)

View File

@ -1,4 +0,0 @@
<?php
/**
* Comming Soon
*/

View File

@ -0,0 +1,64 @@
<?php namespace Wn\Generators\Commands;
class SeederCommand extends BaseCommand {
protected $signature = 'wn:seeder
{model : full qualified name of the model.}
{--cout=10 : number of elements to add in database.}
';
protected $description = 'Generates a model factory';
public function handle()
{
$model = $this->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;
}
}

View File

@ -15,7 +15,7 @@ class CommandsServiceProvider extends ServiceProvider
$this->registerResourceCommand();
$this->registerResourcesCommand();
$this->registerPivotTableCommand();
$this->registerFactoryCommand();
// $this->registerFactoryCommand();
// $this->registerSeedCommand();
// $this->registerTestCommand();
}

View File

@ -13,9 +13,9 @@ class DatabaseSeeder extends Seeder
public function run()
{
Model::unguard();
// $this->call('UserTableSeeder');
{{seeders}}
Model::reguard();
}
}

View File

@ -0,0 +1 @@
$this->call({{name}}::class);

11
templates/seeder.wnt Normal file
View File

@ -0,0 +1,11 @@
<?php
use Illuminate\Database\Seeder;
class {{name}} extends Seeder
{
public function run()
{
factory({{model}}::class, {{count}})->create();
}
}