@@ -58,69 +58,49 @@ private function createMigrationsTable() : bool {
5858 $ this ->success ("Migrations table succesfully created. " );
5959 return true ;
6060 }
61+ private function hasMigrationsTable (?MigrationsRunner $ runner ) : bool {
62+ if (!$ this ->isArgProvided ('--ini ' )) {
63+ return true ;
64+ }
65+ $ conn = $ this ->getDBConnection ($ runner );
66+ if ($ conn !== null ) {
67+ $ temp = $ runner !== null ? $ runner : new MigrationsRunner (APP_PATH , '\\' .APP_DIR , $ conn );
68+ $ temp ->createMigrationsTable ();
69+ return true ;
70+ }
71+ return false ;
72+ }
73+ private function getNS (?MigrationsRunner $ runner = null ) {
74+ if ($ this ->isArgProvided ('--ns ' )) {
75+ return $ this ->getArgValue ('--ns ' );
76+ } else if ($ runner !== null ) {
77+ return $ runner ->getMigrationsNamespace ();
78+ } else {
79+ $ this ->info ("Using default namespace for migrations. " );
80+ return '\\' .APP_DIR .'\\database \\migrations ' ;
81+ }
82+ }
83+
6184 /**
6285 * Execute the command.
6386 *
6487 * @return int 0 in case of success. Other value if failed.
6588 */
6689 public function exec () : int {
67- if ($ this ->isArgProvided ('--ini ' )) {
68- if ($ this ->getRunnerArgValidity () == 0 ) {
69- $ ns = $ this ->isArgProvided ('--ns ' ) ? $ this ->getArgValue ('--ns ' ) : '\\' .APP_DIR .'\\database \\migrations ' ;
70- if (!$ this ->hasConnections ()) {
71- return 0 ;
72- }
73- if ($ this ->isConnectionSet ()) {
74- $ this ->migrationsRunner = new MigrationsRunner (APP_PATH , '\\app ' , null );
75- if (!$ this ->createMigrationsTable ()) {
76- return -1 ;
77- }
78- } else {
79- return -2 ;
80- }
81- } else if (!$ this ->createMigrationsTable ()) {
82- return -1 ;
83- }
84- }
85- if ($ this ->getRunnerArgValidity () == 0 ) {
86- if ($ this ->isArgProvided ('--ns ' )) {
87- $ ns = $ this ->getArgValue ('--ns ' );
88- } else {
89- $ this ->info ("Using default namespace for migrations. " );
90- $ ns = '\\' .APP_DIR .'\\database \\migrations ' ;
91- }
92-
93-
94- if (!$ this ->hasMigrations ($ ns )) {
95- return 0 ;
96- }
97-
98- if (!$ this ->hasConnections ()) {
99- return 0 ;
100- }
101-
102- if ($ this ->isConnectionSet ()) {
103-
104- $ this ->migrationsRunner ->setConnectionInfo ($ this ->connectionInfo );
105- } else {
106- return -2 ;
107- }
108- } else if ($ this ->migrationsRunner === null ) {
109- return -2 ;
110- }
11190
112- if (count ($ this ->migrationsRunner ->getMigrations ()) === 0 ) {
113- $ this ->info ("No migrations where found in the namespace ' " .$ this ->migrationsRunner ->getMigrationsNamespace ()."'. " );
91+ $ runner = $ this ->getRunnerArg ();
92+ $ ns = $ this ->getNS ($ runner );
93+ $ this ->hasMigrationsTable ($ runner );
94+
95+ if (!$ this ->hasMigrations ($ ns )) {
11496 return 0 ;
11597 }
11698
117- if (!$ this ->migrationsRunner ->isConnected ()) {
118- $ err = $ this ->migrationsRunner ->getLastError ();
119- $ this ->error ($ err ['message ' ]);
120- return -1 ;
121- }
99+ $ this ->executeMigrations ($ runner );
100+ }
101+ private function executeMigrations (MigrationsRunner $ runner ) {
122102 $ listOfApplied = [];
123- while ($ this ->applyNext ($ listOfApplied )){};
103+ while ($ this ->applyNext ($ runner , $ listOfApplied )){};
124104
125105 if (count ($ listOfApplied ) != 0 ) {
126106 $ this ->info ("Number of applied migrations: " .count ($ listOfApplied ));
@@ -133,9 +113,13 @@ public function exec() : int {
133113 }
134114 return 0 ;
135115 }
136- private function isConnectionSet () : bool {
137- if ($ this ->connectionInfo !== null ) {
138- return true ;
116+ private function getDBConnection (?MigrationsRunner $ runner = null ) : ?ConnectionInfo {
117+
118+ if ($ runner !== null ) {
119+ return $ runner ->getConnectionInfo ();
120+ }
121+ if (!$ this ->hasConnections ()) {
122+ return null ;
139123 }
140124 $ dbConnections = array_keys (App::getConfig ()->getDBConnections ());
141125
@@ -144,21 +128,18 @@ private function isConnectionSet() : bool {
144128
145129 if (!in_array ($ connection , $ dbConnections )) {
146130 $ this ->error ("No connection was found which has the name ' $ connection'. " );
147- return false ;
131+ return null ;
148132 } else {
149- $ this ->connectionInfo = App::getConfig ()->getDBConnection ($ connection );
150- return true ;
133+ return App::getConfig ()->getDBConnection ($ connection );
151134 }
152135 } else {
153- $ this ->connectionInfo = CLIUtils::getConnectionName ($ this );
154- return true ;
136+ return CLIUtils::getConnectionName ($ this );
155137 }
156- return false ;
157138 }
158- private function applyNext (&$ listOfApplied ) : bool {
139+ private function applyNext (MigrationsRunner $ runner , &$ listOfApplied ) : bool {
159140 try {
160141 $ this ->println ("Executing migration... " );
161- $ applied = $ this -> migrationsRunner ->applyOne ();
142+ $ applied = $ runner ->applyOne ();
162143
163144 if ($ applied !== null ) {
164145 $ this ->success ("Migration ' " .$ applied ->getName ()."' applied successfuly. " );
@@ -174,34 +155,30 @@ private function applyNext(&$listOfApplied) : bool {
174155 return false ;
175156 }
176157 }
177- private function getRunnerArgValidity () {
158+ private function getRunnerArg () : ? MigrationsRunner {
178159 $ runner = $ this ->getArgValue ('--runner ' );
179160
180161 if ($ runner === null ) {
181- return 0 ;
162+ return null ;
182163 }
183164
184165 if (class_exists ($ runner )) {
185166 try {
186167 $ runnerInst = new $ runner ();
187168 } catch (Throwable $ exc ) {
188169 $ this ->error ('The argument --runner has invalid value: Exception: " ' .$ exc ->getMessage ().'". ' );
189- return - 1 ;
170+ return null ;
190171 }
191172
192-
193-
194173 if (!($ runnerInst instanceof MigrationsRunner)) {
195174 $ this ->error ('The argument --runner has invalid value: " ' .$ runner .'" is not an instance of "MigrationsRunner". ' );
196- return - 1 ;
175+ return null ;
197176 } else {
198- $ this ->migrationsRunner = $ runnerInst ;
199- $ this ->connectionInfo = $ runnerInst ->getConnectionInfo ();
200- return 1 ;
177+ return $ runnerInst ;
201178 }
202179 } else {
203180 $ this ->error ('The argument --runner has invalid value: Class " ' .$ runner .'" does not exist. ' );
204- return - 1 ;
181+ return null ;
205182 }
206183 }
207184 private function hasConnections () : bool {
@@ -213,14 +190,14 @@ private function hasConnections() : bool {
213190 return true ;
214191 }
215192 private function hasMigrations (string $ namespace ) : bool {
216- $ this -> migrationsRunner = new MigrationsRunner (ROOT_PATH .DS .str_replace ('\\' , DS , $ namespace ), $ namespace , null );
193+ $ tmpRunner = new MigrationsRunner (ROOT_PATH .DS .str_replace ('\\' , DS , $ namespace ), $ namespace , null );
217194 $ this ->println ("Checking namespace ' $ namespace' for migrations... " );
218- $ count = count ($ this -> migrationsRunner ->getMigrations ());
195+ $ count = count ($ tmpRunner ->getMigrations ());
219196 if ($ count == 0 ) {
220- $ this ->info ("No migrations were found in the namespace ' $ namespace'. " );
197+ $ this ->info ("No migrations found in the namespace ' $ namespace'. " );
221198 return false ;
222199 }
223- $ this ->println ("Found $ count migration(s). " );
200+ $ this ->info ("Found $ count migration(s) in the namespace ' $ namespace ' . " );
224201 return true ;
225202 }
226203}
0 commit comments