33namespace LaravelDomainOriented ;
44
55use Carbon \Carbon ;
6- use Illuminate \Filesystem \Filesystem ;
76use Illuminate \Support \Collection ;
7+ use Illuminate \Support \Facades \File ;
88use Illuminate \Support \Str ;
99
1010class Builder
1111{
12- private Filesystem $ filesystem ;
1312 private array $ filesPaths = [
1413 'Controller ' => 'app/Http/Controllers/ ' ,
1514 'Resource ' => 'app/Domain/%s/ ' ,
@@ -25,9 +24,9 @@ class Builder
2524 ];
2625 private Collection $ names ;
2726
28- public function __construct (Filesystem $ filesystem )
27+ public function __construct ()
2928 {
30- $ this ->filesystem = $ filesystem ;
29+ $ this ->createDomainsFile () ;
3130 }
3231
3332 public function prepare (): array
@@ -41,6 +40,8 @@ public function prepare(): array
4140 }
4241 }
4342
43+ clearstatcache ();
44+
4445 return array_filter ($ exists );
4546 }
4647
@@ -49,18 +50,22 @@ public function run(): void
4950 foreach ($ this ->filesPaths as $ stubName => $ finalPath ) {
5051 $ this ->createFile ($ stubName , $ finalPath );
5152 }
53+
54+ $ this ->insertDomain ();
5255 }
5356
5457 public function clear ()
5558 {
5659 foreach ($ this ->filesPaths as $ stubName => $ finalPath ) {
5760 $ this ->removeFile ($ stubName , $ finalPath );
5861 }
62+
63+ $ this ->removeDomain ();
5964 }
6065
6166 public function createFile (string $ stubName , string $ finalPath ): void
6267 {
63- $ stubFile = $ this -> filesystem -> get ($ this ->getStub ($ stubName ));
68+ $ stubFile = File:: get ($ this ->getStub ($ stubName ));
6469 $ content = $ this ->replacePlaceholders ($ stubFile );
6570 $ path = base_path (sprintf ($ finalPath , $ this ->getDomainName ()));
6671 $ fileName = $ this ->getName ('singularName ' , 'Dummy ' ) . $ stubName . '.php ' ;
@@ -71,7 +76,7 @@ public function createFile(string $stubName, string $finalPath): void
7176 }
7277
7378 $ file = $ path .$ fileName ;
74- $ this -> filesystem -> put ($ file , $ content );
79+ File:: put ($ file , $ content );
7580 }
7681
7782 // todo - we can use glob function as well, for the other files
@@ -83,10 +88,10 @@ public function removeFile(string $stubName, string $finalPath)
8388 $ file = $ path .$ fileName ;
8489
8590 if ($ stubName === 'Migration ' ) {
86- $ file = $ this -> filesystem -> glob ($ path .'*_create_ ' .$ this ->getName ('tableName ' ).'_table.php ' );
91+ $ file = File:: glob ($ path .'*_create_ ' .$ this ->getName ('tableName ' ).'_table.php ' );
8792 }
8893
89- $ this -> filesystem -> delete ($ file );
94+ File:: delete ($ file );
9095 }
9196
9297 public function getStub ($ stubName ): string
@@ -144,7 +149,7 @@ public function replacePlaceholders($stubFile)
144149 private function checkMigrationExists (): string
145150 {
146151 $ migrationPath = base_path ('database/migrations ' );
147- $ migrationFiles = $ this -> filesystem -> glob ($ migrationPath .'/*.php ' );
152+ $ migrationFiles = File:: glob ($ migrationPath .'/*.php ' );
148153
149154 $ exists = false ;
150155
@@ -163,11 +168,47 @@ private function checkClassExists(string $stubName, string $finalPath): string
163168 $ fileName = $ this ->getName ('singularName ' , 'Dummy ' ) . $ stubName . '.php ' ;
164169 $ file = $ path .$ fileName ;
165170
166- return $ this -> filesystem -> exists ($ file ) ? $ file : false ;
171+ return File:: exists ($ file ) ? $ file : false ;
167172 }
168173
169174 public function createDomainFolder ()
170175 {
171- $ this ->filesystem ->ensureDirectoryExists ($ this ->getDomainFolder ());
176+ File::ensureDirectoryExists ($ this ->getDomainFolder ());
177+ }
178+
179+ public function createDomainsFile ()
180+ {
181+ $ filePath = app_path ('domains.php ' );
182+ $ exists = File::exists ($ filePath );
183+ $ stubFile = File::get (__DIR__ .'/Stubs/domains.stub ' );
184+
185+ if (!$ exists ) {
186+ File::put ($ filePath , $ stubFile );
187+ }
188+ }
189+
190+ public function insertDomain (): void
191+ {
192+ $ path = app_path ('domains.php ' );
193+ $ domains = require ($ path );
194+
195+ array_push ($ domains , $ this ->getDomainName ());
196+ $ domains = array_unique ($ domains );
197+
198+ $ content = "<?php \n\nreturn " . preg_replace ("/[0-9]+ \=\>/i " , '' ,var_export ($ domains , true )). '; ' ;
199+ File::replace ($ path , $ content );
200+ }
201+
202+ public function removeDomain ()
203+ {
204+ $ path = app_path ('domains.php ' );
205+ $ domains = require ($ path );
206+
207+ if (($ key = array_search ($ this ->getDomainName (), $ domains )) !== false ) {
208+ unset($ domains [$ key ]);
209+ }
210+
211+ $ content = "<?php \n\nreturn " . preg_replace ("/[0-9]+ \=\>/i " , '' ,var_export ($ domains , true )). '; ' ;
212+ File::replace ($ path , $ content );
172213 }
173214}
0 commit comments