Skip to content

Commit f246aa0

Browse files
committed
Merge remote-tracking branch 'origin/master' into add-resource-not-exists-exception
2 parents 360ec8b + 6e3ec23 commit f246aa0

19 files changed

+199
-54
lines changed

src/Generators/ControllerGenerator.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,12 @@
44

55
use Illuminate\Contracts\Filesystem\FileNotFoundException;
66
use RonasIT\Support\Events\SuccessCreateMessage;
7-
use RonasIT\Support\Exceptions\ResourceAlreadyExistsException;
87

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

1914
$this->checkResourceNotExists('services', "{$this->model}Controller", "{$this->model}Service");
2015

src/Generators/EntityGenerator.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use RonasIT\Support\Exceptions\ClassNotExistsException;
1616
use RonasIT\Support\Exceptions\ResourceNotExistsException;
1717
use RonasIT\Support\Exceptions\IncorrectClassPathException;
18+
use RonasIT\Support\Exceptions\ResourceAlreadyExistsException;
1819

1920
/**
2021
* @property Filesystem $fs
@@ -320,6 +321,15 @@ protected function checkConfigHasCorrectPaths(): void
320321
}
321322
}
322323

324+
protected function checkResourceExists(string $path, string $resourceName, ?string $subFolder = null): void
325+
{
326+
if ($this->classExists($path, $resourceName, $subFolder)) {
327+
$filePath = $this->getClassPath($path, $resourceName, $subFolder);
328+
329+
throw new ResourceAlreadyExistsException($filePath);
330+
}
331+
}
332+
323333
protected function checkResourceNotExists(string $path, string $entity, string $resourceName, ?string $subFolder = null): void
324334
{
325335
if (!$this->classExists($path, $resourceName, $subFolder)) {

src/Generators/FactoryGenerator.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use InvalidArgumentException;
99
use RonasIT\Support\Exceptions\FakerMethodNotFoundException;
1010
use RonasIT\Support\Events\SuccessCreateMessage;
11-
use RonasIT\Support\Exceptions\ResourceAlreadyExistsException;
1211

1312
class FactoryGenerator extends EntityGenerator
1413
{
@@ -28,11 +27,7 @@ public function generate(): void
2827
{
2928
$this->checkResourceNotExists('models', "{$this->model}Factory", $this->model, $this->modelSubFolder);
3029

31-
if ($this->classExists('factories', "{$this->model}Factory")) {
32-
$path = $this->getClassPath('factories', "{$this->model}Factory");
33-
34-
throw new ResourceAlreadyExistsException($path);
35-
}
30+
$this->checkResourceExists('factories', "{$this->model}Factory");
3631

3732
if (!$this->isStubExists('factory')) {
3833
return;

src/Generators/ModelGenerator.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Illuminate\Support\Arr;
66
use Illuminate\Support\Str;
77
use RonasIT\Support\Events\SuccessCreateMessage;
8-
use RonasIT\Support\Exceptions\ResourceAlreadyExistsException;
98

109
class ModelGenerator extends EntityGenerator
1110
{
@@ -16,11 +15,7 @@ class ModelGenerator extends EntityGenerator
1615

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

2520
if ($this->isStubExists('model') && (!$this->hasRelations() || $this->isStubExists('relation', 'model'))) {
2621
$this->createNamespace('models', $this->modelSubFolder);

src/Generators/NovaResourceGenerator.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Support\Facades\DB;
88
use Laravel\Nova\NovaServiceProvider;
99
use RonasIT\Support\Events\SuccessCreateMessage;
10+
use RonasIT\Support\Exceptions\ClassNotExistsException;
1011
use RonasIT\Support\Exceptions\ResourceAlreadyExistsException;
1112
use RonasIT\Support\Support\CommandLineNovaField;
1213
use RonasIT\Support\Support\DatabaseNovaField;
@@ -56,11 +57,7 @@ public function generate(): void
5657
if (class_exists(NovaServiceProvider::class)) {
5758
$this->checkResourceNotExists('models', "{$this->model} Nova resource", $this->model, $this->modelSubFolder);
5859

59-
if ($this->classExists('nova', "{$this->model}Resource")) {
60-
$path = $this->getClassPath('nova', "{$this->model}Resource", $this->modelSubFolder);
61-
62-
throw new ResourceAlreadyExistsException($path);
63-
}
60+
$this->checkResourceExists('nova', "{$this->model}Resource", $this->modelSubFolder);
6461

6562
if (!$this->isStubExists('nova_resource')) {
6663
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
@@ -10,6 +10,8 @@ public function generate(): void
1010
{
1111
$this->checkResourceNotExists('models', "{$this->model}Repository", $this->model, $this->modelSubFolder);
1212

13+
$this->checkResourceExists('repositories', "{$this->model}Repository");
14+
1315
if (!$this->isStubExists('repository')) {
1416
return;
1517
}

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)