Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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
9 changes: 2 additions & 7 deletions config/entity-generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@
'requests' => 'app/Http/Requests',
'controllers' => 'app/Http/Controllers',
'migrations' => 'database/migrations',
'seeders' => (version_compare(app()->version(), '8', '>='))
? 'database/seeders'
: 'database/seeds',
'database_seeder' => (version_compare(app()->version(), '8', '>='))
? 'database/seeders/DatabaseSeeder.php'
: 'database/seeds/DatabaseSeeder.php',
'seeders' => 'database/seeders',
'database_seeder' => 'database/seeders/DatabaseSeeder.php',
'repositories' => 'app/Repositories',
'tests' => 'tests',
'routes' => 'routes/api.php',
Expand All @@ -33,7 +29,6 @@
'use_routes' => 'entity-generator::use_routes',
'factory' => 'entity-generator::factory',
'seeder' => 'entity-generator::seeder',
'legacy_seeder' => 'entity-generator::legacy_seeder',
'database_empty_seeder' => 'entity-generator::database_empty_seeder',
'migration' => 'entity-generator::migration',
'dump' => 'entity-generator::dumps.pgsql',
Expand Down
21 changes: 16 additions & 5 deletions src/Generators/AbstractTestsGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ protected function getFixturesPath($fileName = null): string

protected function createDump(): void
{
if (!$this->isStubExists('dump')) {
return;
}

$content = $this->getStub('dump', [
'inserts' => $this->getInserts()
]);

$fixturePath = $this->getFixturesPath();

if (!file_exists($fixturePath)) {
mkdir($fixturePath, 0777, true);
}
$this->createFixtureFolder();

$dumpName = $this->getDumpName();

Expand Down Expand Up @@ -186,6 +186,8 @@ protected function generateFixtures(): void
$object = $this->getFixtureValuesList($this->model);
$entity = Str::snake($this->model);

$this->createFixtureFolder();

foreach (self::FIXTURE_TYPES as $type => $modifications) {
if ($this->isFixtureNeeded($type)) {
foreach ($modifications as $modification) {
Expand Down Expand Up @@ -241,6 +243,15 @@ protected function canGenerateUserData(): bool
&& $this->isMethodExists('User', 'getFields');
}

protected function createFixtureFolder(): void
{
$fixturePath = $this->getFixturesPath();

if (!file_exists($fixturePath)) {
mkdir($fixturePath, 0777, true);
}
}

abstract protected function getTestClassName(): string;

abstract protected function isFixtureNeeded($type): bool;
Expand Down
12 changes: 9 additions & 3 deletions src/Generators/ControllerGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@ public function generate(): void
if (!$this->classExists('services', "{$this->model}Service")) {
$this->throwFailureException(
ClassNotExistsException::class,
"Cannot create {$this->model}Service cause {$this->model}Service does not exists.",
"Cannot create {$this->model}Controller cause {$this->model}Service does not exists.",
"Create a {$this->model}Service by himself.",
);
}

if (!$this->isStubExists('controller')) {
return;
}

$controllerContent = $this->getControllerContent($this->model);

$this->saveClass('controllers', "{$this->model}Controller", $controllerContent);
Expand Down Expand Up @@ -61,8 +65,10 @@ protected function createRoutes(): void
);
}

$this->addUseController($routesPath);
$this->addRoutes($routesPath);
if ($this->isStubExists('routes') && $this->isStubExists('use_routes')) {
$this->addUseController($routesPath);
$this->addRoutes($routesPath);
}
}

protected function addRoutes($routesPath): string
Expand Down
20 changes: 20 additions & 0 deletions src/Generators/EntityGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use RonasIT\Support\Events\WarningEvent;
use RonasIT\Support\Exceptions\ClassNotExistsException;
use Throwable;
use ReflectionMethod;
Expand Down Expand Up @@ -211,4 +212,23 @@ protected function getModelClass(string $model): string

return "{$modelNamespace}\\{$model}";
}

protected function isStubExists(string $stubName): bool
{
$config = "entity-generator.stubs.{$stubName}";

$stubPath = config($config);

if (!view()->exists($stubPath)) {
$generationType = Str::replace('_', ' ', $stubName);

$message = "Generation of {$generationType} has been skipped cause the view {$stubPath} from the config {$config} is not exists. Please check that config has the correct view name value.";

event(new WarningEvent($message));

return false;
}

return true;
}
}
6 changes: 5 additions & 1 deletion src/Generators/FactoryGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public function generate(): void
);
}

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

$factoryContent = $this->getStub('factory', [
'namespace' => $this->getOrCreateNamespace('factories'),
'entity' => $this->model,
Expand Down Expand Up @@ -113,4 +117,4 @@ protected function prepareFields(): array

return $result;
}
}
}
4 changes: 4 additions & 0 deletions src/Generators/MigrationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ public function generate(): void
{
$entities = $this->getTableName($this->model);

if (!$this->isStubExists('migration')) {
return;
}

$content = $this->getStub('migration', [
'class' => $this->getPluralName($this->model),
'entity' => $this->model,
Expand Down
10 changes: 6 additions & 4 deletions src/Generators/ModelGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ public function generate(): void
);
}

$this->prepareRelatedModels();
$modelContent = $this->getNewModelContent();
if ($this->isStubExists('model') && ($this->isStubExists('relation') || empty($this->relations))) {
$this->prepareRelatedModels();
$modelContent = $this->getNewModelContent();

$this->saveClass('models', $this->model, $modelContent);
$this->saveClass('models', $this->model, $modelContent);

event(new SuccessCreateMessage("Created a new Model: {$this->model}"));
event(new SuccessCreateMessage("Created a new Model: {$this->model}"));
}
}

protected function getNewModelContent(): string
Expand Down
4 changes: 4 additions & 0 deletions src/Generators/NovaResourceGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ public function generate(): void
);
}

if (!$this->isStubExists('nova_resource')) {
return;
}

$novaFields = $this->prepareNovaFields();

$fileContent = $this->getStub('nova_resource', [
Expand Down
4 changes: 4 additions & 0 deletions src/Generators/NovaTestGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public function generate(): void

public function generateTests(): void
{
if (!$this->isStubExists('nova_test')) {
return;
}

$actions = $this->getActions();
$filters = $this->collectFilters();

Expand Down
4 changes: 4 additions & 0 deletions src/Generators/RepositoryGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ public function generate(): void
);
}

if (!$this->isStubExists('repository')) {
return;
}

$repositoryContent = $this->getStub('repository', [
'entity' => $this->model,
'namespace' => $this->getOrCreateNamespace('repositories'),
Expand Down
4 changes: 4 additions & 0 deletions src/Generators/RequestsGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ public function setRelations($relations)

public function generate(): void
{
if (!$this->isStubExists('request')) {
return;
}

if (in_array('R', $this->crudOptions)) {
$this->createRequest(
self::GET_METHOD,
Expand Down
9 changes: 7 additions & 2 deletions src/Generators/ResourceGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ class ResourceGenerator extends EntityGenerator
{
public function generate(): void
{
$this->generateResource();
$this->generateCollectionResource();
if ($this->isStubExists('resource')) {
$this->generateResource();

if ($this->isStubExists('collection_resource')) {
$this->generateCollectionResource();
}
}
}

public function generateCollectionResource(): void
Expand Down
29 changes: 9 additions & 20 deletions src/Generators/SeederGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public function __construct()

public function generate(): void
{
if (!$this->isStubExists('seeder') || !$this->isStubExists('database_empty_seeder')) {
return;
}

if (!file_exists($this->seedsPath)) {
mkdir($this->seedsPath);
}
Expand All @@ -43,20 +47,9 @@ public function generate(): void

protected function createDatabaseSeeder(): void
{
$stubPath = config('entity-generator.stubs.database_empty_seeder');

//@TODO: remove after implementing https://github.com/RonasIT/laravel-entity-generator/issues/93
if ($stubPath === 'entity-generator::database_seed_empty') {
$stubPath = 'entity-generator::database_empty_seeder';

$message = "You are using the deprecated value for 'entity-generator.stubs.database_empty_seeder' config. Please use 'entity-generator::database_empty_seeder'.";

event(new WarningEvent($message));
}

$content = "<?php \n\n" . view($stubPath, [
$content = "<?php \n\n" . $this->getStub('database_empty_seeder', [
'namespace' => $this->getOrCreateNamespace('seeders')
])->render();
]);

file_put_contents($this->databaseSeederPath, $content);

Expand All @@ -67,16 +60,12 @@ protected function createDatabaseSeeder(): void

protected function createEntitySeeder(): void
{
$seeder = (version_compare(app()->version(), '8', '>=')) ? 'seeder' : 'legacy_seeder';

$stubPath = config("entity-generator.stubs.{$seeder}");

$content = "<?php \n\n" . view($stubPath)->with([
$content = "<?php \n\n" . $this->getStub('seeder', [
'entity' => $this->model,
'relations' => $this->relations,
'namespace' => $this->getOrCreateNamespace('seeders'),
'modelsNamespace' => $this->getOrCreateNamespace('models')
])->render();
'modelsNamespace' => $this->getOrCreateNamespace('models'),
]);

$seederPath = "{$this->seedsPath}/{$this->model}Seeder.php";

Expand Down
4 changes: 4 additions & 0 deletions src/Generators/ServiceGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public function generate(): void
}
}

if (!$this->isStubExists($stub)) {
return;
}

$serviceContent = $this->getStub($stub, [
'entity' => $this->model,
'fields' => $this->getFields(),
Expand Down
4 changes: 4 additions & 0 deletions src/Generators/TestsGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ protected function generateFixture($fixtureName, $data): void

protected function generateTests(): void
{
if (!$this->isStubExists('test')) {
return;
}

$content = $this->getStub('test', [
'entity' => $this->model,
'databaseTableName' => $this->getTableName($this->model),
Expand Down
4 changes: 2 additions & 2 deletions src/Generators/TranslationsGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ public function __construct()

public function generate(): void
{
if (!file_exists($this->translationPath)) {
if (!file_exists($this->translationPath) && $this->isStubExists('validation')) {
$this->createTranslate();
}

if ($this->isTranslationMissed('validation.exceptions.not_found')) {
if ($this->isTranslationMissed('validation.exceptions.not_found') && $this->isStubExists('translation_not_found')) {
$this->appendNotFoundException();
}
}
Expand Down
44 changes: 0 additions & 44 deletions stubs/legacy_seeder.blade.php

This file was deleted.

Loading