Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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: 9 additions & 0 deletions src/Exceptions/IncorrectClassPathException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace RonasIT\Support\Exceptions;

use Exception;

class IncorrectClassPathException extends Exception
{
}
27 changes: 25 additions & 2 deletions src/Generators/EntityGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Support\Str;
use RonasIT\Support\Events\WarningEvent;
use RonasIT\Support\Exceptions\ClassNotExistsException;
use RonasIT\Support\Exceptions\IncorrectClassPathException;
use Throwable;
use ReflectionMethod;
use ReflectionClass;
Expand Down Expand Up @@ -84,15 +85,21 @@ public function __construct()
$this->paths = config('entity-generator.paths');
}

protected function getOrCreateNamespace(string $path): string
protected function getOrCreateNamespace(string $configPath): string
{
$path = $this->paths[$path];
$path = $this->paths[$configPath];
$pathParts = explode('/', $path);

if (Str::endsWith(Arr::last($pathParts), '.php')) {
array_pop($pathParts);
}

foreach ($pathParts as $part) {
if (!$this->isFolderHasCorrectCase($part, $configPath)) {
throw new IncorrectClassPathException("Incorrect path to {$configPath}, {$part} folder must start with a capital letter, please specify the path according to the PSR.");
}
}

$namespace = array_map(function (string $part) {
return ucfirst($part);
}, $pathParts);
Expand All @@ -106,6 +113,22 @@ protected function getOrCreateNamespace(string $path): string
return implode('\\', $namespace);
}

protected function isFolderHasCorrectCase(string $folder, string $configPath): bool
{
$lowerCaseDirectoriesMap = [
'migrations' => 'database/migrations',
'factories' => 'database/factories',
'seeders' => 'database/seeders',
'database_seeder' => 'database/seeders',
'tests' => 'tests',
'routes' => 'routes',
];

$directory = Arr::get($lowerCaseDirectoriesMap, $configPath);
Copy link
Collaborator

Choose a reason for hiding this comment

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

could we just cut the correct lower case path from the path?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

for example: $path = database/V1/factories, we can remove database and factories words from $path and check that $path has correct case.


return $folder === 'app' || preg_match('/^[A-Z]/', $folder) || Str::contains($directory, $folder);
}

abstract public function generate(): void;

protected function classExists($path, $name): bool
Expand Down
4 changes: 1 addition & 3 deletions src/Generators/SeederGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

use Illuminate\Support\Arr;
use RonasIT\Support\Events\SuccessCreateMessage;
use RonasIT\Support\Events\WarningEvent;
use RonasIT\Support\Exceptions\EntityCreateException;

class SeederGenerator extends EntityGenerator
{
Expand Down Expand Up @@ -64,7 +62,7 @@ protected function createEntitySeeder(): void
'entity' => $this->model,
'relations' => $this->relations,
'namespace' => $this->getOrCreateNamespace('seeders'),
'modelsNamespace' => $this->getOrCreateNamespace('models'),
'factoryNamespace' => $this->getOrCreateNamespace('factories'),
]);

$seederPath = "{$this->seedsPath}/{$this->model}Seeder.php";
Expand Down
18 changes: 9 additions & 9 deletions stubs/seeder.blade.php
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
namespace {{$namespace}};

use Illuminate\Database\Seeder;
use {{$modelsNamespace}}\{{$entity}};
use {{$factoryNamespace}}\{{$entity}}Factory;

class {{$entity}}Seeder extends Seeder
{
public function run()
{
@if (empty($relations['belongsTo']))
@if(empty(array_filter($relations)))
{{$entity}}::factory()->create();
{{$entity}}Factory::new()->create();
@else
${{strtolower($entity)}} = {{$entity}}::factory()->create();
${{strtolower($entity)}} = {{$entity}}Factory::new()->create();
@endif
@else
@if(empty(array_filter($relations)))
${{strtolower($entity)}} = {{$entity}}::factory()->make([
${{strtolower($entity)}} = {{$entity}}Factory::new()->make([
@else
{{$entity}}::factory()->make([
{{$entity}}Factory::new()->make([
@endif
@foreach($relations['belongsTo'] as $relation)
'{{strtolower($relation)}}_id' => \{{$modelsNamespace}}\{{$relation}}::factory()->create()->id,
'{{strtolower($relation)}}_id' => \{{$factoryNamespace}}\{{$relation}}Factory::new()->create()->id,
@endforeach
]);
@endif

@foreach($relations['hasOne'] as $relation)
\{{$modelsNamespace}}\{{$relation}}::factory()->make([
\{{$factoryNamespace}}\{{$relation}}Factory::new()->make([
'{{strtolower($entity)}}_id' => ${{strtolower($entity)}}->id,
]);

@endforeach
@foreach($relations['hasMany'] as $relation)
\{{$modelsNamespace}}\{{$relation}}::factory()->count(10)->make([
\{{$factoryNamespace}}\{{$relation}}Factory::new()->count(10)->make([
'{{strtolower($entity)}}_id' => ${{strtolower($entity)}}->id,
]);

@endforeach
@foreach($relations['belongsToMany'] as $relation)
$list = \{{$modelsNamespace}}\{{$relation}}::factory()->count(10)->create()->pluck('id');
$list = \{{$factoryNamespace}}\{{$relation}}Factory::new()->count(10)->create()->pluck('id');
${{strtolower($entity)}}->{{strtolower($relation)}}s()->sync($list);
@endforeach
}
Expand Down
8 changes: 4 additions & 4 deletions tests/fixtures/SeederGeneratorTest/post_seeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\Post;
use Database\Factories\PostFactory;

class PostSeeder extends Seeder
{
public function run()
{
Post::factory()->make([
'user_id' => \App\Models\User::factory()->create()->id,
PostFactory::new()->make([
'user_id' => \Database\Factories\UserFactory::new()->create()->id,
]);

\App\Models\Comment::factory()->count(10)->make([
\Database\Factories\CommentFactory::new()->count(10)->make([
'post_id' => $post->id,
]);

Expand Down
Loading