Skip to content

Commit

Permalink
Add generate:pivot tableOne tableTwo
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeffrey Way committed Aug 2, 2013
1 parent a263851 commit 364afe2
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 4 deletions.
61 changes: 61 additions & 0 deletions src/Way/Generators/Commands/PivotGeneratorCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php namespace Way\Generators\Commands;

use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;

class PivotGeneratorCommand extends BaseGeneratorCommand {

/**
* The console command name.
*
* @var string
*/
protected $name = 'generate:pivot';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Generate a pivot table';

public function fire()
{
$tables = $this->sortDesiredTables();

$this->call(
'generate:migration',
array(
'name' => "create_{$tables[0]}_{$tables[1]}_table",
'--fields' => "{$tables[0]}_id:integer, {$tables[1]}_id:integer"
)
);
}

public function sortDesiredTables()
{
$tableOne = str_singular($this->argument('tableOne'));
$tableTwo = str_singular($this->argument('tableTwo'));

$tables = array($tableOne, $tableTwo);
sort($tables);

return $tables;
}

/**
* Get the console command arguments.
*
* @return array
*/
protected function getArguments()
{
return array(
array('tableOne', InputArgument::REQUIRED, 'Name of the first table.'),
array('tableTwo', InputArgument::REQUIRED, 'Name of the second table.')
);
}

}

23 changes: 19 additions & 4 deletions src/Way/Generators/GeneratorsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ public function register()
$this->registerScaffoldGenerator();
$this->registerViewGenerator();
$this->registerMigrationGenerator();
$this->registerPivotGenerator();
$this->registerSeedGenerator();
$this->registerFormDumper();
$this->registerFormDumper();

$this->commands(
'generate.model',
Expand All @@ -40,7 +41,8 @@ public function register()
'generate.view',
'generate.migration',
'generate.seed',
'generate.form'
'generate.form',
'generate.pivot'
);
}

Expand Down Expand Up @@ -157,7 +159,20 @@ protected function registerMigrationGenerator()
}

/**
* Register generate:migration
* Register generate:pivot
*
* @return Commands\PivotGeneratorCommand
*/
protected function registerPivotGenerator()
{
$this->app['generate.pivot'] = $this->app->share(function($app)
{
return new Commands\PivotGeneratorCommand;
});
}

/**
* Register generate:seed
*
* @return Commands\MigrationGeneratorCommand
*/
Expand Down Expand Up @@ -187,4 +202,4 @@ protected function registerFormDumper()
});
}

}
}

0 comments on commit 364afe2

Please sign in to comment.