44namespace N3XT0R \MigrationGenerator \Service \Generator \Compiler ;
55
66use Illuminate \Database \Migrations \Migration ;
7- use N3XT0R \MigrationGenerator \Service \Generator \Compiler \Mapper \MapperInterface ;
8- use Illuminate \View \Factory as ViewFactory ;
97use Illuminate \Filesystem \Filesystem ;
8+ use Illuminate \Support \Str ;
9+ use Illuminate \View \Factory as ViewFactory ;
10+ use N3XT0R \MigrationGenerator \Service \Generator \Compiler \Mapper \MapperInterface ;
1011use N3XT0R \MigrationGenerator \Service \Generator \Definition \Entity \ResultEntity ;
1112use N3XT0R \MigrationGenerator \Service \Generator \Sort \TopSort ;
12- use Illuminate \Support \Str ;
1313
1414class MigrationCompiler implements MigrationCompilerInterface
1515{
@@ -44,7 +44,7 @@ public function getMapper(): array
4444 }
4545
4646 /**
47- * @param array $mapper
47+ * @param array $mapper
4848 */
4949 public function setMapper (array $ mapper ): void
5050 {
@@ -70,7 +70,7 @@ public function getFilesystem(): Filesystem
7070 }
7171
7272 /**
73- * @param Filesystem $filesystem
73+ * @param Filesystem $filesystem
7474 */
7575 public function setFilesystem (Filesystem $ filesystem ): void
7676 {
@@ -86,7 +86,7 @@ public function getMigrationFiles(): array
8686 }
8787
8888 /**
89- * @param array $migrationFiles
89+ * @param array $migrationFiles
9090 */
9191 public function setMigrationFiles (array $ migrationFiles ): void
9292 {
@@ -114,64 +114,88 @@ public function generateByResult(ResultEntity $resultEntity, string $customMigra
114114 $ mapper = $ this ->getMapper ();
115115 $ sortedMapper = TopSort::sort ($ mapper );
116116
117- $ data = [
118- 'migrationNamespace ' => 'use ' . Migration::class . '; ' ,
119- 'tableName ' => $ tableName ,
120- 'columns ' => [],
121- ];
122-
123- if (!empty ($ customMigrationClass ) && class_exists ($ customMigrationClass )) {
124- $ data ['migrationNamespace ' ] = 'use ' . $ customMigrationClass . '; ' ;
125- }
117+ $ migrationNamespace = $ this ->resolveMigrationNamespace ($ customMigrationClass );
118+ $ migrationClass = $ this ->extractClassFromNamespace ($ migrationNamespace );
126119
127- $ namespaceParts = explode ('\\' , str_replace ('; ' , '' , $ data ['migrationNamespace ' ]));
128- $ data ['migrationClass ' ] = $ namespaceParts [count ($ namespaceParts ) - 1 ];
120+ $ columns = [];
129121
130- foreach ($ sortedMapper as $ key => $ mappingName ) {
122+ foreach ($ sortedMapper as $ mappingName ) {
131123 $ mapping = app ()->make ($ mapper [$ mappingName ]['class ' ]);
132124 if ($ mapping instanceof MapperInterface) {
133125 $ resultData = $ resultEntity ->getResultByTableNameAndKey ($ tableName , $ mappingName );
134- $ extractedLines = $ mapping ->map ($ resultData );
135- $ data ['columns ' ] = array_merge ($ data ['columns ' ], $ extractedLines );
126+ foreach ($ mapping ->map ($ resultData ) as $ line ) {
127+ $ columns [] = $ line ;
128+ }
136129 }
137130 }
138131
132+ $ data = [
133+ 'migrationNamespace ' => $ migrationNamespace ,
134+ 'migrationClass ' => $ migrationClass ,
135+ 'tableName ' => $ tableName ,
136+ 'columns ' => $ columns ,
137+ ];
138+
139139 $ this ->setRenderedTemplate ($ this ->render ('migration-generator::CreateTableStub ' , $ data ));
140140 }
141141
142+ private function resolveMigrationNamespace (string $ customClass ): string
143+ {
144+ return (!empty ($ customClass ) && class_exists ($ customClass ))
145+ ? 'use ' .$ customClass .'; '
146+ : 'use ' .Migration::class.'; ' ;
147+ }
148+
149+ private function extractClassFromNamespace (string $ namespaceLine ): string
150+ {
151+ $ parts = explode ('\\' , str_replace ('; ' , '' , $ namespaceLine ));
152+ return end ($ parts );
153+ }
154+
142155 public function writeToDisk (
143156 string $ name ,
144157 string $ path ,
145158 int $ currentAmount = -1 ,
146159 int $ maxAmount = -1 ,
147160 int $ timestamp = -1
148161 ): bool {
149- $ this ->setMigrationFiles ([]);
150162 $ result = false ;
163+ $ this ->setMigrationFiles ([]);
151164 $ tpl = $ this ->getRenderedTemplate ();
152- if (!empty ($ tpl )) {
153- $ filesystem = $ this ->getFilesystem ();
154- $ datePrefix = date ('Y_m_d_His ' );
155- if (-1 !== $ currentAmount && -1 !== $ maxAmount && -1 !== $ timestamp ) {
156- $ datePrefix = $ this ->getHourMinuteSecondPrefix ($ currentAmount , $ maxAmount , $ timestamp );
157- }
158165
159- $ fileName = $ datePrefix . '_ ' . Str::snake ($ name ) . '.php ' ;
166+ if (!empty ($ tpl )) {
167+ $ fileName = $ this ->generateFilename ($ name , $ currentAmount , $ maxAmount , $ timestamp );
160168 $ renderedTemplate = str_replace ('DummyClass ' , Str::studly ($ name ), $ tpl );
169+ $ result = $ this ->writeTemplateToFile ($ path , $ fileName , $ renderedTemplate );
170+ }
161171
162- if ($ filesystem ->exists ($ path )) {
163- $ fileLocation = $ path . DIRECTORY_SEPARATOR . $ fileName ;
164- if (false === $ filesystem ->exists ($ fileLocation )) {
165- $ result = $ filesystem ->put ($ fileLocation , $ renderedTemplate ) > 0 ;
166- if (true === $ result ) {
167- $ this ->addMigrationFile ($ fileName );
168- }
169- }
172+ return $ result ;
173+ }
174+
175+ private function generateFilename (string $ name , int $ currentAmount , int $ maxAmount , int $ timestamp ): string
176+ {
177+ $ prefix = ($ currentAmount !== -1 && $ maxAmount !== -1 && $ timestamp !== -1 )
178+ ? $ this ->getHourMinuteSecondPrefix ($ currentAmount , $ maxAmount , $ timestamp )
179+ : date ('Y_m_d_His ' );
180+
181+ return $ prefix .'_ ' .Str::snake ($ name ).'.php ' ;
182+ }
183+
184+ private function writeTemplateToFile (string $ path , string $ fileName , string $ content ): bool
185+ {
186+ $ filesystem = $ this ->getFilesystem ();
187+ $ filePath = $ path .DIRECTORY_SEPARATOR .$ fileName ;
188+ $ success = false ;
189+
190+ if ($ filesystem ->exists ($ path ) && !$ filesystem ->exists ($ filePath )) {
191+ $ success = $ filesystem ->put ($ filePath , $ content ) > 0 ;
192+
193+ if ($ success ) {
194+ $ this ->addMigrationFile ($ fileName );
170195 }
171196 }
172197
173-
174- return $ result ;
198+ return $ success ;
175199 }
176200
177201 private function getHourMinuteSecondPrefix (int $ actual , int $ max , int $ timestamp ): string
0 commit comments