Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/Exceptions/AbstractResourceException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace RonasIT\Support\Exceptions;

use Exception;
use Illuminate\Support\Str;

abstract class AbstractResourceException extends Exception
{
protected function getEntity(string $filePath): string
{
$fileName = Str::afterLast($filePath, '/');

return Str::before($fileName, '.php');
}
}
18 changes: 4 additions & 14 deletions src/Exceptions/ResourceAlreadyExistsException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,13 @@

namespace RonasIT\Support\Exceptions;

use Exception;
use Illuminate\Support\Str;

class ResourceAlreadyExistsException extends Exception
class ResourceAlreadyExistsException extends AbstractResourceException
{
public function __construct(
protected string $filePath,
string $filePath,
) {
$entity = $this->getEntity();

parent::__construct("Cannot create {$entity} cause it already exists. Remove {$this->filePath} and run command again.");
}

protected function getEntity(): string
{
$fileName = Str::afterLast($this->filePath, '/');
$entity = $this->getEntity($filePath);

return Str::before($fileName, '.php');
parent::__construct("Cannot create {$entity} cause it already exists. Remove {$filePath} and run command again.");
}
}
15 changes: 15 additions & 0 deletions src/Exceptions/ResourceNotExistsException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace RonasIT\Support\Exceptions;

class ResourceNotExistsException extends AbstractResourceException
{
public function __construct(
string $entity,
string $filePath,
) {
$resource = $this->getEntity($filePath);

parent::__construct("Cannot create {$entity} cause {$resource} does not exist. Create {$filePath} and run command again.");
}
}
9 changes: 1 addition & 8 deletions src/Generators/ControllerGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace RonasIT\Support\Generators;

use Illuminate\Contracts\Filesystem\FileNotFoundException;
use RonasIT\Support\Exceptions\ClassNotExistsException;
use RonasIT\Support\Events\SuccessCreateMessage;
use RonasIT\Support\Exceptions\ResourceAlreadyExistsException;

Expand All @@ -17,13 +16,7 @@ public function generate(): void
throw new ResourceAlreadyExistsException($path);
}

if (!$this->classExists('services', "{$this->model}Service")) {
$this->throwFailureException(
ClassNotExistsException::class,
"Cannot create {$this->model}Controller cause {$this->model}Service does not exists.",
"Create a {$this->model}Service by himself.",
);
}
$this->checkResourceNotExists('services', "{$this->model}Controller", "{$this->model}Service");

if (!$this->isStubExists('controller')) {
return;
Expand Down
22 changes: 16 additions & 6 deletions src/Generators/EntityGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

namespace RonasIT\Support\Generators;

use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Filesystem\Filesystem;
use Throwable;
use ReflectionClass;
use ReflectionMethod;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\DB;
use Illuminate\Filesystem\Filesystem;
use RonasIT\Support\DTO\RelationsDTO;
use RonasIT\Support\Events\WarningEvent;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use RonasIT\Support\Exceptions\ClassNotExistsException;
use RonasIT\Support\Exceptions\ResourceNotExistsException;
use RonasIT\Support\Exceptions\IncorrectClassPathException;
use Throwable;
use ReflectionMethod;
use ReflectionClass;

/**
* @property Filesystem $fs
Expand Down Expand Up @@ -308,4 +309,13 @@ protected function pathToNamespace(string $name): string
{
return ucwords(Str::replace('/', '\\', $name), '\\');
}

protected function checkResourceNotExists(string $path, string $entity, string $resourceName, ?string $subFolder = null): void
{
if (!$this->classExists($path, $resourceName, $subFolder)) {
$filePath = $this->getClassPath($path, $resourceName, $subFolder);

throw new ResourceNotExistsException($entity, $filePath);
}
}
}
10 changes: 1 addition & 9 deletions src/Generators/FactoryGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Illuminate\Support\Str;
use InvalidArgumentException;
use RonasIT\Support\Exceptions\FakerMethodNotFoundException;
use RonasIT\Support\Exceptions\ClassNotExistsException;
use RonasIT\Support\Events\SuccessCreateMessage;
use RonasIT\Support\Exceptions\ResourceAlreadyExistsException;

Expand All @@ -27,14 +26,7 @@ class FactoryGenerator extends EntityGenerator

public function generate(): void
{
if (!$this->classExists('models', $this->model, $this->modelSubFolder)) {
// TODO: pass $this->modelSubfolder to Exception after refactoring in https://github.com/RonasIT/laravel-entity-generator/issues/179
$this->throwFailureException(
exceptionClass: ClassNotExistsException::class,
failureMessage: "Cannot create {$this->model}Factory cause {$this->model} Model does not exists.",
recommendedMessage: "Create a {$this->model} Model by itself or run command 'php artisan make:entity {$this->model} --only-model'.",
);
}
$this->checkResourceNotExists('models', "{$this->model}Factory", $this->model, $this->modelSubFolder);

if ($this->classExists('factories', "{$this->model}Factory")) {
$path = $this->getClassPath('factories', "{$this->model}Factory");
Expand Down
10 changes: 1 addition & 9 deletions src/Generators/ModelGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use RonasIT\Support\Exceptions\ClassNotExistsException;
use RonasIT\Support\Events\SuccessCreateMessage;
use RonasIT\Support\Exceptions\ResourceAlreadyExistsException;

Expand Down Expand Up @@ -65,14 +64,7 @@ public function prepareRelatedModels(): void

foreach ($this->relations as $type => $relationsByType) {
foreach ($relationsByType as $relation) {
if (!$this->classExists('models', $relation)) {
// TODO: pass $this->modelSubfolder to Exception after refactoring in https://github.com/RonasIT/laravel-entity-generator/issues/179
$this->throwFailureException(
exceptionClass: ClassNotExistsException::class,
failureMessage: "Cannot create {$this->model} Model cause relation model {$relation} does not exist.",
recommendedMessage: "Create the {$relation} Model by himself or run command 'php artisan make:entity {$relation} --only-model'.",
);
}
$this->checkResourceNotExists('models', $this->model, $relation);

$content = $this->getModelContent($relation);

Expand Down
10 changes: 1 addition & 9 deletions src/Generators/NovaResourceGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Illuminate\Support\Facades\DB;
use Laravel\Nova\NovaServiceProvider;
use RonasIT\Support\Events\SuccessCreateMessage;
use RonasIT\Support\Exceptions\ClassNotExistsException;
use RonasIT\Support\Exceptions\ResourceAlreadyExistsException;
use RonasIT\Support\Support\CommandLineNovaField;
use RonasIT\Support\Support\DatabaseNovaField;
Expand Down Expand Up @@ -55,14 +54,7 @@ class NovaResourceGenerator extends EntityGenerator
public function generate(): void
{
if (class_exists(NovaServiceProvider::class)) {
if (!$this->classExists('models', $this->model, $this->modelSubFolder)) {
// TODO: pass $this->modelSubfolder to Exception after refactoring in https://github.com/RonasIT/laravel-entity-generator/issues/179
$this->throwFailureException(
ClassNotExistsException::class,
"Cannot create {$this->model} Nova resource cause {$this->model} Model does not exists.",
"Create a {$this->model} Model by himself or run command 'php artisan make:entity {$this->model} --only-model'."
);
}
$this->checkResourceNotExists('models', "{$this->model} Nova resource", $this->model, $this->modelSubFolder);

if ($this->classExists('nova', "{$this->model}Resource")) {
$path = $this->getClassPath('nova', "{$this->model}Resource", $this->modelSubFolder);
Expand Down
10 changes: 1 addition & 9 deletions src/Generators/RepositoryGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,13 @@

namespace RonasIT\Support\Generators;

use RonasIT\Support\Exceptions\ClassNotExistsException;
use RonasIT\Support\Events\SuccessCreateMessage;

class RepositoryGenerator extends EntityGenerator
{
public function generate(): void
{
if (!$this->classExists('models', $this->model, $this->modelSubFolder)) {
// TODO: pass $this->modelSubfolder to Exception after refactoring in https://github.com/RonasIT/laravel-entity-generator/issues/179
$this->throwFailureException(
ClassNotExistsException::class,
"Cannot create {$this->model}Repository cause {$this->model} Model does not exists.",
"Create a {$this->model} Model by himself or run command 'php artisan make:entity {$this->model} --only-model'."
);
}
$this->checkResourceNotExists('models', "{$this->model}Repository", $this->model, $this->modelSubFolder);

if (!$this->isStubExists('repository')) {
return;
Expand Down
9 changes: 1 addition & 8 deletions src/Generators/ServiceGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,13 @@
namespace RonasIT\Support\Generators;

use Illuminate\Support\Arr;
use RonasIT\Support\Exceptions\ClassNotExistsException;
use RonasIT\Support\Events\SuccessCreateMessage;

class ServiceGenerator extends EntityGenerator
{
public function generate(): void
{
if (!$this->classExists('repositories', "{$this->model}Repository")) {
$this->throwFailureException(
exceptionClass: ClassNotExistsException::class,
failureMessage: "Cannot create {$this->model}Service cause {$this->model}Repository does not exists.",
recommendedMessage: "Create a {$this->model}Repository by himself or run command 'php artisan make:entity {$this->model} --only-repository'.",
);
}
$this->checkResourceNotExists('repositories', "{$this->model}Service", "{$this->model}Repository");

if (!$this->isStubExists('service')) {
return;
Expand Down
12 changes: 6 additions & 6 deletions tests/ControllerGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace RonasIT\Support\Tests;

use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Support\Facades\View;
use RonasIT\Support\Events\SuccessCreateMessage;
use RonasIT\Support\Events\WarningEvent;
use RonasIT\Support\Exceptions\ClassNotExistsException;
use RonasIT\Support\Exceptions\ResourceAlreadyExistsException;
use RonasIT\Support\Events\SuccessCreateMessage;
use RonasIT\Support\Generators\ControllerGenerator;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use RonasIT\Support\Exceptions\ResourceNotExistsException;
use RonasIT\Support\Exceptions\ResourceAlreadyExistsException;
use RonasIT\Support\Tests\Support\ControllerGeneratorTest\ControllerGeneratorMockTrait;

class ControllerGeneratorTest extends TestCase
Expand Down Expand Up @@ -46,8 +46,8 @@ public function testModelServiceNotExists()
]);

$this->assertExceptionThrew(
className: ClassNotExistsException::class,
message: 'Cannot create PostController cause PostService does not exists. Create a PostService by himself.',
className: ResourceNotExistsException::class,
message: 'Cannot create PostController cause PostService does not exist. Create app/Services/PostService.php and run command again.',
);

app(ControllerGenerator::class)
Expand Down
17 changes: 8 additions & 9 deletions tests/FactoryGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

namespace RonasIT\Support\Tests;

use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Event;
use Illuminate\View\ViewException;
use Illuminate\Support\Facades\Event;
use RonasIT\Support\DTO\RelationsDTO;
use RonasIT\Support\Events\SuccessCreateMessage;
use Illuminate\Support\Facades\Config;
use RonasIT\Support\Events\WarningEvent;
use RonasIT\Support\Exceptions\ClassNotExistsException;
use RonasIT\Support\Exceptions\IncorrectClassPathException;
use RonasIT\Support\Exceptions\ResourceAlreadyExistsException;
use RonasIT\Support\Events\SuccessCreateMessage;
use RonasIT\Support\Generators\FactoryGenerator;
use RonasIT\Support\Exceptions\ResourceNotExistsException;
use RonasIT\Support\Exceptions\IncorrectClassPathException;
use RonasIT\Support\Tests\Support\Factory\FactoryMockTrait;
use RonasIT\Support\Exceptions\ResourceAlreadyExistsException;

class FactoryGeneratorTest extends TestCase
{
Expand All @@ -30,9 +30,8 @@ public function testModelNotExists()
$this->mockFileSystemWithoutPostModel();

$this->assertExceptionThrew(
className: ClassNotExistsException::class,
message: "Cannot create PostFactory cause Post Model does not exists. "
. "Create a Post Model by itself or run command 'php artisan make:entity Post --only-model'.",
className: ResourceNotExistsException::class,
message: 'Cannot create PostFactory cause Post does not exist. Create app/Models/Post.php and run command again.',
);

app(FactoryGenerator::class)
Expand Down
11 changes: 5 additions & 6 deletions tests/ModelGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
namespace RonasIT\Support\Tests;

use RonasIT\Support\DTO\RelationsDTO;
use RonasIT\Support\Events\SuccessCreateMessage;
use RonasIT\Support\Events\WarningEvent;
use RonasIT\Support\Exceptions\ClassNotExistsException;
use RonasIT\Support\Exceptions\ResourceAlreadyExistsException;
use RonasIT\Support\Generators\ModelGenerator;
use RonasIT\Support\Events\SuccessCreateMessage;
use RonasIT\Support\Tests\Support\Model\ModelMockTrait;
use Symfony\Component\Console\Exception\RuntimeException;
use RonasIT\Support\Exceptions\ResourceNotExistsException;
use RonasIT\Support\Exceptions\ResourceAlreadyExistsException;

class ModelGeneratorTest extends TestCase
{
Expand Down Expand Up @@ -44,9 +44,8 @@ public function testRelationModelMissing()
$this->mockFileSystemWithoutCommentModel();

$this->assertExceptionThrew(
className: ClassNotExistsException::class,
message: "Cannot create Post Model cause relation model Comment does not exist. "
. "Create the Comment Model by himself or run command 'php artisan make:entity Comment --only-model'.",
className: ResourceNotExistsException::class,
message: 'Cannot create Post cause Comment does not exist. Create app/Models/Comment.php and run command again.',
);

app(ModelGenerator::class)
Expand Down
13 changes: 6 additions & 7 deletions tests/NovaResourceGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace RonasIT\Support\Tests;

use RonasIT\Support\Events\SuccessCreateMessage;
use RonasIT\Support\Events\WarningEvent;
use RonasIT\Support\Exceptions\ClassNotExistsException;
use RonasIT\Support\Exceptions\ResourceAlreadyExistsException;
use RonasIT\Support\Events\SuccessCreateMessage;
use RonasIT\Support\Generators\NovaResourceGenerator;
use RonasIT\Support\Tests\Support\NovaResourceGeneratorTest\NovaResourceGeneratorMockTrait;
use RonasIT\Support\Exceptions\ResourceNotExistsException;
use RonasIT\Support\Exceptions\ResourceAlreadyExistsException;
use RonasIT\Support\Tests\Support\NovaResourceGeneratorTest\Post;
use RonasIT\Support\Tests\Support\NovaResourceGeneratorTest\NovaResourceGeneratorMockTrait;

class NovaResourceGeneratorTest extends TestCase
{
Expand Down Expand Up @@ -42,9 +42,8 @@ public function testCreateNovaResourceWithMissingModel()
$this->mockNovaServiceProviderExists();

$this->assertExceptionThrew(
className: ClassNotExistsException::class,
message: 'Cannot create Post Nova resource cause Post Model does not exists. '
. "Create a Post Model by himself or run command 'php artisan make:entity Post --only-model'"
className: ResourceNotExistsException::class,
message: 'Cannot create Post Nova resource cause Post does not exist. Create app/Models/Post.php and run command again.'
);

app(NovaResourceGenerator::class)
Expand Down
9 changes: 4 additions & 5 deletions tests/RepositoryGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace RonasIT\Support\Tests;

use RonasIT\Support\Events\SuccessCreateMessage;
use RonasIT\Support\Events\WarningEvent;
use RonasIT\Support\Exceptions\ClassNotExistsException;
use RonasIT\Support\Events\SuccessCreateMessage;
use RonasIT\Support\Generators\RepositoryGenerator;
use RonasIT\Support\Exceptions\ResourceNotExistsException;
use RonasIT\Support\Tests\Support\Repository\RepositoryMockTrait;

class RepositoryGeneratorTest extends TestCase
Expand All @@ -26,9 +26,8 @@ public function testModelNotExist()
]);

$this->assertExceptionThrew(
className: ClassNotExistsException::class,
message: "Cannot create PostRepository cause Post Model does not exists. "
. "Create a Post Model by himself or run command 'php artisan make:entity Post --only-model'.",
className: ResourceNotExistsException::class,
message: 'Cannot create PostRepository cause Post does not exist. Create app/Models/Post.php and run command again.',
);

app(RepositoryGenerator::class)
Expand Down
Loading