Skip to content

Commit b392d1b

Browse files
committed
improve MigrateController
1 parent d5ad625 commit b392d1b

File tree

1 file changed

+36
-12
lines changed

1 file changed

+36
-12
lines changed

src/console/MigrateController.php

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010

1111
use nullref\core\interfaces\IHasMigrateNamespace;
1212
use Yii;
13+
use yii\base\Exception;
1314
use yii\base\Module;
15+
use yii\console\controllers\MigrateController as BaseMigrateController;
1416
use yii\db\Query;
1517
use yii\helpers\ArrayHelper;
1618

17-
class MigrateController extends \yii\console\controllers\MigrateController
19+
class MigrateController extends BaseMigrateController
1820
{
1921
/** @var null|string */
2022
public $moduleId = null;
@@ -37,12 +39,16 @@ public function options($actionID)
3739
* Set `migrationNamespaces` if it empty
3840
* @param \yii\base\Action $action
3941
* @return bool
42+
* @throws Exception
4043
*/
4144
public function beforeAction($action)
4245
{
4346
if (count($this->migrationNamespaces) === 0) {
4447
if ($this->moduleId) {
4548
$namespaces = $this->getMigrationNamespace(Yii::$app->getModule($this->moduleId));
49+
if (count($namespaces) === 0) {
50+
throw new Exception('Can\'t find any one migration namespace');
51+
}
4652
} else {
4753
/** @var Module[] $modules */
4854
$modules = Yii::$app->getModules();
@@ -87,6 +93,34 @@ public function getMigrationNamespace($module)
8793
return $namespaces;
8894
}
8995

96+
/**
97+
* @param string $name
98+
*/
99+
public function actionCreate($name)
100+
{
101+
if (!$this->nameHasNamespace($name)) {
102+
$migrationNamespaces = $this->migrationNamespaces;
103+
$namespace = array_shift($migrationNamespaces);
104+
$name = $namespace . '\\' . $name;
105+
}
106+
parent::actionCreate($name);
107+
}
108+
109+
/**
110+
* @param $name
111+
* @return bool
112+
*/
113+
protected function nameHasNamespace($name)
114+
{
115+
$namespaces = $this->migrationNamespaces;
116+
foreach ($namespaces as $namespace) {
117+
if (strpos($name, $namespace) === 0) {
118+
return true;
119+
}
120+
}
121+
return false;
122+
}
123+
90124
/**
91125
* Return migrations only with namespace
92126
* @param int $limit
@@ -122,17 +156,7 @@ protected function getMigrationHistory($limit)
122156
protected function getNewMigrations()
123157
{
124158
$migrations = parent::getNewMigrations();
125-
$namespaces = $this->migrationNamespaces;
126-
$migrations = array_filter($migrations, function ($migration) use ($namespaces) {
127-
foreach ($namespaces as $namespace) {
128-
if (strpos($migration, $namespace) === 0) {
129-
return true;
130-
}
131-
}
132-
return false;
133-
});
159+
$migrations = array_filter($migrations, [$this, 'nameHasNamespace']);
134160
return $migrations;
135161
}
136-
137-
138162
}

0 commit comments

Comments
 (0)