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/ ' ,
@@ -22,14 +21,10 @@ class Builder
2221 'Migration ' => 'database/migrations/ ' ,
2322 'Factory ' => 'database/factories/ ' ,
2423 'Seeder ' => 'database/seeders/ ' ,
24+ 'Policy ' => 'app/Domain/%s/ ' ,
2525 ];
2626 private Collection $ names ;
2727
28- public function __construct (Filesystem $ filesystem )
29- {
30- $ this ->filesystem = $ filesystem ;
31- }
32-
3328 public function prepare (): array
3429 {
3530 $ exists = [];
@@ -41,6 +36,8 @@ public function prepare(): array
4136 }
4237 }
4338
39+ clearstatcache ();
40+
4441 return array_filter ($ exists );
4542 }
4643
@@ -49,18 +46,22 @@ public function run(): void
4946 foreach ($ this ->filesPaths as $ stubName => $ finalPath ) {
5047 $ this ->createFile ($ stubName , $ finalPath );
5148 }
49+
50+ $ this ->insertDomain ();
5251 }
5352
5453 public function clear ()
5554 {
5655 foreach ($ this ->filesPaths as $ stubName => $ finalPath ) {
5756 $ this ->removeFile ($ stubName , $ finalPath );
5857 }
58+
59+ $ this ->removeDomain ();
5960 }
6061
6162 public function createFile (string $ stubName , string $ finalPath ): void
6263 {
63- $ stubFile = $ this -> filesystem -> get ($ this ->getStub ($ stubName ));
64+ $ stubFile = File:: get ($ this ->getStub ($ stubName ));
6465 $ content = $ this ->replacePlaceholders ($ stubFile );
6566 $ path = base_path (sprintf ($ finalPath , $ this ->getDomainName ()));
6667 $ fileName = $ this ->getName ('singularName ' , 'Dummy ' ) . $ stubName . '.php ' ;
@@ -71,7 +72,7 @@ public function createFile(string $stubName, string $finalPath): void
7172 }
7273
7374 $ file = $ path .$ fileName ;
74- $ this -> filesystem -> put ($ file , $ content );
75+ File:: put ($ file , $ content );
7576 }
7677
7778 // todo - we can use glob function as well, for the other files
@@ -83,10 +84,10 @@ public function removeFile(string $stubName, string $finalPath)
8384 $ file = $ path .$ fileName ;
8485
8586 if ($ stubName === 'Migration ' ) {
86- $ file = $ this -> filesystem -> glob ($ path .'*_create_ ' .$ this ->getName ('tableName ' ).'_table.php ' );
87+ $ file = File:: glob ($ path .'*_create_ ' .$ this ->getName ('tableName ' ).'_table.php ' );
8788 }
8889
89- $ this -> filesystem -> delete ($ file );
90+ File:: delete ($ file );
9091 }
9192
9293 public function getStub ($ stubName ): string
@@ -144,7 +145,7 @@ public function replacePlaceholders($stubFile)
144145 private function checkMigrationExists (): string
145146 {
146147 $ migrationPath = base_path ('database/migrations ' );
147- $ migrationFiles = $ this -> filesystem -> glob ($ migrationPath .'/*.php ' );
148+ $ migrationFiles = File:: glob ($ migrationPath .'/*.php ' );
148149
149150 $ exists = false ;
150151
@@ -163,11 +164,36 @@ private function checkClassExists(string $stubName, string $finalPath): string
163164 $ fileName = $ this ->getName ('singularName ' , 'Dummy ' ) . $ stubName . '.php ' ;
164165 $ file = $ path .$ fileName ;
165166
166- return $ this -> filesystem -> exists ($ file ) ? $ file : false ;
167+ return File:: exists ($ file ) ? $ file : false ;
167168 }
168169
169170 public function createDomainFolder ()
170171 {
171- $ this ->filesystem ->ensureDirectoryExists ($ this ->getDomainFolder ());
172+ File::ensureDirectoryExists ($ this ->getDomainFolder ());
173+ }
174+
175+ public function insertDomain (): void
176+ {
177+ $ path = app_path ('domains.php ' );
178+ $ domains = require ($ path );
179+
180+ array_push ($ domains , $ this ->getDomainName ());
181+ $ domains = array_unique ($ domains );
182+
183+ $ content = "<?php \n\nreturn " . preg_replace ("/[0-9]+ \=\>/i " , '' ,var_export ($ domains , true )). '; ' ;
184+ File::replace ($ path , $ content );
185+ }
186+
187+ public function removeDomain ()
188+ {
189+ $ path = app_path ('domains.php ' );
190+ $ domains = require ($ path );
191+
192+ if (($ key = array_search ($ this ->getDomainName (), $ domains )) !== false ) {
193+ unset($ domains [$ key ]);
194+ }
195+
196+ $ content = "<?php \n\nreturn " . preg_replace ("/[0-9]+ \=\>/i " , '' ,var_export ($ domains , true )). '; ' ;
197+ File::replace ($ path , $ content );
172198 }
173199}
0 commit comments