Skip to content

Commit 219d2b1

Browse files
authored
Merge pull request #215 from RonasIT/190_change_getNamespace_method
190 change get namespace method
2 parents ff3a96e + 4c5c460 commit 219d2b1

13 files changed

+54
-44
lines changed

src/Generators/AbstractTestsGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ protected function getMockModel($model): array
175175
return [];
176176
}
177177

178-
$factoryNamespace = "{$this->getNamespace('factories')}\\{$model}Factory";
178+
$factoryNamespace = "{$this->generateNamespace($this->paths['factories'])}\\{$model}Factory";
179179
$factory = $factoryNamespace::new();
180180

181181
return $factory

src/Generators/ControllerGenerator.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ protected function getControllerContent($model): string
4545
return $this->getStub('controller', [
4646
'entity' => $model,
4747
'requestsFolder' => $model,
48-
'namespace' => $this->getNamespace('controllers'),
49-
'requestsNamespace' => $this->getNamespace('requests'),
50-
'resourcesNamespace' => $this->getNamespace('resources'),
51-
'servicesNamespace' => $this->getNamespace('services'),
48+
'namespace' => $this->generateNamespace($this->paths['controllers']),
49+
'requestsNamespace' => $this->generateNamespace($this->paths['requests']),
50+
'resourcesNamespace' => $this->generateNamespace($this->paths['resources']),
51+
'servicesNamespace' => $this->generateNamespace($this->paths['services']),
5252
]);
5353
}
5454

@@ -98,7 +98,7 @@ protected function addUseController(string $routesPath): void
9898
$routesFileContent = file_get_contents($routesPath);
9999

100100
$stub = $this->getStub('use_routes', [
101-
'namespace' => $this->getNamespace('controllers'),
101+
'namespace' => $this->generateNamespace($this->paths['controllers']),
102102
'entity' => $this->model
103103
]);
104104

src/Generators/EntityGenerator.php

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/Generators/FactoryGenerator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ public function generate(): void
4949
$this->createNamespace('factories');
5050

5151
$factoryContent = $this->getStub('factory', [
52-
'namespace' => $this->getNamespace('factories'),
52+
'namespace' => $this->generateNamespace($this->paths['factories']),
5353
'entity' => $this->model,
5454
'fields' => $this->prepareFields(),
55-
'modelNamespace' => $this->getNamespace('models', $this->modelSubFolder),
55+
'modelNamespace' => $this->generateNamespace($this->paths['models'], $this->modelSubFolder),
5656
]);
5757

5858
$this->saveClass('factories', "{$this->model}Factory", $factoryContent);

src/Generators/ModelGenerator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ protected function getNewModelContent(): string
4747
'fields' => Arr::collapse($this->fields),
4848
'relations' => $this->prepareRelations(),
4949
'casts' => $this->getCasts($this->fields),
50-
'namespace' => $this->getNamespace('models', $this->modelSubFolder),
50+
'namespace' => $this->generateNamespace($this->paths['models'], $this->modelSubFolder),
5151
'importRelations' => $this->getImportedRelations(),
5252
'anotationProperties' => $this->generateAnnotationProperties($this->fields),
5353
'hasCarbonField' => !empty($this->fields['timestamp']) || !empty($this->fields['timestamp-required']),
@@ -189,7 +189,7 @@ protected function shouldImportRelation(string $relation): bool
189189

190190
protected function generateClassNamespace(string $className, ?string $folder = null): string
191191
{
192-
$path = $this->getNamespace('models', $folder);
192+
$path = $this->generateNamespace($this->paths['models'], $folder);
193193
$psrPath = $this->pathToNamespace($className);
194194

195195
return "{$path}\\{$psrPath}";

src/Generators/NovaResourceGenerator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public function generate(): void
8383
'fields' => $novaFields,
8484
'types' => array_unique(data_get($novaFields, '*.type')),
8585
'imports' => $this->getImports(),
86-
'namespace' => $this->getNamespace('nova', $this->modelSubFolder),
86+
'namespace' => $this->generateNamespace($this->paths['nova'], $this->modelSubFolder),
8787
]);
8888

8989
$this->saveClass('nova', "{$this->model}Resource", $fileContent, $this->modelSubFolder);
@@ -179,11 +179,11 @@ protected function getColumnList(string $table, ?string $connectionName = null):
179179
protected function getImports(): array
180180
{
181181
$imports = [
182-
"{$this->getNamespace('models', $this->modelSubFolder)}\\{$this->model}",
182+
"{$this->generateNamespace($this->paths['models'], $this->modelSubFolder)}\\{$this->model}",
183183
];
184184

185185
if (!empty($this->modelSubFolder)) {
186-
$imports[] = "{$this->getNamespace('nova')}\\Resource";
186+
$imports[] = "{$this->generateNamespace($this->paths['nova'])}\\Resource";
187187
}
188188

189189
return $imports;

src/Generators/NovaTestGenerator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function generateTests(): void
6969
$resourceClass = Str::afterLast($this->novaResourceClassName, '\\');
7070

7171
$fileContent = $this->getStub('nova_test', [
72-
'entity_namespace' => $this->getNamespace('models', $this->modelSubFolder),
72+
'entity_namespace' => $this->generateNamespace($this->paths['models'], $this->modelSubFolder),
7373
'entity' => $this->model,
7474
'resource_name' => $resourceClass,
7575
'resource_namespace' => $this->novaResourceClassName,
@@ -78,7 +78,7 @@ public function generateTests(): void
7878
'lower_entities' => $this->getPluralName(Str::snake($this->model)),
7979
'actions' => $actions,
8080
'filters' => $filters,
81-
'models_namespace' => $this->getNamespace('models'),
81+
'models_namespace' => $this->generateNamespace($this->paths['models']),
8282
]);
8383

8484
$this->saveClass('tests', "Nova{$this->model}ResourceTest", $fileContent);

src/Generators/RepositoryGenerator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ public function generate(): void
2626

2727
$repositoryContent = $this->getStub('repository', [
2828
'entity' => $this->model,
29-
'namespace' => $this->getNamespace('repositories'),
30-
'modelNamespace' => $this->getNamespace('models', $this->modelSubFolder)
29+
'namespace' => $this->generateNamespace($this->paths['repositories']),
30+
'modelNamespace' => $this->generateNamespace($this->paths['models'], $this->modelSubFolder)
3131
]);
3232

3333
$this->saveClass('repositories', "{$this->model}Repository", $repositoryContent);

src/Generators/RequestsGenerator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ protected function createRequest($method, $needToValidate = true, $parameters =
7373
'parameters' => $parameters,
7474
'needToValidate' => $needToValidate,
7575
'requestsFolder' => $requestsFolder,
76-
'namespace' => $this->getNamespace('requests'),
77-
'servicesNamespace' => $this->getNamespace('services'),
76+
'namespace' => $this->generateNamespace($this->paths['requests']),
77+
'servicesNamespace' => $this->generateNamespace($this->paths['services']),
7878
'entityNamespace' => $this->getModelClass($this->model),
7979
'needToValidateWith' => !is_null(Arr::first($parameters, fn ($parameter) => $parameter['name'] === 'with.*')),
8080
]);

src/Generators/ResourceGenerator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function generateCollectionResource(): void
3434
$collectionResourceContent = $this->getStub('collection_resource', [
3535
'singular_name' => $this->model,
3636
'plural_name' => $pluralName,
37-
'namespace' => $this->getNamespace('resources')
37+
'namespace' => $this->generateNamespace($this->paths['resources']),
3838
]);
3939

4040
$this->saveClass('resources', "{$pluralName}CollectionResource", $collectionResourceContent, $this->model);
@@ -52,8 +52,8 @@ public function generateResource(): void
5252

5353
$resourceContent = $this->getStub('resource', [
5454
'entity' => $this->model,
55-
'namespace' => $this->getNamespace('resources'),
56-
'model_namespace' => $this->getNamespace('models', $this->modelSubFolder),
55+
'namespace' => $this->generateNamespace($this->paths['resources']),
56+
'model_namespace' => $this->generateNamespace($this->paths['models'], $this->modelSubFolder),
5757
'fields' => when($this->fields, fn () => Arr::flatten($this->fields)),
5858
]);
5959

0 commit comments

Comments
 (0)