forked from dahabit/Laravel-4-Generators
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Return pivot generator - closes #274
- Loading branch information
1 parent
e1d1a32
commit be05dbb
Showing
3 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php namespace Way\Generators\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
|
||
class PivotGeneratorCommand extends Command { | ||
|
||
/** | ||
* The console command name. | ||
* | ||
* @var string | ||
*/ | ||
protected $name = 'generate:pivot'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Generate a pivot table'; | ||
|
||
/** | ||
* Create a pivot table migration | ||
*/ | ||
public function fire() | ||
{ | ||
list($tableOne, $tableTwo) = $this->sortDesiredTables(); | ||
|
||
$this->call( | ||
'generate:migration', | ||
[ | ||
'migrationName' => "create_{$tableOne}_{$tableTwo}_table", | ||
'--fields' => $this->getMigrationFields($tableOne, $tableTwo) | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* Sort the provided pivot tables in alphabetical order | ||
* | ||
* @return array | ||
*/ | ||
public function sortDesiredTables() | ||
{ | ||
$tables = array_except(array_map('str_singular', $this->argument()), 'command'); | ||
|
||
sort($tables); | ||
|
||
return $tables; | ||
} | ||
|
||
/** | ||
* Get the fields for the pivot migration | ||
* | ||
* @param $tableOne | ||
* @param $tableTwo | ||
* @return array | ||
*/ | ||
public function getMigrationFields($tableOne, $tableTwo) | ||
{ | ||
return implode(', ', [ | ||
"{$tableOne}_id:integer:unsigned:index", | ||
"{$tableTwo}_id:integer:unsigned:index" | ||
]); | ||
} | ||
|
||
/** | ||
* Get the console command arguments. | ||
* | ||
* @return array | ||
*/ | ||
protected function getArguments() | ||
{ | ||
return [ | ||
['tableOne', InputArgument::REQUIRED, 'Name of the first table'], | ||
['tableTwo', InputArgument::REQUIRED, 'Name of the second table'] | ||
]; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters