22
33namespace N3XT0R \MigrationGenerator \Console \Commands ;
44
5+ use Illuminate \Console \Command ;
56use Illuminate \Console \ConfirmableTrait ;
67use Illuminate \Database \Console \Migrations \MigrateMakeCommand ;
78use Illuminate \Database \Migrations \MigrationCreator ;
89use Illuminate \Database \Migrations \Migrator ;
910use Illuminate \Support \Composer ;
11+ use Illuminate \Support \Facades \Config ;
1012use N3XT0R \MigrationGenerator \Service \Generator \DTO \MigrationTimingDto ;
1113use N3XT0R \MigrationGenerator \Service \Generator \MigrationGenerator ;
1214use N3XT0R \MigrationGenerator \Service \Generator \MigrationGeneratorInterface ;
15+ use N3XT0R \MigrationGenerator \Service \Generator \Normalization \SchemaNormalizationManager ;
16+ use N3XT0R \MigrationGenerator \Service \Generator \Normalization \SchemaNormalizationManagerInterface ;
1317use N3XT0R \MigrationGenerator \Service \Parser \SchemaParserInterface ;
1418
1519class MigrationGeneratorCommand extends MigrateMakeCommand
@@ -22,7 +26,7 @@ class MigrationGeneratorCommand extends MigrateMakeCommand
2226 *
2327 * @var string
2428 */
25- protected $ signature = 'migrate:regenerate {{--table= : specific table}}
29+ protected $ signature = 'migrate:regenerate
2630 {{--database= : The database connection to use}} ' ; //later for 1.1 : {{--force : force re-init in migrations-table}}
2731
2832 /**
@@ -41,9 +45,9 @@ class MigrationGeneratorCommand extends MigrateMakeCommand
4145
4246 /**
4347 * MigrationGeneratorCommand constructor.
44- * @param MigrationCreator $creator
45- * @param Composer $composer
46- * @param Migrator|null $migrator
48+ * @param MigrationCreator $creator
49+ * @param Composer $composer
50+ * @param Migrator|null $migrator
4751 * @throws \Illuminate\Contracts\Container\BindingResolutionException
4852 */
4953 public function __construct (MigrationCreator $ creator , Composer $ composer , Migrator $ migrator = null )
@@ -56,6 +60,7 @@ public function __construct(MigrationCreator $creator, Composer $composer, Migra
5660 $ migrator = app ()->make ('migrator ' );
5761 }
5862 $ this ->setMigrator ($ migrator );
63+ $ this ->extendSignatureWithNormalizers ();
5964 }
6065
6166 public function setMigrator (Migrator $ migrator ): void
@@ -68,13 +73,21 @@ public function getMigrator(): Migrator
6873 return $ this ->migrator ;
6974 }
7075
71- public function handle (): void
76+ public function handle (): int
7277 {
7378 if (!$ this ->confirmToProceed ()) {
74- return ;
79+ return Command::FAILURE ;
80+ }
81+
82+ $ enabled = $ this ->resolveEnabledNormalizers ();
83+ if (!$ this ->validateNormalizers ($ enabled )) {
84+ return Command::FAILURE ;
85+ }
86+
87+ if (count ($ enabled ) === 0 ) {
88+ $ enabled = null ;
7589 }
7690
77- $ table = (string ) $ this ->option ('table ' );
7891 $ force = false ;
7992 //$force = (bool)$this->option('force');
8093 $ connectionName = $ this ->option ('database ' ) ?? config ('database.default ' );
@@ -90,24 +103,15 @@ public function handle(): void
90103 ]
91104 );
92105
93- if (!empty ($ table )) {
94- $ this ->createMigrationForSingleTable ($ schemaParser , $ connectionName , $ table );
95- } else {
96- $ this ->createMigrationsForWholeSchema ($ schemaParser , $ connectionName );
97- }
106+ $ this ->createMigrationsForWholeSchema ($ schemaParser , $ connectionName , $ enabled );
98107
99- if (true === $ force ) {
100- /**
101- * @todo reinitialize migrations table
102- */
103- }
108+ return Command::SUCCESS ;
104109 }
105110
106-
107- protected function createMigrationForSingleTable (
111+ protected function createMigrationsForWholeSchema (
108112 SchemaParserInterface $ schemaParser ,
109113 string $ connectionName ,
110- string $ table
114+ ? array $ enabledNormalizer
111115 ): void {
112116 /**
113117 * @var MigrationGenerator $generator
@@ -117,33 +121,14 @@ protected function createMigrationForSingleTable(
117121 ['connectionName ' => $ connectionName ]
118122 );
119123
120- $ database = $ this ->getMigrator ()->resolveConnection ($ connectionName )->getDatabaseName ();
121- $ tables = $ schemaParser ->getTablesFromSchema (
122- $ database
123- );
124- if (!in_array ($ table , $ tables , true )) {
125- $ this ->error ('Table " ' .$ table .'" not exists in Schema " ' .$ database .'" ' );
126- } else {
127- if (true === $ generator ->generateMigrationForTable ($ database , $ table )) {
128- /**
129- * @todo
130- */
131- } else {
132- $ this ->error ('there occurred an error by creating migration for ' .$ table );
133- $ this ->error (implode (', ' , $ generator ->getErrorMessages ()));
134- }
135- }
136- }
137-
138- protected function createMigrationsForWholeSchema (SchemaParserInterface $ schemaParser , string $ connectionName ): void
139- {
140124 /**
141- * @var MigrationGenerator $generator
125+ * @var SchemaNormalizationManager $normalizer
142126 */
143- $ generator = $ this ->getLaravel ()->make (
144- MigrationGeneratorInterface ::class,
145- ['connectionName ' => $ connectionName ]
127+ $ normalizer = $ this ->getLaravel ()->make (
128+ SchemaNormalizationManagerInterface ::class,
129+ ['enabled ' => $ enabledNormalizer ]
146130 );
131+ $ generator ->setNormalizationManager ($ normalizer );
147132
148133 $ database = $ this ->getMigrator ()->resolveConnection ($ connectionName )->getDatabaseName ();
149134 $ tables = $ schemaParser ->getSortedTablesFromSchema (
@@ -162,7 +147,7 @@ protected function createMigrationsForWholeSchema(SchemaParserInterface $schemaP
162147 if (true === $ generator ->generateMigrationForTable ($ database , $ table , $ migrationTimingDto )) {
163148 $ bar ->advance ();
164149 } else {
165- $ this ->error ('there occurred an error by creating migration for ' . $ table );
150+ $ this ->error ('there occurred an error by creating migration for ' . $ table );
166151 $ this ->error (implode (', ' , $ generator ->getErrorMessages ()));
167152 break ;
168153 }
@@ -176,7 +161,7 @@ protected function createMigrationsForWholeSchema(SchemaParserInterface $schemaP
176161 /**
177162 * Prepare the migration database for running.
178163 *
179- * @param string|null $database
164+ * @param string|null $database
180165 * @return void
181166 */
182167 protected function prepareDatabase (string $ database = null ): void
@@ -191,4 +176,36 @@ protected function prepareDatabase(string $database = null): void
191176 );
192177 }
193178 }
179+
180+ protected function extendSignatureWithNormalizers (): void
181+ {
182+ $ normalizers = array_keys (Config::get ('migration-generator.normalizer ' , []));
183+
184+ if (!empty ($ normalizers )) {
185+ $ choices = implode (', ' , $ normalizers );
186+ $ this ->signature .= ' {--normalizer=* : Enabled normalizers (available: ' . $ choices . ')} ' ;
187+ }
188+ }
189+
190+ protected function resolveEnabledNormalizers (): array
191+ {
192+ $ input = $ this ->option ('normalizer ' );
193+ $ config = config ('migration-generator.config.defaults.normalizer ' , []);
194+
195+ return !empty ($ input ) ? (array )$ input : (array )$ config ;
196+ }
197+
198+ protected function validateNormalizers (array $ enabled ): bool
199+ {
200+ $ result = true ;
201+ $ available = array_keys (config ('migration-generator.normalizer ' , []));
202+ $ invalid = array_diff ($ enabled , $ available );
203+
204+ if (!empty ($ invalid )) {
205+ $ this ->error ('Invalid normalizer(s): ' . implode (', ' , $ invalid ));
206+ $ result = false ;
207+ }
208+
209+ return $ result ;
210+ }
194211}
0 commit comments