@@ -32,6 +32,7 @@ abstract class EntityGenerator
3232 'database_seeder ' => 'database/seeders ' ,
3333 'tests ' => 'tests ' ,
3434 'routes ' => 'routes ' ,
35+ 'translations ' => 'lang/en ' ,
3536 ];
3637
3738 protected $ paths = [];
@@ -88,11 +89,13 @@ public function setRelations(RelationsDTO $relations): self
8889 public function __construct ()
8990 {
9091 $ this ->paths = config ('entity-generator.paths ' );
92+
93+ $ this ->checkConfigHasCorrectPaths ();
9194 }
9295
93- protected function getNamespace (string $ configPath , ?string $ subFolder = null ): string
96+ protected function generateNamespace (string $ path , ?string $ additionalSubFolder = null ): string
9497 {
95- $ pathParts = $ this ->getNamespacePathParts ($ configPath , $ subFolder );
98+ $ pathParts = $ this ->getNamespacePathParts ($ path , $ additionalSubFolder );
9699
97100 $ namespace = array_map (fn (string $ part ) => ucfirst ($ part ), $ pathParts );
98101
@@ -101,7 +104,7 @@ protected function getNamespace(string $configPath, ?string $subFolder = null):
101104
102105 protected function createNamespace (string $ configPath , ?string $ subFolder = null ): void
103106 {
104- $ path = $ this ->getPath ($ configPath , $ subFolder );
107+ $ path = $ this ->getPath ($ this -> paths [ $ configPath] , $ subFolder );
105108
106109 $ fullPath = base_path ($ path );
107110
@@ -110,26 +113,20 @@ protected function createNamespace(string $configPath, ?string $subFolder = null
110113 }
111114 }
112115
113- protected function getNamespacePathParts (string $ configPath , ?string $ subFolder = null ): array
116+ protected function getNamespacePathParts (string $ path , ?string $ additionalSubFolder = null ): array
114117 {
115- $ pathParts = explode ('/ ' , $ this ->getPath ($ configPath , $ subFolder ));
118+ $ pathParts = explode ('/ ' , $ this ->getPath ($ path , $ additionalSubFolder ));
116119
117120 if (Str::endsWith (Arr::last ($ pathParts ), '.php ' )) {
118121 array_pop ($ pathParts );
119122 }
120123
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-
127124 return $ pathParts ;
128125 }
129126
130- protected function getPath (string $ configPath , ?string $ subFolder = null ): string
127+ protected function getPath (string $ path , ?string $ subFolder = null ): string
131128 {
132- return when ($ subFolder , fn () => Str::finish ($ this -> paths [ $ configPath ] , '/ ' ) . $ subFolder , $ this -> paths [ $ configPath ] );
129+ return when ($ subFolder , fn () => Str::finish ($ path , '/ ' ) . $ subFolder , $ path );
133130 }
134131
135132 protected function isFolderHasCorrectCase (string $ folder , string $ configPath ): bool
@@ -154,7 +151,7 @@ protected function classExists(string $path, string $name, ?string $subFolder =
154151
155152 protected function getClassPath (string $ path , string $ name , ?string $ subFolder = null ): string
156153 {
157- $ path = $ this ->getPath ($ path , $ subFolder );
154+ $ path = $ this ->getPath ($ this -> paths [ $ path] , $ subFolder );
158155
159156 return "{$ path }/ {$ name }.php " ;
160157 }
@@ -269,7 +266,7 @@ protected function getModelClass(string $model): string
269266 {
270267 $ subfolder = when ($ model === $ this ->model , $ this ->modelSubFolder );
271268
272- $ modelNamespace = $ this ->getNamespace ( 'models ' , $ subfolder );
269+ $ modelNamespace = $ this ->generateNamespace ( $ this -> paths [ 'models ' ] , $ subfolder );
273270
274271 return "{$ modelNamespace }\\{$ model }" ;
275272 }
@@ -308,4 +305,17 @@ protected function pathToNamespace(string $name): string
308305 {
309306 return ucwords (Str::replace ('/ ' , '\\' , $ name ), '\\' );
310307 }
308+
309+ protected function checkConfigHasCorrectPaths (): void
310+ {
311+ foreach ($ this ->paths as $ configPath => $ path ) {
312+ $ pathParts = $ this ->getNamespacePathParts ($ path );
313+
314+ foreach ($ pathParts as $ part ) {
315+ if (!$ this ->isFolderHasCorrectCase ($ part , $ configPath )) {
316+ throw new IncorrectClassPathException ("Incorrect path to {$ configPath }, {$ part } folder must start with a capital letter, please specify the path according to the PSR. " );
317+ }
318+ }
319+ }
320+ }
311321}
0 commit comments