@@ -32,19 +32,19 @@ class GenerateServerVersion extends Command {
3232 /**
3333 * @var string
3434 */
35- private $ outputDir ;
35+ private $ tempDir ;
3636
3737 /**
3838 * @var string
3939 */
40- private $ appDir ;
40+ private $ destinationDir ;
4141
4242 public function __construct ()
4343 {
4444 parent ::__construct ();
4545
46- $ this ->outputDir = config ('openapi-server-generator.output_dir ' );
47- $ this ->appDir = config ('openapi-server-generator.app_dir ' );
46+ $ this ->tempDir = config ('openapi-server-generator.temp_dir ' , config ( ' openapi-server-generator. output_dir ') );
47+ $ this ->destinationDir = config ('openapi-server-generator.destination_dir ' , config ( ' openapi-server-generator. app_dir ') );
4848 }
4949
5050 /**
@@ -57,7 +57,7 @@ public function handle()
5757 $ file = $ this ->getFile ();
5858 $ version = $ this ->getVersion ();
5959
60- $ this ->info ("Generate Dto for file: $ file, version: $ version " );
60+ $ this ->info ("Generating DTOs for file: $ file, version: $ version " );
6161
6262 $ this ->generateDto ();
6363 $ this ->copyGeneratedDtoToApp ();
@@ -73,7 +73,7 @@ private function generateDto(): void
7373 $ inputFile = $ this ->getFile ();
7474 $ invokerPackage = $ this ->getInvokerPackage ();
7575
76- $ command = "$ bin generate -i $ inputFile -g php -p 'invokerPackage= $ invokerPackage,modelPackage= $ modelPackage' -o $ this ->outputDir " ;
76+ $ command = "$ bin generate -i $ inputFile -g php -p 'invokerPackage= $ invokerPackage,modelPackage= $ modelPackage' -o $ this ->tempDir " ;
7777
7878 $ this ->info ("Execute command: $ command " );
7979
@@ -82,17 +82,17 @@ private function generateDto(): void
8282
8383 private function copyGeneratedDtoToApp (): void
8484 {
85+ $ this ->info ("Clearing destination dir: " . $ this ->getAppPathToDto ());
86+
8587 $ this ->clearAppDir ();
8688
87- $ this ->info ("Clear app dir: " . $ this ->getAppPathToDto ());
88-
89+ $ this ->info ("Copying generated DTO files to destination dir: " . $ this ->getAppPathToDto ());
90+
8991 $ this ->copyDto ();
9092
91- $ this ->info ("Copy generated dto files to app dir: " . $ this ->getAppPathToDto () );
93+ $ this ->info ("Removing temporary generated dir: " . $ this ->tempDir );
9294
9395 $ this ->removeGeneratedDto ();
94-
95- $ this ->info ("Remove generated dto dir: " . $ this ->outputDir );
9696 }
9797
9898 private function clearAppDir (): void {
@@ -102,14 +102,14 @@ private function clearAppDir(): void {
102102
103103 private function copyDto (): void
104104 {
105- shell_exec ("cp -rf $ this ->outputDir /lib/Dto " . $ this ->getAppPathToDto ());
106- shell_exec ("cp -f $ this ->outputDir /lib/Configuration.php " . $ this ->getAppPathToDto ());
107- shell_exec ("cp -n $ this ->outputDir /lib/ObjectSerializer.php " . $ this ->getAppPathToDto ());
105+ shell_exec ("cp -rf $ this ->tempDir /lib/Dto " . $ this ->getAppPathToDto ());
106+ shell_exec ("cp -f $ this ->tempDir /lib/Configuration.php " . $ this ->getAppPathToDto ());
107+ shell_exec ("cp -n $ this ->tempDir /lib/ObjectSerializer.php " . $ this ->getAppPathToDto ());
108108 }
109109
110110 private function removeGeneratedDto (): void
111111 {
112- shell_exec ("rm -rf $ this ->outputDir " );
112+ shell_exec ("rm -rf $ this ->tempDir " );
113113 }
114114
115115 private function patchModels (): void
@@ -153,20 +153,34 @@ private function getFile() {
153153 return $ this ->argument ('file ' );
154154 }
155155
156- private function getAppPathToDto () {
157- return app_path ($ this ->appDir . DIRECTORY_SEPARATOR . Str::upper ($ this ->getVersion ()));
156+ private function replaceVersionPlaceHolderInPath (string $ pathWithPossiblePlaceholder ): string
157+ {
158+ $ version = Str::upper ($ this ->getVersion ());
159+
160+ $ count = 0 ;
161+ $ path = str_replace ("{version} " , $ version , $ pathWithPossiblePlaceholder , $ count );
162+ if ($ count ) {
163+ return $ path ;
164+ }
165+
166+ return rtrim ($ path , DIRECTORY_SEPARATOR ) . DIRECTORY_SEPARATOR . $ version ;
167+ }
168+
169+ private function getAppPathToDto ()
170+ {
171+ return app_path ($ this ->replaceVersionPlaceHolderInPath ($ this ->destinationDir ));
158172 }
159173
160174 private function getAppPathToModels ()
161175 {
162176 return $ this ->getAppPathToDto () . DIRECTORY_SEPARATOR . self ::MODEL_PACKAGE ;
163177 }
164178
165- private function getInvokerPackage () {
179+ private function getInvokerPackage ()
180+ {
166181 return collect ([
167182 'App ' ,
168- str_replace (DIRECTORY_SEPARATOR , '\\\\' , $ this ->appDir ),
169- Str::upper ($ this ->getVersion ())
183+ str_replace (DIRECTORY_SEPARATOR , '\\\\' , $ this ->replaceVersionPlaceHolderInPath ($ this ->destinationDir ))
170184 ])->join ('\\\\' );
171185 }
172186}
0 commit comments