Skip to content

Commit 569c284

Browse files
authored
Merge branch 'master' into 203-relationships-annotation
2 parents 21dd8aa + bd9e489 commit 569c284

22 files changed

+289
-120
lines changed

src/Commands/MakeEntityCommand.php

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ public function handle(): void
124124
$this->checkConfigs();
125125
$this->listenEvents();
126126
$this->parseRelations();
127+
$this->entityName = $this->convertToPascalCase($this->entityName);
127128

128129
try {
129130
$this->generate();
@@ -238,19 +239,20 @@ protected function getCrudOptions(): array
238239
protected function parseRelations(): void
239240
{
240241
$this->relations = new RelationsDTO(
241-
hasOne: $this->trimRelations($this->option('has-one')),
242-
hasMany: $this->trimRelations($this->option('has-many')),
243-
belongsTo: $this->trimRelations($this->option('belongs-to')),
244-
belongsToMany: $this->trimRelations($this->option('belongs-to-many')),
242+
hasOne: $this->prepareRelations($this->option('has-one')),
243+
hasMany: $this->prepareRelations($this->option('has-many')),
244+
belongsTo: $this->prepareRelations($this->option('belongs-to')),
245+
belongsToMany: $this->prepareRelations($this->option('belongs-to-many')),
245246
);
246247
}
247248

248-
protected function trimRelations(array $relations): array
249+
protected function prepareRelations(array $relations): array
249250
{
250-
return array_map(
251-
callback: fn ($relation) => Str::trim($relation, '/'),
252-
array: $relations,
253-
);
251+
return array_map(function ($relation) {
252+
$relation = Str::trim($relation, '/');
253+
254+
return $this->convertToPascalCase($relation);
255+
}, $relations);
254256
}
255257

256258
protected function getFields(): array
@@ -305,4 +307,15 @@ protected function listenEvents(): void
305307
listener: fn (WarningEvent $event) => $this->warn($event->message),
306308
);
307309
}
310+
311+
protected function convertToPascalCase(string $entityName): string
312+
{
313+
$pascalEntityName = Str::studly($entityName);
314+
315+
if ($entityName !== $pascalEntityName) {
316+
$this->info("{$entityName} was converted to {$pascalEntityName}");
317+
}
318+
319+
return $pascalEntityName;
320+
}
308321
}

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: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,12 @@
55
use Illuminate\Contracts\Filesystem\FileNotFoundException;
66
use RonasIT\Support\Exceptions\ClassNotExistsException;
77
use RonasIT\Support\Events\SuccessCreateMessage;
8-
use RonasIT\Support\Exceptions\ResourceAlreadyExistsException;
98

109
class ControllerGenerator extends EntityGenerator
1110
{
1211
public function generate(): void
1312
{
14-
if ($this->classExists('controllers', "{$this->model}Controller")) {
15-
$path = $this->getClassPath('controllers', "{$this->model}Controller");
16-
17-
throw new ResourceAlreadyExistsException($path);
18-
}
13+
$this->checkResourceExists('controllers', "{$this->model}Controller");
1914

2015
if (!$this->classExists('services', "{$this->model}Service")) {
2116
$this->throwFailureException(
@@ -45,10 +40,10 @@ protected function getControllerContent($model): string
4540
return $this->getStub('controller', [
4641
'entity' => $model,
4742
'requestsFolder' => $model,
48-
'namespace' => $this->getNamespace('controllers'),
49-
'requestsNamespace' => $this->getNamespace('requests'),
50-
'resourcesNamespace' => $this->getNamespace('resources'),
51-
'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']),
5247
]);
5348
}
5449

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

10095
$stub = $this->getStub('use_routes', [
101-
'namespace' => $this->getNamespace('controllers'),
96+
'namespace' => $this->generateNamespace($this->paths['controllers']),
10297
'entity' => $this->model
10398
]);
10499

src/Generators/EntityGenerator.php

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@
22

33
namespace RonasIT\Support\Generators;
44

5-
use Illuminate\Database\Eloquent\Relations\BelongsTo;
6-
use Illuminate\Filesystem\Filesystem;
5+
use Throwable;
6+
use ReflectionClass;
7+
use ReflectionMethod;
78
use Illuminate\Support\Arr;
8-
use Illuminate\Support\Facades\DB;
99
use Illuminate\Support\Str;
10+
use Illuminate\Support\Facades\DB;
11+
use Illuminate\Filesystem\Filesystem;
1012
use RonasIT\Support\DTO\RelationsDTO;
1113
use RonasIT\Support\Events\WarningEvent;
14+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
1215
use RonasIT\Support\Exceptions\ClassNotExistsException;
1316
use RonasIT\Support\Exceptions\IncorrectClassPathException;
14-
use Throwable;
15-
use ReflectionMethod;
16-
use ReflectionClass;
17+
use RonasIT\Support\Exceptions\ResourceAlreadyExistsException;
1718

1819
/**
1920
* @property Filesystem $fs
@@ -32,6 +33,7 @@ abstract class EntityGenerator
3233
'database_seeder' => 'database/seeders',
3334
'tests' => 'tests',
3435
'routes' => 'routes',
36+
'translations' => 'lang/en',
3537
];
3638

3739
protected $paths = [];
@@ -51,7 +53,7 @@ public function setCrudOptions(array $crudOptions): self
5153

5254
public function setModel(string $model): self
5355
{
54-
$this->model = Str::studly($model);
56+
$this->model = $model;
5557

5658
return $this;
5759
}
@@ -88,11 +90,13 @@ public function setRelations(RelationsDTO $relations): self
8890
public function __construct()
8991
{
9092
$this->paths = config('entity-generator.paths');
93+
94+
$this->checkConfigHasCorrectPaths();
9195
}
9296

93-
protected function getNamespace(string $configPath, ?string $subFolder = null): string
97+
protected function generateNamespace(string $path, ?string $additionalSubFolder = null): string
9498
{
95-
$pathParts = $this->getNamespacePathParts($configPath, $subFolder);
99+
$pathParts = $this->getNamespacePathParts($path, $additionalSubFolder);
96100

97101
$namespace = array_map(fn (string $part) => ucfirst($part), $pathParts);
98102

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

102106
protected function createNamespace(string $configPath, ?string $subFolder = null): void
103107
{
104-
$path = $this->getPath($configPath, $subFolder);
108+
$path = $this->getPath($this->paths[$configPath], $subFolder);
105109

106110
$fullPath = base_path($path);
107111

@@ -110,26 +114,20 @@ protected function createNamespace(string $configPath, ?string $subFolder = null
110114
}
111115
}
112116

113-
protected function getNamespacePathParts(string $configPath, ?string $subFolder = null): array
117+
protected function getNamespacePathParts(string $path, ?string $additionalSubFolder = null): array
114118
{
115-
$pathParts = explode('/', $this->getPath($configPath, $subFolder));
119+
$pathParts = explode('/', $this->getPath($path, $additionalSubFolder));
116120

117121
if (Str::endsWith(Arr::last($pathParts), '.php')) {
118122
array_pop($pathParts);
119123
}
120124

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-
127125
return $pathParts;
128126
}
129127

130-
protected function getPath(string $configPath, ?string $subFolder = null): string
128+
protected function getPath(string $path, ?string $subFolder = null): string
131129
{
132-
return when($subFolder, fn () => Str::finish($this->paths[$configPath], '/') . $subFolder, $this->paths[$configPath]);
130+
return when($subFolder, fn () => Str::finish($path, '/') . $subFolder, $path);
133131
}
134132

135133
protected function isFolderHasCorrectCase(string $folder, string $configPath): bool
@@ -154,7 +152,7 @@ protected function classExists(string $path, string $name, ?string $subFolder =
154152

155153
protected function getClassPath(string $path, string $name, ?string $subFolder = null): string
156154
{
157-
$path = $this->getPath($path, $subFolder);
155+
$path = $this->getPath($this->paths[$path], $subFolder);
158156

159157
return "{$path}/{$name}.php";
160158
}
@@ -269,7 +267,7 @@ protected function getModelClass(string $model): string
269267
{
270268
$subfolder = when($model === $this->model, $this->modelSubFolder);
271269

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

274272
return "{$modelNamespace}\\{$model}";
275273
}
@@ -308,4 +306,26 @@ protected function pathToNamespace(string $name): string
308306
{
309307
return ucwords(Str::replace('/', '\\', $name), '\\');
310308
}
309+
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+
323+
protected function checkResourceExists(string $path, string $resourceName, ?string $subFolder = null): void
324+
{
325+
if ($this->classExists($path, $resourceName, $subFolder)) {
326+
$filePath = $this->getClassPath($path, $resourceName, $subFolder);
327+
328+
throw new ResourceAlreadyExistsException($filePath);
329+
}
330+
}
311331
}

src/Generators/FactoryGenerator.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use RonasIT\Support\Exceptions\FakerMethodNotFoundException;
1010
use RonasIT\Support\Exceptions\ClassNotExistsException;
1111
use RonasIT\Support\Events\SuccessCreateMessage;
12-
use RonasIT\Support\Exceptions\ResourceAlreadyExistsException;
1312

1413
class FactoryGenerator extends EntityGenerator
1514
{
@@ -36,11 +35,7 @@ public function generate(): void
3635
);
3736
}
3837

39-
if ($this->classExists('factories', "{$this->model}Factory")) {
40-
$path = $this->getClassPath('factories', "{$this->model}Factory");
41-
42-
throw new ResourceAlreadyExistsException($path);
43-
}
38+
$this->checkResourceExists('factories', "{$this->model}Factory");
4439

4540
if (!$this->isStubExists('factory')) {
4641
return;
@@ -49,10 +44,10 @@ public function generate(): void
4944
$this->createNamespace('factories');
5045

5146
$factoryContent = $this->getStub('factory', [
52-
'namespace' => $this->getNamespace('factories'),
47+
'namespace' => $this->generateNamespace($this->paths['factories']),
5348
'entity' => $this->model,
5449
'fields' => $this->prepareFields(),
55-
'modelNamespace' => $this->getNamespace('models', $this->modelSubFolder),
50+
'modelNamespace' => $this->generateNamespace($this->paths['models'], $this->modelSubFolder),
5651
]);
5752

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

src/Generators/ModelGenerator.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use Illuminate\Support\Str;
77
use RonasIT\Support\Exceptions\ClassNotExistsException;
88
use RonasIT\Support\Events\SuccessCreateMessage;
9-
use RonasIT\Support\Exceptions\ResourceAlreadyExistsException;
109

1110
class ModelGenerator extends EntityGenerator
1211
{
@@ -17,11 +16,7 @@ class ModelGenerator extends EntityGenerator
1716

1817
public function generate(): void
1918
{
20-
if ($this->classExists('models', $this->model, $this->modelSubFolder)) {
21-
$path = $this->getClassPath('models', $this->model, $this->modelSubFolder);
22-
23-
throw new ResourceAlreadyExistsException($path);
24-
}
19+
$this->checkResourceExists('models', $this->model, $this->modelSubFolder);
2520

2621
if ($this->isStubExists('model') && (!$this->hasRelations() || $this->isStubExists('relation', 'model'))) {
2722
$this->createNamespace('models', $this->modelSubFolder);
@@ -49,7 +44,7 @@ protected function getNewModelContent(): string
4944
'fields' => Arr::collapse($this->fields),
5045
'relations' => $relations,
5146
'casts' => $this->getCasts($this->fields),
52-
'namespace' => $this->getNamespace('models', $this->modelSubFolder),
47+
'namespace' => $this->generateNamespace($this->paths['models'], $this->modelSubFolder),
5348
'importRelations' => $this->getImportedRelations(),
5449
'annotationProperties' => $this->generateAnnotationProperties($this->fields, $relations),
5550
'hasCarbonField' => !empty($this->fields['timestamp']) || !empty($this->fields['timestamp-required']),
@@ -198,7 +193,7 @@ protected function shouldImportRelation(string $relation): bool
198193

199194
protected function generateClassNamespace(string $className, ?string $folder = null): string
200195
{
201-
$path = $this->getNamespace('models', $folder);
196+
$path = $this->generateNamespace($this->paths['models'], $folder);
202197
$psrPath = $this->pathToNamespace($className);
203198

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

src/Generators/NovaResourceGenerator.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Laravel\Nova\NovaServiceProvider;
99
use RonasIT\Support\Events\SuccessCreateMessage;
1010
use RonasIT\Support\Exceptions\ClassNotExistsException;
11-
use RonasIT\Support\Exceptions\ResourceAlreadyExistsException;
1211
use RonasIT\Support\Support\CommandLineNovaField;
1312
use RonasIT\Support\Support\DatabaseNovaField;
1413

@@ -64,11 +63,7 @@ public function generate(): void
6463
);
6564
}
6665

67-
if ($this->classExists('nova', "{$this->model}Resource")) {
68-
$path = $this->getClassPath('nova', "{$this->model}Resource", $this->modelSubFolder);
69-
70-
throw new ResourceAlreadyExistsException($path);
71-
}
66+
$this->checkResourceExists('nova', "{$this->model}Resource", $this->modelSubFolder);
7267

7368
if (!$this->isStubExists('nova_resource')) {
7469
return;
@@ -83,7 +78,7 @@ public function generate(): void
8378
'fields' => $novaFields,
8479
'types' => array_unique(data_get($novaFields, '*.type')),
8580
'imports' => $this->getImports(),
86-
'namespace' => $this->getNamespace('nova', $this->modelSubFolder),
81+
'namespace' => $this->generateNamespace($this->paths['nova'], $this->modelSubFolder),
8782
]);
8883

8984
$this->saveClass('nova', "{$this->model}Resource", $fileContent, $this->modelSubFolder);
@@ -179,11 +174,11 @@ protected function getColumnList(string $table, ?string $connectionName = null):
179174
protected function getImports(): array
180175
{
181176
$imports = [
182-
"{$this->getNamespace('models', $this->modelSubFolder)}\\{$this->model}",
177+
"{$this->generateNamespace($this->paths['models'], $this->modelSubFolder)}\\{$this->model}",
183178
];
184179

185180
if (!empty($this->modelSubFolder)) {
186-
$imports[] = "{$this->getNamespace('nova')}\\Resource";
181+
$imports[] = "{$this->generateNamespace($this->paths['nova'])}\\Resource";
187182
}
188183

189184
return $imports;

0 commit comments

Comments
 (0)