22
33namespace RonasIT \Support \Generators ;
44
5- use Illuminate \Database \Eloquent \Relations \BelongsTo ;
6- use Illuminate \Filesystem \Filesystem ;
5+ use Throwable ;
6+ use ReflectionClass ;
7+ use ReflectionMethod ;
78use Illuminate \Support \Arr ;
8- use Illuminate \Support \Facades \DB ;
99use Illuminate \Support \Str ;
10+ use Illuminate \Support \Facades \DB ;
11+ use Illuminate \Filesystem \Filesystem ;
1012use RonasIT \Support \DTO \RelationsDTO ;
1113use RonasIT \Support \Events \WarningEvent ;
14+ use Illuminate \Database \Eloquent \Relations \BelongsTo ;
1215use RonasIT \Support \Exceptions \ClassNotExistsException ;
1316use RonasIT \Support \Exceptions \IncorrectClassPathException ;
14- use Throwable ;
15- use ReflectionMethod ;
16- use ReflectionClass ;
17+ use RonasIT \Support \Exceptions \ResourceAlreadyExistsException ;
1718
1819/**
1920 * @property Filesystem $fs
@@ -32,6 +33,7 @@ abstract class EntityGenerator
3233 'database_seeder ' => 'database/seeders ' ,
3334 'tests ' => 'tests ' ,
3435 'routes ' => 'routes ' ,
36+ 'translations ' => 'lang/en ' ,
3537 ];
3638
3739 protected $ paths = [];
@@ -51,7 +53,7 @@ public function setCrudOptions(array $crudOptions): self
5153
5254 public function setModel (string $ model ): self
5355 {
54- $ this ->model = Str:: studly ( $ model) ;
56+ $ this ->model = $ model ;
5557
5658 return $ this ;
5759 }
@@ -88,11 +90,13 @@ public function setRelations(RelationsDTO $relations): self
8890 public function __construct ()
8991 {
9092 $ this ->paths = config ('entity-generator.paths ' );
93+
94+ $ this ->checkConfigHasCorrectPaths ();
9195 }
9296
93- protected function getNamespace (string $ configPath , ?string $ subFolder = null ): string
97+ protected function generateNamespace (string $ path , ?string $ additionalSubFolder = null ): string
9498 {
95- $ pathParts = $ this ->getNamespacePathParts ($ configPath , $ subFolder );
99+ $ pathParts = $ this ->getNamespacePathParts ($ path , $ additionalSubFolder );
96100
97101 $ namespace = array_map (fn (string $ part ) => ucfirst ($ part ), $ pathParts );
98102
@@ -101,7 +105,7 @@ protected function getNamespace(string $configPath, ?string $subFolder = null):
101105
102106 protected function createNamespace (string $ configPath , ?string $ subFolder = null ): void
103107 {
104- $ path = $ this ->getPath ($ configPath , $ subFolder );
108+ $ path = $ this ->getPath ($ this -> paths [ $ configPath] , $ subFolder );
105109
106110 $ fullPath = base_path ($ path );
107111
@@ -110,26 +114,20 @@ protected function createNamespace(string $configPath, ?string $subFolder = null
110114 }
111115 }
112116
113- protected function getNamespacePathParts (string $ configPath , ?string $ subFolder = null ): array
117+ protected function getNamespacePathParts (string $ path , ?string $ additionalSubFolder = null ): array
114118 {
115- $ pathParts = explode ('/ ' , $ this ->getPath ($ configPath , $ subFolder ));
119+ $ pathParts = explode ('/ ' , $ this ->getPath ($ path , $ additionalSubFolder ));
116120
117121 if (Str::endsWith (Arr::last ($ pathParts ), '.php ' )) {
118122 array_pop ($ pathParts );
119123 }
120124
121- foreach ($ pathParts as $ part ) {
122- if (!$ this ->isFolderHasCorrectCase ($ part , $ configPath )) {
123- throw new IncorrectClassPathException ("Incorrect path to {$ configPath }, {$ part } folder must start with a capital letter, please specify the path according to the PSR. " );
124- }
125- }
126-
127125 return $ pathParts ;
128126 }
129127
130- protected function getPath (string $ configPath , ?string $ subFolder = null ): string
128+ protected function getPath (string $ path , ?string $ subFolder = null ): string
131129 {
132- return when ($ subFolder , fn () => Str::finish ($ this -> paths [ $ configPath ] , '/ ' ) . $ subFolder , $ this -> paths [ $ configPath ] );
130+ return when ($ subFolder , fn () => Str::finish ($ path , '/ ' ) . $ subFolder , $ path );
133131 }
134132
135133 protected function isFolderHasCorrectCase (string $ folder , string $ configPath ): bool
@@ -154,7 +152,7 @@ protected function classExists(string $path, string $name, ?string $subFolder =
154152
155153 protected function getClassPath (string $ path , string $ name , ?string $ subFolder = null ): string
156154 {
157- $ path = $ this ->getPath ($ path , $ subFolder );
155+ $ path = $ this ->getPath ($ this -> paths [ $ path] , $ subFolder );
158156
159157 return "{$ path }/ {$ name }.php " ;
160158 }
@@ -269,7 +267,7 @@ protected function getModelClass(string $model): string
269267 {
270268 $ subfolder = when ($ model === $ this ->model , $ this ->modelSubFolder );
271269
272- $ modelNamespace = $ this ->getNamespace ( 'models ' , $ subfolder );
270+ $ modelNamespace = $ this ->generateNamespace ( $ this -> paths [ 'models ' ] , $ subfolder );
273271
274272 return "{$ modelNamespace }\\{$ model }" ;
275273 }
@@ -308,4 +306,26 @@ protected function pathToNamespace(string $name): string
308306 {
309307 return ucwords (Str::replace ('/ ' , '\\' , $ name ), '\\' );
310308 }
309+
310+ protected function checkConfigHasCorrectPaths (): void
311+ {
312+ foreach ($ this ->paths as $ configPath => $ path ) {
313+ $ pathParts = $ this ->getNamespacePathParts ($ path );
314+
315+ foreach ($ pathParts as $ part ) {
316+ if (!$ this ->isFolderHasCorrectCase ($ part , $ configPath )) {
317+ throw new IncorrectClassPathException ("Incorrect path to {$ configPath }, {$ part } folder must start with a capital letter, please specify the path according to the PSR. " );
318+ }
319+ }
320+ }
321+ }
322+
323+ protected function checkResourceExists (string $ path , string $ resourceName , ?string $ subFolder = null ): void
324+ {
325+ if ($ this ->classExists ($ path , $ resourceName , $ subFolder )) {
326+ $ filePath = $ this ->getClassPath ($ path , $ resourceName , $ subFolder );
327+
328+ throw new ResourceAlreadyExistsException ($ filePath );
329+ }
330+ }
311331}
0 commit comments