99use Tempest \Console \ConsoleCommand ;
1010use Tempest \Core \PublishesFiles ;
1111use Tempest \Database \Enums \MigrationType ;
12- use Tempest \Database \Stubs \MigrationStub ;
12+ use Tempest \Database \Stubs \ObjectMigrationStub ;
13+ use Tempest \Database \Stubs \UpMigrationStub ;
1314use Tempest \Discovery \SkipDiscovery ;
1415use Tempest \Generation \ClassManipulator ;
1516use Tempest \Generation \DataObjects \StubFile ;
1617use Tempest \Generation \Exceptions \FileGenerationFailedException ;
1718use Tempest \Generation \Exceptions \FileGenerationWasAborted ;
19+ use Tempest \Support \Str ;
1820use Tempest \Validation \Rules \EndsWith ;
1921use Tempest \Validation \Rules \IsNotEmptyString ;
2022
@@ -30,21 +32,21 @@ final class MakeMigrationCommand
3032 aliases: ['migration:make ' , 'migration:create ' , 'create:migration ' ],
3133 )]
3234 public function __invoke (
33- #[ConsoleArgument(
34- description: 'The file name of the migration ' ,
35- )]
35+ #[ConsoleArgument(description: 'The file name of the migration ' )]
3636 string $ fileName ,
37- #[ConsoleArgument(
38- name: 'type ' ,
39- description: 'The type of the migration to create ' ,
40- )]
37+ #[ConsoleArgument(name: 'type ' , description: 'The type of the migration to create ' )]
4138 MigrationType $ migrationType = MigrationType::OBJECT ,
4239 ): void {
4340 try {
44- $ stubFile = $ this ->getStubFileFromMigrationType ($ migrationType );
41+ $ stub = match ($ migrationType ) {
42+ MigrationType::RAW => StubFile::from (dirname (__DIR__ ) . '/Stubs/migration.stub.sql ' ),
43+ MigrationType::OBJECT => StubFile::from (ObjectMigrationStub::class),
44+ MigrationType::UP => StubFile::from (UpMigrationStub::class),
45+ };
46+
4547 $ targetPath = match ($ migrationType ) {
46- MigrationType::RAW => $ this ->generateRawFile ($ fileName , $ stubFile ),
47- default => $ this ->generateClassFile ($ fileName , $ stubFile ),
48+ MigrationType::RAW => $ this ->generateRawFile ($ fileName , $ stub ),
49+ default => $ this ->generateClassFile ($ fileName , $ stub ),
4850 };
4951
5052 $ this ->success (sprintf ('Migration file successfully created at "%s". ' , $ targetPath ));
@@ -53,36 +55,36 @@ public function __invoke(
5355 }
5456 }
5557
56- /**
57- * Generates a raw migration file.
58- * @param string $fileName The name of the file.
59- * @param StubFile $stubFile The stub file to use.
60- *
61- * @return string The path to the generated file.
62- */
63- private function generateRawFile (
64- string $ fileName ,
65- StubFile $ stubFile ,
66- ): string {
67- $ now = date ('Y-m-d ' );
68- $ tableName = str ($ fileName )->snake ()->toString ();
69- $ suggestedPath = str ($ this ->getSuggestedPath ('Dummy ' ))
70- ->replace (
71- ['Dummy ' , '.php ' ],
72- [$ now . '_ ' . $ tableName , '.sql ' ],
73- )
58+ private function generateRawFile (string $ filename , StubFile $ stubFile ): string
59+ {
60+ $ tableName = str ($ filename )
61+ ->snake ()
62+ ->stripStart ('create ' )
63+ ->stripEnd ('table ' )
64+ ->stripStart ('_ ' )
65+ ->stripEnd ('_ ' )
7466 ->toString ();
7567
68+ $ filename = str ($ filename )
69+ ->start ('create_ ' )
70+ ->finish ('_table ' )
71+ ->toString ();
72+
73+ $ suggestedPath = Str \replace (
74+ string: $ this ->getSuggestedPath ('Dummy ' ),
75+ search: ['Dummy ' , '.php ' ],
76+ replace: [date ('Y-m-d ' ) . '_ ' . $ filename , '.sql ' ],
77+ );
78+
7679 $ targetPath = $ this ->promptTargetPath ($ suggestedPath , rules: [
7780 new IsNotEmptyString (),
7881 new EndsWith ('.sql ' ),
7982 ]);
80- $ shouldOverride = $ this ->askForOverride ($ targetPath );
8183
8284 $ this ->stubFileGenerator ->generateRawFile (
8385 stubFile: $ stubFile ,
8486 targetPath: $ targetPath ,
85- shouldOverride: $ shouldOverride ,
87+ shouldOverride: $ this -> askForOverride ( $ targetPath ) ,
8688 replacements: [
8789 'DummyTableName ' => $ tableName ,
8890 ],
@@ -91,48 +93,41 @@ private function generateRawFile(
9193 return $ targetPath ;
9294 }
9395
94- /**
95- * Generates a class migration file.
96- *
97- * @param string $fileName The name of the file.
98- * @param StubFile $stubFile The stub file to use.
99- *
100- * @return string The path to the generated file.
101- */
102- private function generateClassFile (
103- string $ fileName ,
104- StubFile $ stubFile ,
105- ): string {
106- $ suggestedPath = $ this ->getSuggestedPath ($ fileName );
107- $ targetPath = $ this ->promptTargetPath ($ suggestedPath );
108- $ shouldOverride = $ this ->askForOverride ($ targetPath );
96+ private function generateClassFile (string $ filename , StubFile $ stubFile ): string
97+ {
98+ $ tableName = str ($ filename )
99+ ->snake ()
100+ ->stripStart ('create ' )
101+ ->stripEnd ('table ' )
102+ ->stripStart ('_ ' )
103+ ->stripEnd ('_ ' )
104+ ->toString ();
105+
106+ $ filename = str ($ filename )
107+ ->afterLast (['\\' , '/ ' ])
108+ ->start ('Create ' )
109+ ->finish ('Table ' )
110+ ->when (
111+ condition: Str \contains ($ filename , ['\\' , '/ ' ]),
112+ callback: fn ($ path ) => $ path ->prepend (Str \before_last ($ filename , ['\\' , '/ ' ]), '/ ' ),
113+ )
114+ ->toString ();
115+
116+ $ targetPath = $ this ->promptTargetPath ($ this ->getSuggestedPath ($ filename ));
109117
110118 $ this ->stubFileGenerator ->generateClassFile (
111119 stubFile: $ stubFile ,
112120 targetPath: $ targetPath ,
113- shouldOverride: $ shouldOverride ,
121+ shouldOverride: $ this -> askForOverride ( $ targetPath ) ,
114122 replacements: [
115123 'dummy-date ' => date ('Y-m-d ' ),
116- 'dummy-table-name ' => str ( $ fileName )-> snake ()-> toString () ,
124+ 'dummy-table-name ' => $ tableName ,
117125 ],
118126 manipulations: [
119- fn (ClassManipulator $ class ) => $ class ->removeClassAttribute (SkipDiscovery::class),
127+ static fn (ClassManipulator $ class ) => $ class ->removeClassAttribute (SkipDiscovery::class),
120128 ],
121129 );
122130
123131 return $ targetPath ;
124132 }
125-
126- private function getStubFileFromMigrationType (MigrationType $ migrationType ): StubFile
127- {
128- try {
129- return match ($ migrationType ) {
130- MigrationType::RAW => StubFile::from (dirname (__DIR__ ) . '/Stubs/migration.stub.sql ' ),
131- MigrationType::OBJECT => StubFile::from (MigrationStub::class), // @phpstan-ignore match.alwaysTrue (Because this is a guardrail for the future implementations)
132- default => throw new InvalidArgumentException (sprintf ('The "%s" migration type has no supported stub file. ' , $ migrationType ->value )),
133- };
134- } catch (InvalidArgumentException $ invalidArgumentException ) {
135- throw new FileGenerationFailedException (sprintf ('Cannot retrieve stub file: %s ' , $ invalidArgumentException ->getMessage ()));
136- }
137- }
138133}
0 commit comments