Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
21 changes: 16 additions & 5 deletions src/Generators/AbstractTestsGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ protected function getFixturesPath($fileName = null): string

protected function createDump(): void
{
if (!$this->checkStubExists('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 @@ -194,6 +194,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 @@ -273,6 +275,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->checkStubExists('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->checkStubExists('routes') && $this->checkStubExists('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 @@ -5,6 +5,7 @@
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use RonasIT\Support\Events\WarningEvent;

/**
* @property Filesystem $fs
Expand Down Expand Up @@ -141,6 +142,25 @@ protected function getStub($stub, $data = []): string
return view($stubPath)->with($data)->render();
}

protected function checkStubExists(string $stub): bool
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
protected function checkStubExists(string $stub): bool
protected function isStubExists(string $stubName): bool

{
$config = "entity-generator.stubs.{$stub}";

$stubPath = config($config);

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

$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;
}

protected function getTableName($entityName, $delimiter = '_'): string
{
$entityName = Str::snake($entityName, $delimiter);
Expand Down
54 changes: 30 additions & 24 deletions src/Generators/FactoryGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,40 +57,46 @@ protected function generateSeparateClass(): string

protected function generateToGenericClass(): string
{
if (!file_exists($this->paths['factory'])) {
$this->prepareEmptyFactory();
}

if (!$this->checkExistModelFactory() && $this->checkExistRelatedModelsFactories()) {
$stubPath = config("entity-generator.stubs.legacy_factory");
$stubPath = config("entity-generator.stubs.legacy_factory");

$content = view($stubPath)->with([
'entity' => $this->model,
'fields' => $this->prepareFields(),
'modelsNamespace' => $this->getOrCreateNamespace('models')
])->render();

$content = "\n\n" . $content;
$content = view($stubPath)->with([
'entity' => $this->model,
'fields' => $this->prepareFields(),
'modelsNamespace' => $this->getOrCreateNamespace('models')
])->render();

$createMessage = "Created a new Test factory for {$this->model} model in '{$this->paths['factory']}'";
$content = "\n\n" . $content;

file_put_contents($this->paths['factory'], $content, FILE_APPEND);
file_put_contents($this->paths['factory'], $content, FILE_APPEND);

$this->prepareRelatedFactories();
} else {
$createMessage = "Factory for {$this->model} model has already created, so new factory not necessary create.";
}
$this->prepareRelatedFactories();

return $createMessage;
return "Created a new Test factory for {$this->model} model in '{$this->paths['factory']}'";
}

public function generate(): void
{
$createMessage = (version_compare(app()->version(), '8', '>='))
? $this->generateSeparateClass()
: $this->generateToGenericClass();
$isActualVersion = version_compare(app()->version(), '8', '>=');

event(new SuccessCreateMessage($createMessage));
if ($isActualVersion && $this->checkStubExists('factory')) {
event(new SuccessCreateMessage($this->generateSeparateClass()));
} else if (!$isActualVersion) {
if (!file_exists($this->paths['factory']) && $this->checkStubExists('legacy_empty_factory')) {
$this->prepareEmptyFactory();
}

if (!$this->checkExistModelFactory() && $this->checkExistRelatedModelsFactories()) {
if (!$this->checkStubExists('legacy_factory')) {
return;
}

$createMessage = $this->generateToGenericClass();
} else {
$createMessage = "Factory for {$this->model} model has already created, so new factory not necessary create.";
}

event(new SuccessCreateMessage($createMessage));
}
}

protected function prepareEmptyFactory(): void
Expand Down
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->checkStubExists('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->checkStubExists('model') && ($this->checkStubExists('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->checkStubExists('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->checkStubExists('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->checkStubExists('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->checkStubExists('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->checkStubExists('resource')) {
$this->generateResource();

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

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

public function generate(): void
{
$entitySeeder = (version_compare(app()->version(), '8', '>=')) ? 'seeder' : 'legacy_seeder';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's remove legacy seeder check


if (!$this->checkStubExists($entitySeeder) || !$this->checkStubExists('database_empty_seeder')) {
return;
}

if (!file_exists($this->seedsPath)) {
mkdir($this->seedsPath);
}
Expand All @@ -36,27 +42,16 @@ public function generate(): void
$this->createDatabaseSeeder();
}

$this->createEntitySeeder();
$this->createEntitySeeder($entitySeeder);

$this->appendSeederToList();
}

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 @@ -65,18 +60,14 @@ protected function createDatabaseSeeder(): void
event(new SuccessCreateMessage($createMessage));
}

protected function createEntitySeeder(): void
protected function createEntitySeeder(string $entitySeeder): 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($entitySeeder, [
'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->checkStubExists($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->checkStubExists('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->checkStubExists('validation')) {
$this->createTranslate();
}

if ($this->isTranslationMissed('validation.exceptions.not_found')) {
if ($this->isTranslationMissed('validation.exceptions.not_found') && $this->checkStubExists('translation_not_found')) {
$this->appendNotFoundException();
}
}
Expand Down
Loading