10
10
11
11
use nullref \core \interfaces \IHasMigrateNamespace ;
12
12
use Yii ;
13
+ use yii \base \Exception ;
13
14
use yii \base \Module ;
15
+ use yii \console \controllers \MigrateController as BaseMigrateController ;
14
16
use yii \db \Query ;
15
17
use yii \helpers \ArrayHelper ;
16
18
17
- class MigrateController extends \ yii \ console \ controllers \MigrateController
19
+ class MigrateController extends BaseMigrateController
18
20
{
19
21
/** @var null|string */
20
22
public $ moduleId = null ;
@@ -37,12 +39,16 @@ public function options($actionID)
37
39
* Set `migrationNamespaces` if it empty
38
40
* @param \yii\base\Action $action
39
41
* @return bool
42
+ * @throws Exception
40
43
*/
41
44
public function beforeAction ($ action )
42
45
{
43
46
if (count ($ this ->migrationNamespaces ) === 0 ) {
44
47
if ($ this ->moduleId ) {
45
48
$ 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
+ }
46
52
} else {
47
53
/** @var Module[] $modules */
48
54
$ modules = Yii::$ app ->getModules ();
@@ -87,6 +93,34 @@ public function getMigrationNamespace($module)
87
93
return $ namespaces ;
88
94
}
89
95
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
+
90
124
/**
91
125
* Return migrations only with namespace
92
126
* @param int $limit
@@ -122,17 +156,7 @@ protected function getMigrationHistory($limit)
122
156
protected function getNewMigrations ()
123
157
{
124
158
$ 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 ' ]);
134
160
return $ migrations ;
135
161
}
136
-
137
-
138
162
}
0 commit comments