Skip to content

Commit f45f792

Browse files
author
Ni Nelli
committed
chore: merge with master
2 parents 381bdf6 + 3e8b8e7 commit f45f792

35 files changed

+157
-244
lines changed

config/entity-generator.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
'relation' => 'entity-generator::relation',
2323
'repository' => 'entity-generator::repository',
2424
'service' => 'entity-generator::service',
25-
'service_with_trait' => 'entity-generator::service_with_trait',
2625
'controller' => 'entity-generator::controller',
2726
'request' => 'entity-generator::request',
2827
'routes' => 'entity-generator::routes',

src/Commands/MakeEntityCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class MakeEntityCommand extends Command
9090
protected $rules = [
9191
'only' => [
9292
'only-api' => [ResourceGenerator::class, ControllerGenerator::class, RequestsGenerator::class, TestsGenerator::class],
93-
'only-entity' => [MigrationGenerator::class, ModelGenerator::class, ServiceGenerator::class, RepositoryGenerator::class, FactoryGenerator::class, SeederGenerator::class],
93+
'only-entity' => [MigrationGenerator::class, ModelGenerator::class, RepositoryGenerator::class, ServiceGenerator::class, FactoryGenerator::class, SeederGenerator::class],
9494
'only-model' => [ModelGenerator::class],
9595
'only-repository' => [RepositoryGenerator::class],
9696
'only-service' => [ServiceGenerator::class],

src/DTO/RelationsDTO.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,4 @@ public function __construct(
1111
public array $belongsToMany = [],
1212
) {
1313
}
14-
15-
public function toArray(): array
16-
{
17-
return get_object_vars($this);
18-
}
1914
}

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: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ abstract class EntityGenerator
3636

3737
protected $paths = [];
3838
protected $model;
39-
protected $modelSubFolder = null;
39+
protected $modelSubFolder = '';
4040
protected $fields;
4141
protected $relations = [];
4242
protected $crudOptions;
@@ -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
}
@@ -276,4 +289,15 @@ protected function isStubExists(string $stubName, ?string $generationType = null
276289

277290
return true;
278291
}
292+
293+
protected function prepareRelations(): array
294+
{
295+
$result = [];
296+
297+
foreach ($this->relations as $relationType => $relations) {
298+
$result[$relationType] = array_map(fn ($relation) => class_basename($relation), $relations);
299+
}
300+
301+
return $result;
302+
}
279303
}

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/MigrationGenerator.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,6 @@ public function generate(): void
3333
event(new SuccessCreateMessage("Created a new Migration: {$entities}_create_table"));
3434
}
3535

36-
protected function prepareRelations(): array
37-
{
38-
$result = [];
39-
40-
foreach ($this->relations->toArray() as $relationType => $relations) {
41-
$result[$relationType] = array_map(fn ($relation) => Str::afterLast($relation, '/'), $relations);
42-
}
43-
44-
return $result;
45-
}
46-
4736
protected function isJson(string $typeName): bool
4837
{
4938
return $typeName === 'json';

src/Generators/ModelGenerator.php

Lines changed: 6 additions & 4 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']),
@@ -111,7 +113,7 @@ protected function insertImport(string &$classContent, string $import): void
111113
}
112114
}
113115

114-
public function prepareRelations(): array
116+
protected function prepareRelations(): array
115117
{
116118
$result = [];
117119

@@ -185,12 +187,12 @@ protected function shouldImportRelation(string $relation): bool
185187
{
186188
$relationNamespace = when(Str::contains($relation, '/'), fn () => Str::beforeLast($relation, '/'), '');
187189

188-
return $relationNamespace != $this->modelSubFolder;
190+
return $relationNamespace !== $this->modelSubFolder;
189191
}
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);

0 commit comments

Comments
 (0)