Skip to content

Commit

Permalink
Cleaning up migration generator command
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffreyWay committed Oct 1, 2014
1 parent 72fcd7c commit 5146b56
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 60 deletions.
68 changes: 8 additions & 60 deletions src/Way/Generators/Commands/MigrationGeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@

use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Way\Generators\Parsers\MigrationNameParser;
use Way\Generators\Parsers\MigrationFieldsParser;
use Way\Generators\Templates\Data\Migration as MigrationData;
use Way\Generators\Generator;
use Way\Generators\SchemaCreator;
use Config;

class MigrationGeneratorCommand extends GeneratorCommand {

Expand All @@ -25,58 +22,21 @@ class MigrationGeneratorCommand extends GeneratorCommand {
protected $description = 'Generate a new migration';

/**
* @var \Way\Generators\ModelGenerator
*/
protected $generator;

/**
* @var MigrationNameParser
*/
private $migrationNameParser;

/**
* @var SchemaWriter
*/
private $schemaCreator;

/**
* @param Generator $generator
* @param MigrationNameParser $migrationNameParser
* @param MigrationFieldsParser $migrationFieldsParser
* @param SchemaCreator $schemaCreator
*/
public function __construct(
Generator $generator,
MigrationNameParser $migrationNameParser,
MigrationFieldsParser $migrationFieldsParser,
SchemaCreator $schemaCreator
)
{
$this->generator = $generator;
$this->migrationNameParser = $migrationNameParser;
$this->migrationFieldsParser = $migrationFieldsParser;
$this->schemaCreator = $schemaCreator;

parent::__construct($generator);
}

/**
* Execute the console command
* Execute the console command.
*/
public function fire()
{
parent::fire();

// Now that the file has been generated,
// let's run dump-autoload to refresh everything
// We'll run dump-autoload to refresh everything.
if ( ! $this->option('testing'))
{
$this->call('dump-autoload');
}
}

/**
* The path where the file will be created
* The path to where the file will be created.
*
* @return mixed
*/
Expand All @@ -89,7 +49,7 @@ protected function getFileGenerationPath()
}

/**
* Get the date prefix for the migration.
* Get a date prefix for the migration.
*
* @return string
*/
Expand All @@ -99,29 +59,17 @@ protected function getDatePrefix()
}

/**
* Fetch the template data
* Fetch the template data for the migration generator.
*
* @return array
*/
protected function getTemplateData()
{
$migrationName = $this->argument('migrationName');

// This will tell us the table name and action that we'll be performing
$migrationData = $this->migrationNameParser->parse($migrationName);

// We also need to parse the migration fields, if provided
$fields = $this->migrationFieldsParser->parse($this->option('fields'));

return [
'CLASS' => ucwords(camel_case($migrationName)),
'UP' => $this->schemaCreator->up($migrationData, $fields),
'DOWN' => $this->schemaCreator->down($migrationData, $fields)
];
return (new MigrationData($this->argument('migrationName'), $this->option('fields')))->fetch();
}

/**
* Get path to template for generator
* Get the path to the generator template.
*
* @return mixed
*/
Expand Down
118 changes: 118 additions & 0 deletions src/Way/Generators/templates/Data/Migration.php
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');
}

}

0 comments on commit 5146b56

Please sign in to comment.