Skip to content

Commit 0cd7937

Browse files
author
Ni Nelli
committed
chore: fix merge conflicts
refs: #204
2 parents 4456144 + 55a07ef commit 0cd7937

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+495
-158
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: 47 additions & 25 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
@@ -145,16 +143,18 @@ abstract public function generate(): void;
145143

146144
protected function classExists(string $path, string $name, ?string $subFolder = null): bool
147145
{
148-
$classPath = $this->getClassPath($path, $name, $subFolder);
146+
$relativePath = $this->getClassPath($path, $name, $subFolder);
147+
148+
$absolutePath = base_path($relativePath);
149149

150-
return file_exists($classPath);
150+
return file_exists($absolutePath);
151151
}
152152

153153
protected function getClassPath(string $path, string $name, ?string $subFolder = null): string
154154
{
155-
$path = $this->getPath($path, $subFolder);
155+
$path = $this->getPath($this->paths[$path], $subFolder);
156156

157-
return base_path("{$path}/{$name}.php");
157+
return "{$path}/{$name}.php";
158158
}
159159

160160
protected function saveClass($path, $name, $content, ?string $entityFolder = null): string
@@ -267,7 +267,7 @@ protected function getModelClass(string $model): string
267267
{
268268
$subfolder = when($model === $this->model, $this->modelSubFolder);
269269

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

272272
return "{$modelNamespace}\\{$model}";
273273
}
@@ -306,4 +306,26 @@ protected function pathToNamespace(string $name): string
306306
{
307307
return ucwords(Str::replace('/', '\\', $name), '\\');
308308
}
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+
}
309331
}

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);

0 commit comments

Comments
 (0)