Browse Source

`MigrateController` refactored avoiding internal state changing

tags/2.0.8
Klimov Paul 9 years ago
parent
commit
a90ffecdb3
  1. 50
      framework/console/controllers/MigrateController.php

50
framework/console/controllers/MigrateController.php

@ -96,12 +96,6 @@ class MigrateController extends BaseMigrateController
*/ */
public $db = 'db'; public $db = 'db';
/**
* @var array columns which have a foreign key and their related table.
* @since 2.0.8
*/
protected $foreignKeys = [];
/** /**
* @inheritdoc * @inheritdoc
@ -226,7 +220,9 @@ class MigrateController extends BaseMigrateController
*/ */
protected function generateMigrationSourceCode($params) protected function generateMigrationSourceCode($params)
{ {
$this->parseFields(); $parsedFields = $this->parseFields();
$fields = $parsedFields['fields'];
$foreignKeys = $parsedFields['foreignKeys'];
$name = $params['name']; $name = $params['name'];
@ -237,7 +233,7 @@ class MigrateController extends BaseMigrateController
$firstTable = mb_strtolower($matches[1], Yii::$app->charset); $firstTable = mb_strtolower($matches[1], Yii::$app->charset);
$secondTable = mb_strtolower($matches[2], Yii::$app->charset); $secondTable = mb_strtolower($matches[2], Yii::$app->charset);
$this->fields = array_merge( $fields = array_merge(
[ [
[ [
'property' => $firstTable . '_id', 'property' => $firstTable . '_id',
@ -248,7 +244,7 @@ class MigrateController extends BaseMigrateController
'decorators' => 'integer()', 'decorators' => 'integer()',
], ],
], ],
$this->fields, $fields,
[ [
[ [
'property' => 'PRIMARY KEY(' . 'property' => 'PRIMARY KEY(' .
@ -258,8 +254,8 @@ class MigrateController extends BaseMigrateController
] ]
); );
$this->foreignKeys[$firstTable . '_id'] = $firstTable; $foreignKeys[$firstTable . '_id'] = $firstTable;
$this->foreignKeys[$secondTable . '_id'] = $secondTable; $foreignKeys[$secondTable . '_id'] = $secondTable;
$table = $firstTable . '_' . $secondTable; $table = $firstTable . '_' . $secondTable;
} elseif (preg_match('/^add_(.+)_to_(.+)$/', $name, $matches)) { } elseif (preg_match('/^add_(.+)_to_(.+)$/', $name, $matches)) {
$templateFile = $this->generatorTemplateFiles['add_column']; $templateFile = $this->generatorTemplateFiles['add_column'];
@ -268,28 +264,36 @@ class MigrateController extends BaseMigrateController
$templateFile = $this->generatorTemplateFiles['drop_column']; $templateFile = $this->generatorTemplateFiles['drop_column'];
$table = mb_strtolower($matches[2], Yii::$app->charset); $table = mb_strtolower($matches[2], Yii::$app->charset);
} elseif (preg_match('/^create_(.+)$/', $name, $matches)) { } elseif (preg_match('/^create_(.+)$/', $name, $matches)) {
$this->addDefaultPrimaryKey(); $this->addDefaultPrimaryKey($fields);
$templateFile = $this->generatorTemplateFiles['create_table']; $templateFile = $this->generatorTemplateFiles['create_table'];
$table = mb_strtolower($matches[1], Yii::$app->charset); $table = mb_strtolower($matches[1], Yii::$app->charset);
} elseif (preg_match('/^drop_(.+)$/', $name, $matches)) { } elseif (preg_match('/^drop_(.+)$/', $name, $matches)) {
$this->addDefaultPrimaryKey(); $this->addDefaultPrimaryKey($fields);
$templateFile = $this->generatorTemplateFiles['drop_table']; $templateFile = $this->generatorTemplateFiles['drop_table'];
$table = mb_strtolower($matches[1], Yii::$app->charset); $table = mb_strtolower($matches[1], Yii::$app->charset);
} }
return $this->renderFile(Yii::getAlias($templateFile), array_merge($params, [ return $this->renderFile(Yii::getAlias($templateFile), array_merge($params, [
'table' => $table, 'table' => $table,
'fields' => $this->fields, 'fields' => $fields,
'foreignKeys' => $this->foreignKeys, 'foreignKeys' => $foreignKeys,
])); ]));
} }
/** /**
* Parse the command line migration fields * Parse the command line migration fields
* @return array parse result with following fields:
*
* - fields: array, parsed fields
* - foreignKeys: array, detected foreign keys
*
* @since 2.0.7 * @since 2.0.7
*/ */
protected function parseFields() protected function parseFields()
{ {
$fields = [];
$foreignKeys = [];
foreach ($this->fields as $index => $field) { foreach ($this->fields as $index => $field) {
$chunks = preg_split('/\s?:\s?/', $field, null); $chunks = preg_split('/\s?:\s?/', $field, null);
$property = array_shift($chunks); $property = array_shift($chunks);
@ -297,7 +301,7 @@ class MigrateController extends BaseMigrateController
foreach ($chunks as $i => &$chunk) { foreach ($chunks as $i => &$chunk) {
if (strpos($chunk, 'foreignKey') === 0) { if (strpos($chunk, 'foreignKey') === 0) {
preg_match('/foreignKey\((\w*)\)/', $chunk, $matches); preg_match('/foreignKey\((\w*)\)/', $chunk, $matches);
$this->foreignKeys[$property] = isset($matches[1]) $foreignKeys[$property] = isset($matches[1])
? $matches[1] ? $matches[1]
: preg_replace('/_id$/', '', $property); : preg_replace('/_id$/', '', $property);
@ -309,24 +313,30 @@ class MigrateController extends BaseMigrateController
$chunk .= '()'; $chunk .= '()';
} }
} }
$this->fields[$index] = [ $fields[] = [
'property' => $property, 'property' => $property,
'decorators' => implode('->', $chunks), 'decorators' => implode('->', $chunks),
]; ];
} }
return [
'fields' => $fields,
'foreignKeys' => $foreignKeys,
];
} }
/** /**
* Adds default primary key to fields list if there's no primary key specified * Adds default primary key to fields list if there's no primary key specified
* @param array $fields parsed fields
* @since 2.0.7 * @since 2.0.7
*/ */
protected function addDefaultPrimaryKey() protected function addDefaultPrimaryKey(&$fields)
{ {
foreach ($this->fields as $field) { foreach ($fields as $field) {
if ($field['decorators'] === 'primaryKey()') { if ($field['decorators'] === 'primaryKey()') {
return; return;
} }
} }
array_unshift($this->fields, ['property' => 'id', 'decorators' => 'primaryKey()']); array_unshift($fields, ['property' => 'id', 'decorators' => 'primaryKey()']);
} }
} }

Loading…
Cancel
Save