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.
Cleaning up migration generator command
- Loading branch information
1 parent
72fcd7c
commit 5146b56
Showing
2 changed files
with
126 additions
and
60 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,118 @@ | ||
<?php namespace Way\Generators\Templates\Data; | ||
|
||
use App; | ||
use Way\Generators\Parsers\MigrationFieldsParser; | ||
use Way\Generators\Parsers\MigrationNameParser; | ||
|
||
class Migration { | ||
|
||
/** | ||
* The name of the migration. | ||
* | ||
* @var string | ||
*/ | ||
private $migrationName; | ||
|
||
/** | ||
* A string representation of the migration fields. | ||
* | ||
* @var string | ||
*/ | ||
private $fields; | ||
|
||
/** | ||
* Create a new Migration template data instance. | ||
* | ||
* @param string $migrationName | ||
* @param string $fields | ||
*/ | ||
public function __construct($migrationName, $fields) | ||
{ | ||
$this->migrationName = $migrationName; | ||
$this->fields = $fields; | ||
} | ||
|
||
/** | ||
* Fetch the template data for a migration generation. | ||
* | ||
* @return array | ||
*/ | ||
public function fetch() | ||
{ | ||
$parsedName = $this->getParsedMigrationName(); | ||
$parsedFields = $this->getParsedMigrationFields(); | ||
|
||
return [ | ||
'class' => $this->getClass(), | ||
'up' => $this->getMigrationUp($parsedName, $parsedFields), | ||
'down' => $this->getMigrationDown($parsedName, $parsedFields) | ||
]; | ||
} | ||
|
||
/** | ||
* Parse the migration name. | ||
* | ||
* @return array | ||
*/ | ||
private function getParsedMigrationName() | ||
{ | ||
$nameParser = new MigrationNameParser; | ||
|
||
return $nameParser->parse($this->migrationName); | ||
} | ||
|
||
/** | ||
* Parse the migration fields. | ||
* | ||
* @return array | ||
*/ | ||
private function getParsedMigrationFields() | ||
{ | ||
$fieldParser = new MigrationFieldsParser; | ||
|
||
return $fieldParser->parse($this->fields); | ||
} | ||
|
||
/** | ||
* Get the class name for the migration. | ||
*/ | ||
private function getClass() | ||
{ | ||
return ucwords(camel_case($this->migrationName)); | ||
} | ||
|
||
/** | ||
* Get the schema for the up() method. | ||
* | ||
* @param $migrationData | ||
* @param $fields | ||
* @return mixed | ||
*/ | ||
private function getMigrationUp($migrationData, $fields) | ||
{ | ||
return $this->resolveSchemaCreator()->up($migrationData, $fields); | ||
} | ||
|
||
/** | ||
* Get the schema for the down() method. | ||
* | ||
* @param $migrationData | ||
* @param $fields | ||
* @return mixed | ||
*/ | ||
private function getMigrationDown($migrationData, $fields) | ||
{ | ||
return $this->resolveSchemaCreator()->down($migrationData, $fields); | ||
} | ||
|
||
/** | ||
* Get a SchemaCreator instance. | ||
* | ||
* @return mixed | ||
*/ | ||
private function resolveSchemaCreator() | ||
{ | ||
return App::make('Way\Generators\SchemaCreator'); | ||
} | ||
|
||
} |