@@ -26,27 +26,55 @@ class CreateMigration extends CreateClassHelper {
2626 * @var DatabaseMigrationWriter
2727 */
2828 private $ writer ;
29+ private $ isConfigured ;
2930 /**
3031 * Creates new instance of the class.
3132 *
3233 * @param CreateCommand $command A command that is used to call the class.
3334 */
3435 public function __construct (CreateCommand $ command ) {
3536 $ ns = APP_DIR .'\\database \\migrations ' ;
36-
37+ $ this -> isConfigured = false ;
3738 if (!$ command ->isArgProvided ('--defaults ' )) {
3839 $ ns = CLIUtils::readNamespace ($ command , $ ns , 'Migration namespace: ' );
3940 }
4041
41- $ runner = new MigrationsRunner (ROOT_PATH .DS . str_replace ('\\' , DS , $ ns ), $ ns , null );
42- parent ::__construct ($ command , new DatabaseMigrationWriter ($ runner ));
43- $ this ->writer = $ this ->getWriter ();
44- $ this ->setNamespace ($ ns );
45-
46- if (!$ command ->isArgProvided ('--defaults ' )) {
47- $ this ->setClassName ($ command ->readClassName ('Provide an optional name for the class that will have migration logic: ' , null ));
48- $ this ->readClassInfo ();
42+ $ runner = $ this ->initRunner ($ ns , $ command );
43+ if ($ runner === null ) {
44+ $ command ->error ("Unable to set migrations path. " );
45+ } else {
46+ parent ::__construct ($ command , new DatabaseMigrationWriter ($ runner ));
47+ $ this ->writer = $ this ->getWriter ();
48+ $ this ->setNamespace ($ ns );
49+
50+ if (!$ command ->isArgProvided ('--defaults ' )) {
51+ $ this ->setClassName ($ command ->readClassName ('Provide an optional name for the class that will have migration logic: ' , null ));
52+ $ this ->readClassInfo ();
53+ $ this ->isConfigured = true ;
54+ }
55+ }
56+ }
57+ public function isConfigured () : bool {
58+ return $ this ->isConfigured ;
59+ }
60+ private function initRunner ($ ns , $ command ) {
61+ $ path = ROOT_PATH .DS . str_replace ('\\' , DS , $ ns );
62+ if (!is_dir ($ path )) {
63+ $ command ->warning ("The path ' $ path' does not exist. " );
64+ $ create = $ command ->confirm ("Would you like to create it? " , true );
65+
66+ if ($ create ) {
67+ if (!mkdir ($ path )) {
68+ $ command ->error ("Unable to create directory. " );
69+ return null ;
70+ }
71+ } else {
72+ return null ;
73+ }
74+ } else {
75+ return null ;
4976 }
77+ return new MigrationsRunner ($ path , $ ns , null );
5078 }
5179
5280 private function readClassInfo () {
0 commit comments