Skip to content

Commit 6e3ec23

Browse files
authored
Merge pull request #219 from RonasIT/bugfix-entity-generation-overwrite
fix: exception thrown for existing entity files
2 parents 219d2b1 + 6b70a23 commit 6e3ec23

19 files changed

+208
-65
lines changed

src/Generators/ControllerGenerator.php

Lines changed: 1 addition & 6 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(

src/Generators/EntityGenerator.php

Lines changed: 16 additions & 6 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
@@ -318,4 +319,13 @@ protected function checkConfigHasCorrectPaths(): void
318319
}
319320
}
320321
}
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+
}
321331
}

src/Generators/FactoryGenerator.php

Lines changed: 1 addition & 6 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;

src/Generators/ModelGenerator.php

Lines changed: 1 addition & 6 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);

src/Generators/NovaResourceGenerator.php

Lines changed: 1 addition & 6 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;

src/Generators/NovaTestGenerator.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use RecursiveIteratorIterator;
1313
use RecursiveDirectoryIterator;
1414
use Illuminate\Support\Arr;
15-
use RonasIT\Support\Exceptions\ResourceAlreadyExistsException;
1615

1716
class NovaTestGenerator extends AbstractTestsGenerator
1817
{
@@ -21,12 +20,7 @@ class NovaTestGenerator extends AbstractTestsGenerator
2120
public function generate(): void
2221
{
2322
if (class_exists(NovaServiceProvider::class)) {
24-
if ($this->classExists('nova', "Nova{$this->model}ResourceTest")) {
25-
26-
$path = $this->getClassPath('nova', "Nova{$this->model}ResourceTest");
27-
28-
throw new ResourceAlreadyExistsException($path);
29-
}
23+
$this->checkResourceExists('nova', "Nova{$this->model}ResourceTest");
3024

3125
$novaResources = $this->getCommonNovaResources();
3226

src/Generators/RepositoryGenerator.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public function generate(): void
1818
);
1919
}
2020

21+
$this->checkResourceExists('repositories', "{$this->model}Repository");
22+
2123
if (!$this->isStubExists('repository')) {
2224
return;
2325
}

src/Generators/RequestsGenerator.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ protected function createRequest($method, $needToValidate = true, $parameters =
6767
$requestsFolder = $this->model;
6868
$modelName = $this->getEntityName($method);
6969

70+
$this->checkResourceExists('requests', "{$requestsFolder}/{$method}{$modelName}Request");
71+
7072
$content = $this->getStub('request', [
7173
'method' => $method,
7274
'entity' => $modelName,

src/Generators/ResourceGenerator.php

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

55
use Illuminate\Support\Arr;
66
use RonasIT\Support\Events\SuccessCreateMessage;
7-
use RonasIT\Support\Exceptions\ResourceAlreadyExistsException;
87

98
class ResourceGenerator extends EntityGenerator
109
{
@@ -25,11 +24,7 @@ public function generateCollectionResource(): void
2524
{
2625
$pluralName = $this->getPluralName($this->model);
2726

28-
if ($this->classExists('resources', "{$pluralName}CollectionResource")) {
29-
$path = $this->getClassPath('resources', "{$pluralName}CollectionResource");
30-
31-
throw new ResourceAlreadyExistsException($path);
32-
}
27+
$this->checkResourceExists('resources', "{$this->model}/{$pluralName}CollectionResource");
3328

3429
$collectionResourceContent = $this->getStub('collection_resource', [
3530
'singular_name' => $this->model,
@@ -44,11 +39,7 @@ public function generateCollectionResource(): void
4439

4540
public function generateResource(): void
4641
{
47-
if ($this->classExists('resources', "{$this->model}Resource")) {
48-
$path = $this->getClassPath('resources', "{$this->model}Resource");
49-
50-
throw new ResourceAlreadyExistsException($path);
51-
}
42+
$this->checkResourceExists('resources', "{$this->model}/{$this->model}Resource");
5243

5344
$resourceContent = $this->getStub('resource', [
5445
'entity' => $this->model,

src/Generators/SeederGenerator.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ public function __construct()
2020

2121
public function generate(): void
2222
{
23+
$this->checkResourceExists('seeders', "{$this->model}Seeder");
24+
2325
if (!$this->isStubExists('seeder') || !$this->isStubExists('database_empty_seeder')) {
2426
return;
2527
}

0 commit comments

Comments
 (0)