From acab7ed4b7c2479551567619347e16dbbb30d76a Mon Sep 17 00:00:00 2001 From: pirs1337 Date: Fri, 17 Oct 2025 13:54:00 +0600 Subject: [PATCH 1/4] feat: change getNamespace method refs: https://github.com/RonasIT/laravel-entity-generator/issues/190 --- src/Generators/AbstractTestsGenerator.php | 2 +- src/Generators/ControllerGenerator.php | 10 +++---- src/Generators/EntityGenerator.php | 33 +++++++++++++---------- src/Generators/FactoryGenerator.php | 4 +-- src/Generators/ModelGenerator.php | 4 +-- src/Generators/NovaResourceGenerator.php | 6 ++--- src/Generators/NovaTestGenerator.php | 2 +- src/Generators/RepositoryGenerator.php | 4 +-- src/Generators/RequestsGenerator.php | 4 +-- src/Generators/ResourceGenerator.php | 6 ++--- src/Generators/SeederGenerator.php | 6 ++--- src/Generators/ServiceGenerator.php | 4 +-- src/Generators/TestsGenerator.php | 4 +-- 13 files changed, 47 insertions(+), 42 deletions(-) diff --git a/src/Generators/AbstractTestsGenerator.php b/src/Generators/AbstractTestsGenerator.php index d64520c7..612f52fc 100644 --- a/src/Generators/AbstractTestsGenerator.php +++ b/src/Generators/AbstractTestsGenerator.php @@ -175,7 +175,7 @@ protected function getMockModel($model): array return []; } - $factoryNamespace = "{$this->getNamespace('factories')}\\{$model}Factory"; + $factoryNamespace = "{$this->generateNamespace($this->paths['factories'])}\\{$model}Factory"; $factory = $factoryNamespace::new(); return $factory diff --git a/src/Generators/ControllerGenerator.php b/src/Generators/ControllerGenerator.php index d619b360..849171fd 100644 --- a/src/Generators/ControllerGenerator.php +++ b/src/Generators/ControllerGenerator.php @@ -47,10 +47,10 @@ protected function getControllerContent($model): string return $this->getStub('controller', [ 'entity' => $model, 'requestsFolder' => $model, - 'namespace' => $this->getNamespace('controllers'), - 'requestsNamespace' => $this->getNamespace('requests'), - 'resourcesNamespace' => $this->getNamespace('resources'), - 'servicesNamespace' => $this->getNamespace('services'), + 'namespace' => $this->generateNamespace($this->paths['controllers']), + 'requestsNamespace' => $this->generateNamespace($this->paths['requests']), + 'resourcesNamespace' => $this->generateNamespace($this->paths['resources']), + 'servicesNamespace' => $this->generateNamespace($this->paths['services']), ]); } @@ -100,7 +100,7 @@ protected function addUseController(string $routesPath): void $routesFileContent = file_get_contents($routesPath); $stub = $this->getStub('use_routes', [ - 'namespace' => $this->getNamespace('controllers'), + 'namespace' => $this->generateNamespace($this->paths['controllers']), 'entity' => $this->model ]); diff --git a/src/Generators/EntityGenerator.php b/src/Generators/EntityGenerator.php index 0f83d698..28137416 100644 --- a/src/Generators/EntityGenerator.php +++ b/src/Generators/EntityGenerator.php @@ -32,6 +32,7 @@ abstract class EntityGenerator 'database_seeder' => 'database/seeders', 'tests' => 'tests', 'routes' => 'routes', + 'translations' => 'lang/en', ]; protected $paths = []; @@ -88,11 +89,21 @@ public function setRelations(RelationsDTO $relations): self public function __construct() { $this->paths = config('entity-generator.paths'); + + foreach ($this->paths as $configPath => $path) { + $pathParts = $this->getNamespacePathParts($path); + + foreach ($pathParts as $part) { + if (!$this->isFolderHasCorrectCase($part, $configPath)) { + throw new IncorrectClassPathException("Incorrect path to {$configPath}, {$part} folder must start with a capital letter, please specify the path according to the PSR."); + } + } + } } - protected function getNamespace(string $configPath, ?string $subFolder = null): string + protected function generateNamespace(string $path, ?string $additionalSubFolder = null): string { - $pathParts = $this->getNamespacePathParts($configPath, $subFolder); + $pathParts = $this->getNamespacePathParts($path, $additionalSubFolder); $namespace = array_map(fn (string $part) => ucfirst($part), $pathParts); @@ -101,7 +112,7 @@ protected function getNamespace(string $configPath, ?string $subFolder = null): protected function createNamespace(string $configPath, ?string $subFolder = null): void { - $path = $this->getPath($configPath, $subFolder); + $path = $this->getPath($this->paths[$configPath], $subFolder); $fullPath = base_path($path); @@ -110,26 +121,20 @@ protected function createNamespace(string $configPath, ?string $subFolder = null } } - protected function getNamespacePathParts(string $configPath, ?string $subFolder = null): array + protected function getNamespacePathParts(string $path, ?string $additionalSubFolder = null): array { - $pathParts = explode('/', $this->getPath($configPath, $subFolder)); + $pathParts = explode('/', $this->getPath($path, $additionalSubFolder)); if (Str::endsWith(Arr::last($pathParts), '.php')) { array_pop($pathParts); } - foreach ($pathParts as $part) { - if (!$this->isFolderHasCorrectCase($part, $configPath)) { - throw new IncorrectClassPathException("Incorrect path to {$configPath}, {$part} folder must start with a capital letter, please specify the path according to the PSR."); - } - } - return $pathParts; } - protected function getPath(string $configPath, ?string $subFolder = null): string + protected function getPath(string $path, ?string $subFolder = null): string { - return when($subFolder, fn () => Str::finish($this->paths[$configPath], '/') . $subFolder, $this->paths[$configPath]); + return when($subFolder, fn () => Str::finish($path, '/') . $subFolder, $path); } protected function isFolderHasCorrectCase(string $folder, string $configPath): bool @@ -266,7 +271,7 @@ protected function getModelClass(string $model): string { $subfolder = when($model === $this->model, $this->modelSubFolder); - $modelNamespace = $this->getNamespace('models', $subfolder); + $modelNamespace = $this->generateNamespace($this->paths['models'], $subfolder); return "{$modelNamespace}\\{$model}"; } diff --git a/src/Generators/FactoryGenerator.php b/src/Generators/FactoryGenerator.php index 178fb4e9..3a9bc975 100644 --- a/src/Generators/FactoryGenerator.php +++ b/src/Generators/FactoryGenerator.php @@ -51,10 +51,10 @@ public function generate(): void $this->createNamespace('factories'); $factoryContent = $this->getStub('factory', [ - 'namespace' => $this->getNamespace('factories'), + 'namespace' => $this->generateNamespace($this->paths['factories']), 'entity' => $this->model, 'fields' => $this->prepareFields(), - 'modelNamespace' => $this->getNamespace('models', $this->modelSubFolder), + 'modelNamespace' => $this->generateNamespace($this->paths['models'], $this->modelSubFolder), ]); $this->saveClass('factories', "{$this->model}Factory", $factoryContent); diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index dfc62bcf..d075f9cc 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -50,7 +50,7 @@ protected function getNewModelContent(): string 'fields' => Arr::collapse($this->fields), 'relations' => $this->prepareRelations(), 'casts' => $this->getCasts($this->fields), - 'namespace' => $this->getNamespace('models', $this->modelSubFolder), + 'namespace' => $this->generateNamespace($this->paths['models'], $this->modelSubFolder), 'importRelations' => $this->getImportedRelations(), 'anotationProperties' => $this->generateAnnotationProperties($this->fields), 'hasCarbonField' => !empty($this->fields['timestamp']) || !empty($this->fields['timestamp-required']), @@ -192,7 +192,7 @@ protected function shouldImportRelation(string $relation): bool protected function generateClassNamespace(string $className, ?string $folder = null): string { - $path = $this->getNamespace('models', $folder); + $path = $this->generateNamespace($this->paths['models'], $folder); $psrPath = $this->pathToNamespace($className); return "{$path}\\{$psrPath}"; diff --git a/src/Generators/NovaResourceGenerator.php b/src/Generators/NovaResourceGenerator.php index 7be416a9..8235151f 100644 --- a/src/Generators/NovaResourceGenerator.php +++ b/src/Generators/NovaResourceGenerator.php @@ -86,7 +86,7 @@ public function generate(): void 'fields' => $novaFields, 'types' => array_unique(data_get($novaFields, '*.type')), 'imports' => $this->getImports(), - 'namespace' => $this->getNamespace('nova', $this->modelSubFolder), + 'namespace' => $this->generateNamespace($this->paths['nova'], $this->modelSubFolder), ]); $this->saveClass('nova', "{$this->model}Resource", $fileContent, $this->modelSubFolder); @@ -182,11 +182,11 @@ protected function getColumnList(string $table, ?string $connectionName = null): protected function getImports(): array { $imports = [ - "{$this->getNamespace('models', $this->modelSubFolder)}\\{$this->model}", + "{$this->generateNamespace($this->paths['models'], $this->modelSubFolder)}\\{$this->model}", ]; if (!empty($this->modelSubFolder)) { - $imports[] = "{$this->getNamespace('nova')}\\Resource"; + $imports[] = "{$this->generateNamespace($this->paths['nova'])}\\Resource"; } return $imports; diff --git a/src/Generators/NovaTestGenerator.php b/src/Generators/NovaTestGenerator.php index 3c3737ef..e0a3afb6 100644 --- a/src/Generators/NovaTestGenerator.php +++ b/src/Generators/NovaTestGenerator.php @@ -80,7 +80,7 @@ public function generateTests(): void $resourceClass = Str::afterLast($this->novaResourceClassName, '\\'); $fileContent = $this->getStub('nova_test', [ - 'entity_namespace' => $this->getNamespace('models', $this->modelSubFolder), + 'entity_namespace' => $this->generateNamespace($this->paths['models'], $this->modelSubFolder), 'entity' => $this->model, 'resource_name' => $resourceClass, 'resource_namespace' => $this->novaResourceClassName, diff --git a/src/Generators/RepositoryGenerator.php b/src/Generators/RepositoryGenerator.php index d051ca8b..c984dcdb 100644 --- a/src/Generators/RepositoryGenerator.php +++ b/src/Generators/RepositoryGenerator.php @@ -26,8 +26,8 @@ public function generate(): void $repositoryContent = $this->getStub('repository', [ 'entity' => $this->model, - 'namespace' => $this->getNamespace('repositories'), - 'modelNamespace' => $this->getNamespace('models', $this->modelSubFolder) + 'namespace' => $this->generateNamespace($this->paths['repositories']), + 'modelNamespace' => $this->generateNamespace($this->paths['models'], $this->modelSubFolder) ]); $this->saveClass('repositories', "{$this->model}Repository", $repositoryContent); diff --git a/src/Generators/RequestsGenerator.php b/src/Generators/RequestsGenerator.php index 504b2229..96e1437a 100644 --- a/src/Generators/RequestsGenerator.php +++ b/src/Generators/RequestsGenerator.php @@ -73,8 +73,8 @@ protected function createRequest($method, $needToValidate = true, $parameters = 'parameters' => $parameters, 'needToValidate' => $needToValidate, 'requestsFolder' => $requestsFolder, - 'namespace' => $this->getNamespace('requests'), - 'servicesNamespace' => $this->getNamespace('services'), + 'namespace' => $this->generateNamespace($this->paths['requests']), + 'servicesNamespace' => $this->generateNamespace($this->paths['services']), 'entityNamespace' => $this->getModelClass($this->model), ]); diff --git a/src/Generators/ResourceGenerator.php b/src/Generators/ResourceGenerator.php index ff6a5d05..6f70a860 100644 --- a/src/Generators/ResourceGenerator.php +++ b/src/Generators/ResourceGenerator.php @@ -35,7 +35,7 @@ public function generateCollectionResource(): void $collectionResourceContent = $this->getStub('collection_resource', [ 'singular_name' => $this->model, 'plural_name' => $pluralName, - 'namespace' => $this->getNamespace('resources') + 'namespace' => $this->generateNamespace($this->paths['resources']), ]); $this->saveClass('resources', "{$pluralName}CollectionResource", $collectionResourceContent, $this->model); @@ -55,8 +55,8 @@ public function generateResource(): void $resourceContent = $this->getStub('resource', [ 'entity' => $this->model, - 'namespace' => $this->getNamespace('resources'), - 'model_namespace' => $this->getNamespace('models', $this->modelSubFolder), + 'namespace' => $this->generateNamespace($this->paths['resources']), + 'model_namespace' => $this->generateNamespace($this->paths['models'], $this->modelSubFolder), ]); $this->saveClass('resources', "{$this->model}Resource", $resourceContent, $this->model); diff --git a/src/Generators/SeederGenerator.php b/src/Generators/SeederGenerator.php index c4c1d7fd..e3a738d5 100644 --- a/src/Generators/SeederGenerator.php +++ b/src/Generators/SeederGenerator.php @@ -36,7 +36,7 @@ public function generate(): void protected function createDatabaseSeeder(): void { $content = "getStub('database_empty_seeder', [ - 'namespace' => $this->getNamespace('seeders') + 'namespace' => $this->generateNamespace($this->paths['seeders']), ]); file_put_contents($this->databaseSeederPath, $content); @@ -51,8 +51,8 @@ protected function createEntitySeeder(): void $content = "getStub('seeder', [ 'entity' => $this->model, 'relations' => $this->prepareRelations(), - 'namespace' => $this->getNamespace('seeders'), - 'factoryNamespace' => $this->getNamespace('factories'), + 'namespace' => $this->generateNamespace($this->paths['seeders']), + 'factoryNamespace' => $this->generateNamespace($this->paths['factories']), ]) . "\n"; $seederPath = "{$this->seedsPath}/{$this->model}Seeder.php"; diff --git a/src/Generators/ServiceGenerator.php b/src/Generators/ServiceGenerator.php index bae74d5c..da252e33 100644 --- a/src/Generators/ServiceGenerator.php +++ b/src/Generators/ServiceGenerator.php @@ -27,8 +27,8 @@ public function generate(): void $serviceContent = $this->getStub('service', [ 'entity' => $this->model, 'fields' => $this->getFields(), - 'namespace' => $this->getNamespace('services'), - 'repositoriesNamespace' => $this->getNamespace('repositories'), + 'namespace' => $this->generateNamespace($this->paths['services']), + 'repositoriesNamespace' => $this->generateNamespace($this->paths['repositories']), ]); $this->saveClass('services', "{$this->model}Service", $serviceContent); diff --git a/src/Generators/TestsGenerator.php b/src/Generators/TestsGenerator.php index 1fc58d60..ab67210f 100644 --- a/src/Generators/TestsGenerator.php +++ b/src/Generators/TestsGenerator.php @@ -41,8 +41,8 @@ protected function generateTests(): void 'databaseTableName' => $this->getTableName($this->model), 'entities' => $this->getTableName($this->model, '-'), 'withAuth' => $this->withAuth, - 'entityNamespace' => $this->getNamespace('models', $this->modelSubFolder), - 'userNamespace' => $this->getNamespace('models'), + 'entityNamespace' => $this->generateNamespace($this->paths['models'], $this->modelSubFolder), + 'userNamespace' => $this->generateNamespace($this->paths['models']), 'hasModificationEndpoints' => !empty(array_intersect($this->crudOptions, ['C', 'U', 'D'])), ]); From 22261588a97ff488806556f1d8534d7186a14c36 Mon Sep 17 00:00:00 2001 From: pirs1337 Date: Tue, 21 Oct 2025 14:33:24 +0600 Subject: [PATCH 2/4] refactor: move config check to protected method refs: https://github.com/RonasIT/laravel-entity-generator/issues/190 --- src/Generators/EntityGenerator.php | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/Generators/EntityGenerator.php b/src/Generators/EntityGenerator.php index 28137416..a2772407 100644 --- a/src/Generators/EntityGenerator.php +++ b/src/Generators/EntityGenerator.php @@ -90,15 +90,7 @@ public function __construct() { $this->paths = config('entity-generator.paths'); - foreach ($this->paths as $configPath => $path) { - $pathParts = $this->getNamespacePathParts($path); - - foreach ($pathParts as $part) { - if (!$this->isFolderHasCorrectCase($part, $configPath)) { - throw new IncorrectClassPathException("Incorrect path to {$configPath}, {$part} folder must start with a capital letter, please specify the path according to the PSR."); - } - } - } + $this->checkConfigHasCorrectPaths(); } protected function generateNamespace(string $path, ?string $additionalSubFolder = null): string @@ -310,4 +302,17 @@ protected function pathToNamespace(string $name): string { return ucwords(Str::replace('/', '\\', $name), '\\'); } + + protected function checkConfigHasCorrectPaths(): void + { + foreach ($this->paths as $configPath => $path) { + $pathParts = $this->getNamespacePathParts($path); + + foreach ($pathParts as $part) { + if (!$this->isFolderHasCorrectCase($part, $configPath)) { + throw new IncorrectClassPathException("Incorrect path to {$configPath}, {$part} folder must start with a capital letter, please specify the path according to the PSR."); + } + } + } + } } From e489422f38c5a1ec8cfe90a12514594f39bf16f9 Mon Sep 17 00:00:00 2001 From: pirs1337 Date: Tue, 21 Oct 2025 16:37:56 +0600 Subject: [PATCH 3/4] fix: conflicts refs: https://github.com/RonasIT/laravel-entity-generator/issues/190 --- src/Generators/EntityGenerator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Generators/EntityGenerator.php b/src/Generators/EntityGenerator.php index 53ccf813..7f9af2d3 100644 --- a/src/Generators/EntityGenerator.php +++ b/src/Generators/EntityGenerator.php @@ -151,7 +151,7 @@ protected function classExists(string $path, string $name, ?string $subFolder = protected function getClassPath(string $path, string $name, ?string $subFolder = null): string { - $path = $this->getPath($path, $subFolder); + $path = $this->getPath($this->paths[$path], $subFolder); return "{$path}/{$name}.php"; } From 4c5c4608f6d2bab287bef5601b7f614cf27536ad Mon Sep 17 00:00:00 2001 From: pirs1337 Date: Thu, 23 Oct 2025 15:47:54 +0600 Subject: [PATCH 4/4] fix: test refs: https://github.com/RonasIT/laravel-entity-generator/issues/190 --- src/Generators/NovaTestGenerator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Generators/NovaTestGenerator.php b/src/Generators/NovaTestGenerator.php index e7c83c45..ca6381e6 100644 --- a/src/Generators/NovaTestGenerator.php +++ b/src/Generators/NovaTestGenerator.php @@ -78,7 +78,7 @@ public function generateTests(): void 'lower_entities' => $this->getPluralName(Str::snake($this->model)), 'actions' => $actions, 'filters' => $filters, - 'models_namespace' => $this->getNamespace('models'), + 'models_namespace' => $this->generateNamespace($this->paths['models']), ]); $this->saveClass('tests', "Nova{$this->model}ResourceTest", $fileContent);