diff --git a/composer.json b/composer.json index 45e9ecd..66a22b4 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "laracasts/generators", + "name": "yolcar/generators", "description": "Extend Laravel 5's generators.", "keywords": ["laravel", "generators"], "license": "MIT", diff --git a/src/Commands/PivotMigrationMakeCommand.php b/src/Commands/PivotMigrationMakeCommand.php index 9f2e4c4..e02a78e 100644 --- a/src/Commands/PivotMigrationMakeCommand.php +++ b/src/Commands/PivotMigrationMakeCommand.php @@ -3,6 +3,9 @@ namespace Laracasts\Generators\Commands; use Illuminate\Console\GeneratorCommand; +use Laracasts\Generators\Migrations\NameParser; +use Laracasts\Generators\Migrations\SchemaParser; +use Laracasts\Generators\Migrations\SyntaxBuilder; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputOption; @@ -29,6 +32,13 @@ class PivotMigrationMakeCommand extends GeneratorCommand */ protected $type = 'Migration'; + /** + * Meta information for the requested migration. + * + * @var array + */ + protected $meta; + /** * Get the desired class name from the input. * @@ -59,7 +69,11 @@ protected function parseName($name) */ protected function getStub() { - return __DIR__ . '/../stubs/pivot.stub'; + if ($this->option('schema')) { + return __DIR__ . '/../stubs/pivot-schema.stub'; + }else{ + return __DIR__ . '/../stubs/pivot.stub'; + } } /** @@ -71,7 +85,7 @@ protected function getStub() protected function getPath($name = null) { return base_path() . '/database/migrations/' . date('Y_m_d_His') . - '_create_' . $this->getPivotTableName() . '_pivot_table.php'; + '_create_' . $this->getPivotTableName() . '_pivot_table.php'; } /** @@ -83,7 +97,6 @@ protected function getPath($name = null) protected function buildClass($name = null) { $stub = $this->files->get($this->getStub()); - return $this->replacePivotTableName($stub) ->replaceSchema($stub) ->replaceClass($stub, $name); @@ -118,9 +131,14 @@ protected function replaceSchema(&$stub) $stub ); + if ($schema = $this->option('schema')) { + $schema = (new SchemaParser)->parse($schema); + $stub = (new SyntaxBuilder)->createPivotSchema($schema,$stub); + } + return $this; } - + /** * Replace the class name for the given stub. * @@ -131,7 +149,6 @@ protected function replaceSchema(&$stub) protected function replaceClass($stub, $name) { $stub = str_replace('{{class}}', $name, $stub); - return $stub; } @@ -174,4 +191,16 @@ protected function getArguments() ['tableTwo', InputArgument::REQUIRED, 'The name of the second table.'] ]; } + + /** + * Get the console command options. + * + * @return array + */ + protected function getOptions() + { + return [ + ['schema', 's', InputOption::VALUE_OPTIONAL, 'Optional schema to be attached to the migration', null] + ]; + } } diff --git a/src/Migrations/SyntaxBuilder.php b/src/Migrations/SyntaxBuilder.php index f820101..462072f 100644 --- a/src/Migrations/SyntaxBuilder.php +++ b/src/Migrations/SyntaxBuilder.php @@ -28,6 +28,12 @@ public function create($schema, $meta) return compact('up', 'down'); } + public function createPivotSchema($schema, $stub) + { + $up = $this->createSchemaForPivot($schema,$stub); + return compact('up'); + } + /** * Create the schema for the "up" method. * @@ -58,6 +64,16 @@ private function createSchemaForUpMethod($schema, $meta) throw new GeneratorException; } + private function createSchemaForPivot($schema,$stub) + { + $fields = $this->constructSchema($schema); + + return $this->insert($fields)->into($stub); + + // Otherwise, we have no idea how to proceed. + throw new GeneratorException; + } + /** * Construct the syntax for a down field. * diff --git a/src/stubs/pivot-schema.stub b/src/stubs/pivot-schema.stub new file mode 100644 index 0000000..e6f81cc --- /dev/null +++ b/src/stubs/pivot-schema.stub @@ -0,0 +1,34 @@ +integer('{{columnOne}}_id')->unsigned()->index(); + $table->foreign('{{columnOne}}_id')->references('id')->on('{{tableOne}}')->onDelete('cascade'); + $table->integer('{{columnTwo}}_id')->unsigned()->index(); + $table->foreign('{{columnTwo}}_id')->references('id')->on('{{tableTwo}}')->onDelete('cascade'); + {{schema_up}} + $table->primary(['{{columnOne}}_id', '{{columnTwo}}_id']); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::drop('{{pivotTableName}}'); + } +}