Skip to content

Commit dff4702

Browse files
committed
Merge remote-tracking branch 'origin/master' into 49-add-service-provider-tests
2 parents b259c30 + 882d04c commit dff4702

File tree

11 files changed

+168
-315
lines changed

11 files changed

+168
-315
lines changed

composer.lock

Lines changed: 96 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace RonasIT\Support\Exceptions;
4+
5+
use Exception;
6+
7+
class IncorrectClassPathException extends Exception
8+
{
9+
}

src/Generators/EntityGenerator.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Illuminate\Support\Str;
1010
use RonasIT\Support\Events\WarningEvent;
1111
use RonasIT\Support\Exceptions\ClassNotExistsException;
12+
use RonasIT\Support\Exceptions\IncorrectClassPathException;
1213
use Throwable;
1314
use ReflectionMethod;
1415
use ReflectionClass;
@@ -23,6 +24,15 @@ abstract class EntityGenerator
2324
'boolean-required', 'boolean', 'timestamp-required', 'timestamp', 'json'
2425
];
2526

27+
const LOVER_CASE_DIRECTORIES_MAP = [
28+
'migrations' => 'database/migrations',
29+
'factories' => 'database/factories',
30+
'seeders' => 'database/seeders',
31+
'database_seeder' => 'database/seeders',
32+
'tests' => 'tests',
33+
'routes' => 'routes',
34+
];
35+
2636
protected $paths = [];
2737
protected $model;
2838
protected $fields;
@@ -84,15 +94,21 @@ public function __construct()
8494
$this->paths = config('entity-generator.paths');
8595
}
8696

87-
protected function getOrCreateNamespace(string $path): string
97+
protected function getOrCreateNamespace(string $configPath): string
8898
{
89-
$path = $this->paths[$path];
99+
$path = $this->paths[$configPath];
90100
$pathParts = explode('/', $path);
91101

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

106+
foreach ($pathParts as $part) {
107+
if (!$this->isFolderHasCorrectCase($part, $configPath)) {
108+
throw new IncorrectClassPathException("Incorrect path to {$configPath}, {$part} folder must start with a capital letter, please specify the path according to the PSR.");
109+
}
110+
}
111+
96112
$namespace = array_map(function (string $part) {
97113
return ucfirst($part);
98114
}, $pathParts);
@@ -106,6 +122,15 @@ protected function getOrCreateNamespace(string $path): string
106122
return implode('\\', $namespace);
107123
}
108124

125+
protected function isFolderHasCorrectCase(string $folder, string $configPath): bool
126+
{
127+
$directory = Arr::get(self::LOVER_CASE_DIRECTORIES_MAP, $configPath);
128+
129+
$firstFolderChar = substr($folder, 0, 1);
130+
131+
return $folder === 'app' || (ucfirst($firstFolderChar) === $firstFolderChar) || Str::contains($directory, $folder);
132+
}
133+
109134
abstract public function generate(): void;
110135

111136
protected function classExists($path, $name): bool

src/Generators/SeederGenerator.php

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

55
use Illuminate\Support\Arr;
66
use RonasIT\Support\Events\SuccessCreateMessage;
7-
use RonasIT\Support\Events\WarningEvent;
8-
use RonasIT\Support\Exceptions\EntityCreateException;
97

108
class SeederGenerator extends EntityGenerator
119
{
@@ -64,7 +62,7 @@ protected function createEntitySeeder(): void
6462
'entity' => $this->model,
6563
'relations' => $this->relations,
6664
'namespace' => $this->getOrCreateNamespace('seeders'),
67-
'modelsNamespace' => $this->getOrCreateNamespace('models'),
65+
'factoryNamespace' => $this->getOrCreateNamespace('factories'),
6866
]);
6967

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

stubs/seeder.blade.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,44 @@
11
namespace {{$namespace}};
22

33
use Illuminate\Database\Seeder;
4-
use {{$modelsNamespace}}\{{$entity}};
4+
use {{$factoryNamespace}}\{{$entity}}Factory;
55

66
class {{$entity}}Seeder extends Seeder
77
{
88
public function run()
99
{
1010
@if (empty($relations['belongsTo']))
1111
@if(empty(array_filter($relations)))
12-
{{$entity}}::factory()->create();
12+
{{$entity}}Factory::new()->create();
1313
@else
14-
${{strtolower($entity)}} = {{$entity}}::factory()->create();
14+
${{strtolower($entity)}} = {{$entity}}Factory::new()->create();
1515
@endif
1616
@else
1717
@if(empty(array_filter($relations)))
18-
${{strtolower($entity)}} = {{$entity}}::factory()->make([
18+
${{strtolower($entity)}} = {{$entity}}Factory::new()->make([
1919
@else
20-
{{$entity}}::factory()->make([
20+
{{$entity}}Factory::new()->make([
2121
@endif
2222
@foreach($relations['belongsTo'] as $relation)
23-
'{{strtolower($relation)}}_id' => \{{$modelsNamespace}}\{{$relation}}::factory()->create()->id,
23+
'{{strtolower($relation)}}_id' => \{{$factoryNamespace}}\{{$relation}}Factory::new()->create()->id,
2424
@endforeach
2525
]);
2626
@endif
2727

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

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

3939
@endforeach
4040
@foreach($relations['belongsToMany'] as $relation)
41-
$list = \{{$modelsNamespace}}\{{$relation}}::factory()->count(10)->create()->pluck('id');
41+
$list = \{{$factoryNamespace}}\{{$relation}}Factory::new()->count(10)->create()->pluck('id');
4242
${{strtolower($entity)}}->{{strtolower($relation)}}s()->sync($list);
4343
@endforeach
4444
}

tests/Support/Translation/TranslationMockTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ trait TranslationMockTrait
1111

1212
public function mockFilesystem(): void
1313
{
14-
$fileSystemMock = new FileSystemMock;
14+
$fileSystemMock = new FileSystemMock();
1515

1616
$fileSystemMock->translations = [];
1717

@@ -22,7 +22,7 @@ public function mockFilesystemForAppend(): void
2222
{
2323
$validation = file_get_contents(getcwd() . '/tests/Support/Translation/validation_without_exceptions.php');
2424

25-
$fileSystemMock = new FileSystemMock;
25+
$fileSystemMock = new FileSystemMock();
2626

2727
$fileSystemMock->translations = ['validation.php' => $validation];
2828

0 commit comments

Comments
 (0)