Skip to content

Commit e0fa9ec

Browse files
committed
Merge branch '140-use-NovaResource-class-in-NovaTestGenerator-instead-of-Model' into 159-ability-to-set-nova-resource-for-nova-tests-generator
2 parents 8c14393 + 5b6c000 commit e0fa9ec

18 files changed

+151
-122
lines changed

src/Enums/ResourceTypeEnum.php

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/Exceptions/ResourceAlreadyExistsException.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,22 @@
33
namespace RonasIT\Support\Exceptions;
44

55
use Exception;
6-
use RonasIT\Support\Enums\ResourceTypeEnum;
6+
use Illuminate\Support\Str;
77

88
class ResourceAlreadyExistsException extends Exception
99
{
1010
public function __construct(
11-
protected string $entityName,
12-
protected ResourceTypeEnum $resourceType,
13-
protected ?string $entityNamespace = null,
11+
protected string $filePath,
1412
) {
15-
parent::__construct("Cannot create {$entityNamespace}{$resourceType->value} cause it already exists. Remove {$entityName}{$resourceType->value} and run command again.");
13+
$entity = $this->getEntity();
14+
15+
parent::__construct("Cannot create {$entity} cause it already exists. Remove {$this->filePath} and run command again.");
16+
}
17+
18+
protected function getEntity(): string
19+
{
20+
$fileName = Str::afterLast($this->filePath, '/');
21+
22+
return Str::before($fileName, '.php');
1623
}
17-
}
24+
}

src/Generators/ControllerGenerator.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,18 @@
33
namespace RonasIT\Support\Generators;
44

55
use Illuminate\Contracts\Filesystem\FileNotFoundException;
6-
use RonasIT\Support\Exceptions\ClassAlreadyExistsException;
76
use RonasIT\Support\Exceptions\ClassNotExistsException;
87
use RonasIT\Support\Events\SuccessCreateMessage;
8+
use RonasIT\Support\Exceptions\ResourceAlreadyExistsException;
99

1010
class ControllerGenerator extends EntityGenerator
1111
{
1212
public function generate(): void
1313
{
1414
if ($this->classExists('controllers', "{$this->model}Controller")) {
15-
$this->throwFailureException(
16-
ClassAlreadyExistsException::class,
17-
"Cannot create {$this->model}Controller cause {$this->model}Controller already exists.",
18-
"Remove {$this->model}Controller.",
19-
);
15+
$path = $this->getClassPath('controllers', "{$this->model}Controller");
16+
17+
throw new ResourceAlreadyExistsException($path);
2018
}
2119

2220
if (!$this->classExists('services', "{$this->model}Service")) {

src/Generators/EntityGenerator.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,16 @@ abstract public function generate(): void;
150150

151151
protected function classExists(string $path, string $name, ?string $subFolder = null): bool
152152
{
153-
$entitiesPath = $this->paths[$path];
153+
$classPath = $this->getClassPath($path, $name, $subFolder);
154154

155-
if (!empty($subFolder)) {
156-
$entitiesPath = "{$entitiesPath}/{$subFolder}";
157-
}
155+
return file_exists($classPath);
156+
}
158157

159-
$classPath = base_path("{$entitiesPath}/{$name}.php");
158+
protected function getClassPath(string $path, string $name, ?string $subFolder = null): string
159+
{
160+
$path = $this->getPath($path, $subFolder);
160161

161-
return file_exists($classPath);
162+
return base_path("{$path}/{$name}.php");
162163
}
163164

164165
protected function saveClass($path, $name, $content, ?string $entityFolder = null): string

src/Generators/FactoryGenerator.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
use InvalidArgumentException;
99
use RonasIT\Support\Exceptions\FakerMethodNotFoundException;
1010
use RonasIT\Support\Exceptions\ClassNotExistsException;
11-
use RonasIT\Support\Exceptions\ClassAlreadyExistsException;
1211
use RonasIT\Support\Events\SuccessCreateMessage;
12+
use RonasIT\Support\Exceptions\ResourceAlreadyExistsException;
1313

1414
class FactoryGenerator extends EntityGenerator
1515
{
@@ -37,11 +37,9 @@ public function generate(): void
3737
}
3838

3939
if ($this->classExists('factories', "{$this->model}Factory")) {
40-
$this->throwFailureException(
41-
exceptionClass: ClassAlreadyExistsException::class,
42-
failureMessage: "Cannot create {$this->model}Factory cause {$this->model}Factory already exists.",
43-
recommendedMessage: "Remove {$this->model}Factory.",
44-
);
40+
$path = $this->getClassPath('factories', "{$this->model}Factory");
41+
42+
throw new ResourceAlreadyExistsException($path);
4543
}
4644

4745
if (!$this->isStubExists('factory')) {

src/Generators/ModelGenerator.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
use Illuminate\Support\Arr;
66
use Illuminate\Support\Str;
7-
use RonasIT\Support\Exceptions\ClassAlreadyExistsException;
87
use RonasIT\Support\Exceptions\ClassNotExistsException;
98
use RonasIT\Support\Events\SuccessCreateMessage;
9+
use RonasIT\Support\Exceptions\ResourceAlreadyExistsException;
1010

1111
class ModelGenerator extends EntityGenerator
1212
{
@@ -18,12 +18,9 @@ class ModelGenerator extends EntityGenerator
1818
public function generate(): void
1919
{
2020
if ($this->classExists('models', $this->model, $this->modelSubFolder)) {
21-
// TODO: pass $this->modelSubfolder to Exception after refactoring in https://github.com/RonasIT/laravel-entity-generator/issues/179
22-
$this->throwFailureException(
23-
exceptionClass: ClassAlreadyExistsException::class,
24-
failureMessage: "Cannot create {$this->model} Model cause {$this->model} Model already exists.",
25-
recommendedMessage: "Remove {$this->model} Model.",
26-
);
21+
$path = $this->getClassPath('models', $this->model, $this->modelSubFolder);
22+
23+
throw new ResourceAlreadyExistsException($path);
2724
}
2825

2926
if ($this->isStubExists('model') && (!$this->hasRelations() || $this->isStubExists('relation', 'model'))) {

src/Generators/NovaResourceGenerator.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
use Illuminate\Support\Facades\DB;
88
use Laravel\Nova\NovaServiceProvider;
99
use RonasIT\Support\Events\SuccessCreateMessage;
10-
use RonasIT\Support\Exceptions\ClassAlreadyExistsException;
1110
use RonasIT\Support\Exceptions\ClassNotExistsException;
11+
use RonasIT\Support\Exceptions\ResourceAlreadyExistsException;
1212
use RonasIT\Support\Support\CommandLineNovaField;
1313
use RonasIT\Support\Support\DatabaseNovaField;
1414

@@ -65,12 +65,9 @@ public function generate(): void
6565
}
6666

6767
if ($this->classExists('nova', "{$this->model}Resource")) {
68-
// TODO: pass $this->modelSubfolder to Exception after refactoring in https://github.com/RonasIT/laravel-entity-generator/issues/179
69-
$this->throwFailureException(
70-
ClassAlreadyExistsException::class,
71-
"Cannot create {$this->model}Resource cause {$this->model}Resource already exists.",
72-
"Remove {$this->model}Resource."
73-
);
68+
$path = $this->getClassPath('nova', "{$this->model}Resource", $this->modelSubFolder);
69+
70+
throw new ResourceAlreadyExistsException($path);
7471
}
7572

7673
if (!$this->isStubExists('nova_resource')) {

src/Generators/NovaTestGenerator.php

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,20 @@
1313
use RecursiveIteratorIterator;
1414
use RecursiveDirectoryIterator;
1515
use Illuminate\Support\Arr;
16+
use RonasIT\Support\Exceptions\ResourceAlreadyExistsException;
1617

1718
class NovaTestGenerator extends AbstractTestsGenerator
1819
{
19-
protected string $novaPath;
20-
2120
protected string $novaResourceClassName;
2221

23-
public function __construct()
24-
{
25-
parent::__construct();
26-
27-
$this->novaPath = base_path($this->paths['nova']);
28-
}
29-
3022
public function generate(): void
3123
{
3224
if (class_exists(NovaServiceProvider::class)) {
3325
if ($this->classExists('nova', "Nova{$this->model}ResourceTest")) {
34-
$this->throwFailureException(
35-
ClassAlreadyExistsException::class,
36-
"Cannot create Nova{$this->model}ResourceTest cause it's already exist.",
37-
"Remove Nova{$this->model}ResourceTest."
38-
);
26+
27+
$path = $this->getClassPath('nova', "Nova{$this->model}ResourceTest");
28+
29+
throw new ResourceAlreadyExistsException($path);
3930
}
4031

4132
$novaResources = $this->getCommonNovaResources();
@@ -123,7 +114,7 @@ protected function getActions(): array
123114

124115
protected function getNovaFiles(): Generator
125116
{
126-
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->novaPath));
117+
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(base_path($this->paths['nova'])));
127118

128119
foreach ($iterator as $file) {
129120
if ($file->isFile() && $file->getExtension() === 'php') {
@@ -137,11 +128,11 @@ protected function getCommonNovaResources(): array
137128
$resources = [];
138129

139130
foreach ($this->getNovaFiles() as $file) {
140-
$relativePath = Str::after($file->getPathname(), $this->novaPath . DIRECTORY_SEPARATOR);
131+
$relativePath = Str::after($file->getPathname(), $this->paths['nova'] . DIRECTORY_SEPARATOR);
141132

142133
$class = Str::before($relativePath, '.');
143134

144-
$className = $this->pathToNamespace($this->novaPath . DIRECTORY_SEPARATOR . $class);
135+
$className = $this->pathToNamespace($this->paths['nova'] . DIRECTORY_SEPARATOR . $class);
145136

146137
if ($this->isResourceNameContainModel($className) && $this->isNovaResource($className)) {
147138
$resources[] = $className;
@@ -185,7 +176,7 @@ protected function isFixtureNeeded($type): bool
185176
{
186177
return true;
187178
}
188-
179+
189180
protected function collectFilters(): array
190181
{
191182
$filtersFromFields = $this->getFiltersFromFields();

src/Generators/ResourceGenerator.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
namespace RonasIT\Support\Generators;
44

5+
use Illuminate\Support\Arr;
56
use RonasIT\Support\Events\SuccessCreateMessage;
6-
use RonasIT\Support\Exceptions\ClassAlreadyExistsException;
7+
use RonasIT\Support\Exceptions\ResourceAlreadyExistsException;
78

89
class ResourceGenerator extends EntityGenerator
910
{
@@ -25,11 +26,9 @@ public function generateCollectionResource(): void
2526
$pluralName = $this->getPluralName($this->model);
2627

2728
if ($this->classExists('resources', "{$pluralName}CollectionResource")) {
28-
$this->throwFailureException(
29-
ClassAlreadyExistsException::class,
30-
"Cannot create {$pluralName}CollectionResource cause {$pluralName}CollectionResource already exists.",
31-
"Remove {$pluralName}CollectionResource."
32-
);
29+
$path = $this->getClassPath('resources', "{$pluralName}CollectionResource");
30+
31+
throw new ResourceAlreadyExistsException($path);
3332
}
3433

3534
$collectionResourceContent = $this->getStub('collection_resource', [
@@ -46,17 +45,16 @@ public function generateCollectionResource(): void
4645
public function generateResource(): void
4746
{
4847
if ($this->classExists('resources', "{$this->model}Resource")) {
49-
$this->throwFailureException(
50-
ClassAlreadyExistsException::class,
51-
"Cannot create {$this->model}Resource cause {$this->model}Resource already exists.",
52-
"Remove {$this->model}Resource."
53-
);
48+
$path = $this->getClassPath('resources', "{$this->model}Resource");
49+
50+
throw new ResourceAlreadyExistsException($path);
5451
}
5552

5653
$resourceContent = $this->getStub('resource', [
5754
'entity' => $this->model,
5855
'namespace' => $this->getNamespace('resources'),
5956
'model_namespace' => $this->getNamespace('models', $this->modelSubFolder),
57+
'fields' => when($this->fields, fn () => Arr::flatten($this->fields)),
6058
]);
6159

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

stubs/resource.blade.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,19 @@
88
*/
99
class {{ $entity }}Resource extends BaseResource
1010
{
11+
@if (empty($fields))
1112
//TODO implement custom serialization logic or remove method redefining
13+
@endif
1214
public function toArray($request): array
1315
{
14-
return parent::toArray($request);
15-
}
16+
@if (!empty($fields))
17+
return [
18+
@foreach($fields as $field)
19+
'{{ $field }}' => $this->resource->{{ $field }},
20+
@endforeach
21+
];
22+
@else
23+
return parent::toArray($request);
24+
@endif
25+
}
1626
}

0 commit comments

Comments
 (0)