Skip to content

Commit de8bfff

Browse files
authored
Merge pull request #193 from RonasIT/189_getOrCreateNamespace-method-refactoring
feat: getOrCreateNamespace method refactoring
2 parents 6efd417 + 970f657 commit de8bfff

13 files changed

+76
-55
lines changed

src/Generators/AbstractTestsGenerator.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ abstract class AbstractTestsGenerator extends EntityGenerator
2525

2626
public function generate(): void
2727
{
28+
$this->createNamespace('factories');
29+
2830
if ($this->canGenerateUserData()) {
2931
$this->withAuth = true;
3032
}
@@ -173,7 +175,7 @@ protected function getMockModel($model): array
173175
return [];
174176
}
175177

176-
$factoryNamespace = "{$this->getOrCreateNamespace('factories')}\\{$model}Factory";
178+
$factoryNamespace = "{$this->getNamespace('factories')}\\{$model}Factory";
177179
$factory = $factoryNamespace::new();
178180

179181
return $factory

src/Generators/ControllerGenerator.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public function generate(): void
3131
return;
3232
}
3333

34+
$this->createNamespace('controllers');
35+
3436
$controllerContent = $this->getControllerContent($this->model);
3537

3638
$this->saveClass('controllers', "{$this->model}Controller", $controllerContent);
@@ -45,10 +47,10 @@ protected function getControllerContent($model): string
4547
return $this->getStub('controller', [
4648
'entity' => $model,
4749
'requestsFolder' => $model,
48-
'namespace' => $this->getOrCreateNamespace('controllers'),
49-
'requestsNamespace' => $this->getOrCreateNamespace('requests'),
50-
'resourcesNamespace' => $this->getOrCreateNamespace('resources'),
51-
'servicesNamespace' => $this->getOrCreateNamespace('services'),
50+
'namespace' => $this->getNamespace('controllers'),
51+
'requestsNamespace' => $this->getNamespace('requests'),
52+
'resourcesNamespace' => $this->getNamespace('resources'),
53+
'servicesNamespace' => $this->getNamespace('services'),
5254
]);
5355
}
5456

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

100102
$stub = $this->getStub('use_routes', [
101-
'namespace' => $this->getOrCreateNamespace('controllers'),
103+
'namespace' => $this->getNamespace('controllers'),
102104
'entity' => $this->model
103105
]);
104106

src/Generators/EntityGenerator.php

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,29 @@ public function __construct()
9090
$this->paths = config('entity-generator.paths');
9191
}
9292

93-
protected function getOrCreateNamespace(string $configPath, ?string $subFolder = null): string
93+
protected function getNamespace(string $configPath, ?string $subFolder = null): string
9494
{
95-
$path = when($subFolder, fn () => Str::finish($this->paths[$configPath], '/') . $subFolder, $this->paths[$configPath]);
96-
97-
$pathParts = explode('/', $path);
95+
$pathParts = $this->getNamespacePathParts($configPath, $subFolder);
96+
97+
$namespace = array_map(fn (string $part) => ucfirst($part), $pathParts);
98+
99+
return implode('\\', $namespace);
100+
}
101+
102+
protected function createNamespace(string $configPath, ?string $subFolder = null): void
103+
{
104+
$path = $this->getPath($configPath, $subFolder);
105+
106+
$fullPath = base_path($path);
107+
108+
if (!file_exists($fullPath)) {
109+
mkdir($fullPath, 0777, true);
110+
}
111+
}
112+
113+
protected function getNamespacePathParts(string $configPath, ?string $subFolder = null): array
114+
{
115+
$pathParts = explode('/', $this->getPath($configPath, $subFolder));
98116

99117
if (Str::endsWith(Arr::last($pathParts), '.php')) {
100118
array_pop($pathParts);
@@ -106,17 +124,12 @@ protected function getOrCreateNamespace(string $configPath, ?string $subFolder =
106124
}
107125
}
108126

109-
$namespace = array_map(function (string $part) {
110-
return ucfirst($part);
111-
}, $pathParts);
112-
113-
$fullPath = base_path($path);
114-
115-
if (!file_exists($fullPath)) {
116-
mkdir($fullPath, 0777, true);
117-
}
127+
return $pathParts;
128+
}
118129

119-
return implode('\\', $namespace);
130+
protected function getPath(string $configPath, ?string $subFolder = null): string
131+
{
132+
return when($subFolder, fn () => Str::finish($this->paths[$configPath], '/') . $subFolder, $this->paths[$configPath]);
120133
}
121134

122135
protected function isFolderHasCorrectCase(string $folder, string $configPath): bool
@@ -239,7 +252,7 @@ protected function getRelatedModels(string $model, string $creatableClass): arra
239252
protected function generateRelativePath(string $namespace, string $basePath): string
240253
{
241254
return Str::after(
242-
subject: $this->namespaceToPath($namespace),
255+
subject: $this->namespaceToPath($namespace),
243256
search: $this->namespaceToPath($basePath) . '/',
244257
);
245258
}
@@ -253,7 +266,7 @@ protected function getModelClass(string $model): string
253266
{
254267
$subfolder = when($model === $this->model, $this->modelSubFolder);
255268

256-
$modelNamespace = $this->getOrCreateNamespace('models', $subfolder);
269+
$modelNamespace = $this->getNamespace('models', $subfolder);
257270

258271
return "{$modelNamespace}\\{$model}";
259272
}

src/Generators/FactoryGenerator.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,13 @@ public function generate(): void
4848
return;
4949
}
5050

51+
$this->createNamespace('factories');
52+
5153
$factoryContent = $this->getStub('factory', [
52-
'namespace' => $this->getOrCreateNamespace('factories'),
54+
'namespace' => $this->getNamespace('factories'),
5355
'entity' => $this->model,
5456
'fields' => $this->prepareFields(),
55-
'modelNamespace' => $this->getOrCreateNamespace('models', $this->modelSubFolder),
57+
'modelNamespace' => $this->getNamespace('models', $this->modelSubFolder),
5658
]);
5759

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

src/Generators/ModelGenerator.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ public function generate(): void
2727
}
2828

2929
if ($this->isStubExists('model') && (!$this->hasRelations() || $this->isStubExists('relation', 'model'))) {
30+
$this->createNamespace('models', $this->modelSubFolder);
31+
3032
$this->prepareRelatedModels();
3133
$modelContent = $this->getNewModelContent();
3234

@@ -48,7 +50,7 @@ protected function getNewModelContent(): string
4850
'fields' => Arr::collapse($this->fields),
4951
'relations' => $this->prepareRelations(),
5052
'casts' => $this->getCasts($this->fields),
51-
'namespace' => $this->getOrCreateNamespace('models', $this->modelSubFolder),
53+
'namespace' => $this->getNamespace('models', $this->modelSubFolder),
5254
'importRelations' => $this->getImportedRelations(),
5355
'anotationProperties' => $this->generateAnnotationProperties($this->fields),
5456
'hasCarbonField' => !empty($this->fields['timestamp']) || !empty($this->fields['timestamp-required']),
@@ -190,7 +192,7 @@ protected function shouldImportRelation(string $relation): bool
190192

191193
protected function generateClassNamespace(string $className, ?string $folder = null): string
192194
{
193-
$path = $this->getOrCreateNamespace('models', $folder);
195+
$path = $this->getNamespace('models', $folder);
194196
$psrPath = Str::replace('/', '\\', $className);
195197

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

src/Generators/NovaResourceGenerator.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,16 @@ public function generate(): void
7777
return;
7878
}
7979

80+
$this->createNamespace('nova');
81+
8082
$novaFields = $this->prepareNovaFields();
8183

8284
$fileContent = $this->getStub('nova_resource', [
8385
'model' => $this->model,
8486
'fields' => $novaFields,
8587
'types' => array_unique(data_get($novaFields, '*.type')),
86-
'modelNamespace' => $this->getOrCreateNamespace('models', $this->modelSubFolder),
87-
'namespace' => $this->getOrCreateNamespace('nova')
88+
'modelNamespace' => $this->getNamespace('models', $this->modelSubFolder),
89+
'namespace' => $this->getNamespace('nova')
8890
]);
8991

9092
$this->saveClass('nova', "{$this->model}Resource", $fileContent, $this->modelSubFolder);

src/Generators/NovaTestGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function generateTests(): void
5050

5151
$fileContent = $this->getStub('nova_test', [
5252
'url_path' => Str::kebab($this->model) . '-resources',
53-
'entity_namespace' => $this->getOrCreateNamespace('models', $this->modelSubFolder),
53+
'entity_namespace' => $this->getNamespace('models', $this->modelSubFolder),
5454
'entity' => $this->model,
5555
'entities' => $this->getPluralName($this->model),
5656
'snake_entity' => Str::snake($this->model),

src/Generators/RepositoryGenerator.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ public function generate(): void
2222
return;
2323
}
2424

25+
$this->createNamespace('repositories');
26+
2527
$repositoryContent = $this->getStub('repository', [
2628
'entity' => $this->model,
27-
'namespace' => $this->getOrCreateNamespace('repositories'),
28-
'modelNamespace' => $this->getOrCreateNamespace('models', $this->modelSubFolder)
29+
'namespace' => $this->getNamespace('repositories'),
30+
'modelNamespace' => $this->getNamespace('models', $this->modelSubFolder)
2931
]);
3032

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

src/Generators/RequestsGenerator.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public function generate(): void
2222
return;
2323
}
2424

25+
$this->createNamespace('requests');
26+
2527
$this->relationFields = array_map(function ($field) {
2628
return Str::snake($field) . '_id';
2729
}, $this->relations->belongsTo);
@@ -71,8 +73,8 @@ protected function createRequest($method, $needToValidate = true, $parameters =
7173
'parameters' => $parameters,
7274
'needToValidate' => $needToValidate,
7375
'requestsFolder' => $requestsFolder,
74-
'namespace' => $this->getOrCreateNamespace('requests'),
75-
'servicesNamespace' => $this->getOrCreateNamespace('services')
76+
'namespace' => $this->getNamespace('requests'),
77+
'servicesNamespace' => $this->getNamespace('services')
7678
]);
7779

7880
$this->saveClass('requests', "{$method}{$modelName}Request",

src/Generators/ResourceGenerator.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ class ResourceGenerator extends EntityGenerator
1010
public function generate(): void
1111
{
1212
if ($this->isStubExists('resource')) {
13+
$this->createNamespace('resources');
14+
1315
$this->generateResource();
1416

1517
if ($this->isStubExists('collection_resource')) {
@@ -33,7 +35,7 @@ public function generateCollectionResource(): void
3335
$collectionResourceContent = $this->getStub('collection_resource', [
3436
'singular_name' => $this->model,
3537
'plural_name' => $pluralName,
36-
'namespace' => $this->getOrCreateNamespace('resources')
38+
'namespace' => $this->getNamespace('resources')
3739
]);
3840

3941
$this->saveClass('resources', "{$pluralName}CollectionResource", $collectionResourceContent, $this->model);
@@ -53,8 +55,8 @@ public function generateResource(): void
5355

5456
$resourceContent = $this->getStub('resource', [
5557
'entity' => $this->model,
56-
'namespace' => $this->getOrCreateNamespace('resources'),
57-
'model_namespace' => $this->getOrCreateNamespace('models', $this->modelSubFolder),
58+
'namespace' => $this->getNamespace('resources'),
59+
'model_namespace' => $this->getNamespace('models', $this->modelSubFolder),
5860
]);
5961

6062
$this->saveClass('resources', "{$this->model}Resource", $resourceContent, $this->model);

0 commit comments

Comments
 (0)