Skip to content

Commit 827f406

Browse files
committed
Merge remote-tracking branch 'origin/master' into bugfix-entity-generation-overwrite
2 parents b145dcc + 219d2b1 commit 827f406

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
@@ -40,10 +40,10 @@ protected function getControllerContent($model): string
4040
return $this->getStub('controller', [
4141
'entity' => $model,
4242
'requestsFolder' => $model,
43-
'namespace' => $this->getNamespace('controllers'),
44-
'requestsNamespace' => $this->getNamespace('requests'),
45-
'resourcesNamespace' => $this->getNamespace('resources'),
46-
'servicesNamespace' => $this->getNamespace('services'),
43+
'namespace' => $this->generateNamespace($this->paths['controllers']),
44+
'requestsNamespace' => $this->generateNamespace($this->paths['requests']),
45+
'resourcesNamespace' => $this->generateNamespace($this->paths['resources']),
46+
'servicesNamespace' => $this->generateNamespace($this->paths['services']),
4747
]);
4848
}
4949

@@ -93,7 +93,7 @@ protected function addUseController(string $routesPath): void
9393
$routesFileContent = file_get_contents($routesPath);
9494

9595
$stub = $this->getStub('use_routes', [
96-
'namespace' => $this->getNamespace('controllers'),
96+
'namespace' => $this->generateNamespace($this->paths['controllers']),
9797
'entity' => $this->model
9898
]);
9999

src/Generators/EntityGenerator.php

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ abstract class EntityGenerator
3333
'database_seeder' => 'database/seeders',
3434
'tests' => 'tests',
3535
'routes' => 'routes',
36+
'translations' => 'lang/en',
3637
];
3738

3839
protected $paths = [];
@@ -89,11 +90,13 @@ public function setRelations(RelationsDTO $relations): self
8990
public function __construct()
9091
{
9192
$this->paths = config('entity-generator.paths');
93+
94+
$this->checkConfigHasCorrectPaths();
9295
}
9396

94-
protected function getNamespace(string $configPath, ?string $subFolder = null): string
97+
protected function generateNamespace(string $path, ?string $additionalSubFolder = null): string
9598
{
96-
$pathParts = $this->getNamespacePathParts($configPath, $subFolder);
99+
$pathParts = $this->getNamespacePathParts($path, $additionalSubFolder);
97100

98101
$namespace = array_map(fn (string $part) => ucfirst($part), $pathParts);
99102

@@ -102,7 +105,7 @@ protected function getNamespace(string $configPath, ?string $subFolder = null):
102105

103106
protected function createNamespace(string $configPath, ?string $subFolder = null): void
104107
{
105-
$path = $this->getPath($configPath, $subFolder);
108+
$path = $this->getPath($this->paths[$configPath], $subFolder);
106109

107110
$fullPath = base_path($path);
108111

@@ -111,26 +114,20 @@ protected function createNamespace(string $configPath, ?string $subFolder = null
111114
}
112115
}
113116

114-
protected function getNamespacePathParts(string $configPath, ?string $subFolder = null): array
117+
protected function getNamespacePathParts(string $path, ?string $additionalSubFolder = null): array
115118
{
116-
$pathParts = explode('/', $this->getPath($configPath, $subFolder));
119+
$pathParts = explode('/', $this->getPath($path, $additionalSubFolder));
117120

118121
if (Str::endsWith(Arr::last($pathParts), '.php')) {
119122
array_pop($pathParts);
120123
}
121124

122-
foreach ($pathParts as $part) {
123-
if (!$this->isFolderHasCorrectCase($part, $configPath)) {
124-
throw new IncorrectClassPathException("Incorrect path to {$configPath}, {$part} folder must start with a capital letter, please specify the path according to the PSR.");
125-
}
126-
}
127-
128125
return $pathParts;
129126
}
130127

131-
protected function getPath(string $configPath, ?string $subFolder = null): string
128+
protected function getPath(string $path, ?string $subFolder = null): string
132129
{
133-
return when($subFolder, fn () => Str::finish($this->paths[$configPath], '/') . $subFolder, $this->paths[$configPath]);
130+
return when($subFolder, fn () => Str::finish($path, '/') . $subFolder, $path);
134131
}
135132

136133
protected function isFolderHasCorrectCase(string $folder, string $configPath): bool
@@ -155,7 +152,7 @@ protected function classExists(string $path, string $name, ?string $subFolder =
155152

156153
protected function getClassPath(string $path, string $name, ?string $subFolder = null): string
157154
{
158-
$path = $this->getPath($path, $subFolder);
155+
$path = $this->getPath($this->paths[$path], $subFolder);
159156

160157
return "{$path}/{$name}.php";
161158
}
@@ -270,7 +267,7 @@ protected function getModelClass(string $model): string
270267
{
271268
$subfolder = when($model === $this->model, $this->modelSubFolder);
272269

273-
$modelNamespace = $this->getNamespace('models', $subfolder);
270+
$modelNamespace = $this->generateNamespace($this->paths['models'], $subfolder);
274271

275272
return "{$modelNamespace}\\{$model}";
276273
}
@@ -310,6 +307,19 @@ protected function pathToNamespace(string $name): string
310307
return ucwords(Str::replace('/', '\\', $name), '\\');
311308
}
312309

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+
313323
protected function checkResourceExists(string $path, string $resourceName, ?string $subFolder = null): void
314324
{
315325
if ($this->classExists($path, $resourceName, $subFolder)) {

src/Generators/FactoryGenerator.php

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

4646
$factoryContent = $this->getStub('factory', [
47-
'namespace' => $this->getNamespace('factories'),
47+
'namespace' => $this->generateNamespace($this->paths['factories']),
4848
'entity' => $this->model,
4949
'fields' => $this->prepareFields(),
50-
'modelNamespace' => $this->getNamespace('models', $this->modelSubFolder),
50+
'modelNamespace' => $this->generateNamespace($this->paths['models'], $this->modelSubFolder),
5151
]);
5252

5353
$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
@@ -42,7 +42,7 @@ protected function getNewModelContent(): string
4242
'fields' => Arr::collapse($this->fields),
4343
'relations' => $this->prepareRelations(),
4444
'casts' => $this->getCasts($this->fields),
45-
'namespace' => $this->getNamespace('models', $this->modelSubFolder),
45+
'namespace' => $this->generateNamespace($this->paths['models'], $this->modelSubFolder),
4646
'importRelations' => $this->getImportedRelations(),
4747
'anotationProperties' => $this->generateAnnotationProperties($this->fields),
4848
'hasCarbonField' => !empty($this->fields['timestamp']) || !empty($this->fields['timestamp-required']),
@@ -184,7 +184,7 @@ protected function shouldImportRelation(string $relation): bool
184184

185185
protected function generateClassNamespace(string $className, ?string $folder = null): string
186186
{
187-
$path = $this->getNamespace('models', $folder);
187+
$path = $this->generateNamespace($this->paths['models'], $folder);
188188
$psrPath = $this->pathToNamespace($className);
189189

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

src/Generators/NovaResourceGenerator.php

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

8484
$this->saveClass('nova', "{$this->model}Resource", $fileContent, $this->modelSubFolder);
@@ -174,11 +174,11 @@ protected function getColumnList(string $table, ?string $connectionName = null):
174174
protected function getImports(): array
175175
{
176176
$imports = [
177-
"{$this->getNamespace('models', $this->modelSubFolder)}\\{$this->model}",
177+
"{$this->generateNamespace($this->paths['models'], $this->modelSubFolder)}\\{$this->model}",
178178
];
179179

180180
if (!empty($this->modelSubFolder)) {
181-
$imports[] = "{$this->getNamespace('nova')}\\Resource";
181+
$imports[] = "{$this->generateNamespace($this->paths['nova'])}\\Resource";
182182
}
183183

184184
return $imports;

src/Generators/NovaTestGenerator.php

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

6565
$fileContent = $this->getStub('nova_test', [
66-
'entity_namespace' => $this->getNamespace('models', $this->modelSubFolder),
66+
'entity_namespace' => $this->generateNamespace($this->paths['models'], $this->modelSubFolder),
6767
'entity' => $this->model,
6868
'resource_name' => $resourceClass,
6969
'resource_namespace' => $this->novaResourceClassName,
@@ -72,7 +72,7 @@ public function generateTests(): void
7272
'lower_entities' => $this->getPluralName(Str::snake($this->model)),
7373
'actions' => $actions,
7474
'filters' => $filters,
75-
'models_namespace' => $this->getNamespace('models'),
75+
'models_namespace' => $this->generateNamespace($this->paths['models']),
7676
]);
7777

7878
$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
@@ -28,8 +28,8 @@ public function generate(): void
2828

2929
$repositoryContent = $this->getStub('repository', [
3030
'entity' => $this->model,
31-
'namespace' => $this->getNamespace('repositories'),
32-
'modelNamespace' => $this->getNamespace('models', $this->modelSubFolder)
31+
'namespace' => $this->generateNamespace($this->paths['repositories']),
32+
'modelNamespace' => $this->generateNamespace($this->paths['models'], $this->modelSubFolder)
3333
]);
3434

3535
$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
@@ -75,8 +75,8 @@ protected function createRequest($method, $needToValidate = true, $parameters =
7575
'parameters' => $parameters,
7676
'needToValidate' => $needToValidate,
7777
'requestsFolder' => $requestsFolder,
78-
'namespace' => $this->getNamespace('requests'),
79-
'servicesNamespace' => $this->getNamespace('services'),
78+
'namespace' => $this->generateNamespace($this->paths['requests']),
79+
'servicesNamespace' => $this->generateNamespace($this->paths['services']),
8080
'entityNamespace' => $this->getModelClass($this->model),
8181
'needToValidateWith' => !is_null(Arr::first($parameters, fn ($parameter) => $parameter['name'] === 'with.*')),
8282
]);

src/Generators/ResourceGenerator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function generateCollectionResource(): void
2929
$collectionResourceContent = $this->getStub('collection_resource', [
3030
'singular_name' => $this->model,
3131
'plural_name' => $pluralName,
32-
'namespace' => $this->getNamespace('resources')
32+
'namespace' => $this->generateNamespace($this->paths['resources']),
3333
]);
3434

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

4444
$resourceContent = $this->getStub('resource', [
4545
'entity' => $this->model,
46-
'namespace' => $this->getNamespace('resources'),
47-
'model_namespace' => $this->getNamespace('models', $this->modelSubFolder),
46+
'namespace' => $this->generateNamespace($this->paths['resources']),
47+
'model_namespace' => $this->generateNamespace($this->paths['models'], $this->modelSubFolder),
4848
'fields' => when($this->fields, fn () => Arr::flatten($this->fields)),
4949
]);
5050

0 commit comments

Comments
 (0)