Skip to content
Merged
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
2 changes: 1 addition & 1 deletion src/Generators/AbstractTestsGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ protected function getMockModel($model): array
return [];
}

$factoryNamespace = "{$this->getNamespace('factories')}\\{$model}Factory";
$factoryNamespace = "{$this->generateNamespace($this->paths['factories'])}\\{$model}Factory";
$factory = $factoryNamespace::new();

return $factory
Expand Down
10 changes: 5 additions & 5 deletions src/Generators/ControllerGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ protected function getControllerContent($model): string
return $this->getStub('controller', [
'entity' => $model,
'requestsFolder' => $model,
'namespace' => $this->getNamespace('controllers'),
'requestsNamespace' => $this->getNamespace('requests'),
'resourcesNamespace' => $this->getNamespace('resources'),
'servicesNamespace' => $this->getNamespace('services'),
'namespace' => $this->generateNamespace($this->paths['controllers']),
'requestsNamespace' => $this->generateNamespace($this->paths['requests']),
'resourcesNamespace' => $this->generateNamespace($this->paths['resources']),
'servicesNamespace' => $this->generateNamespace($this->paths['services']),
]);
}

Expand Down Expand Up @@ -98,7 +98,7 @@ protected function addUseController(string $routesPath): void
$routesFileContent = file_get_contents($routesPath);

$stub = $this->getStub('use_routes', [
'namespace' => $this->getNamespace('controllers'),
'namespace' => $this->generateNamespace($this->paths['controllers']),
'entity' => $this->model
]);

Expand Down
40 changes: 25 additions & 15 deletions src/Generators/EntityGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ abstract class EntityGenerator
'database_seeder' => 'database/seeders',
'tests' => 'tests',
'routes' => 'routes',
'translations' => 'lang/en',
];

protected $paths = [];
Expand Down Expand Up @@ -88,11 +89,13 @@ public function setRelations(RelationsDTO $relations): self
public function __construct()
{
$this->paths = config('entity-generator.paths');

$this->checkConfigHasCorrectPaths();
}

protected function getNamespace(string $configPath, ?string $subFolder = null): string
protected function generateNamespace(string $path, ?string $additionalSubFolder = null): string
{
$pathParts = $this->getNamespacePathParts($configPath, $subFolder);
$pathParts = $this->getNamespacePathParts($path, $additionalSubFolder);

$namespace = array_map(fn (string $part) => ucfirst($part), $pathParts);

Expand All @@ -101,7 +104,7 @@ protected function getNamespace(string $configPath, ?string $subFolder = null):

protected function createNamespace(string $configPath, ?string $subFolder = null): void
{
$path = $this->getPath($configPath, $subFolder);
$path = $this->getPath($this->paths[$configPath], $subFolder);

$fullPath = base_path($path);

Expand All @@ -110,26 +113,20 @@ protected function createNamespace(string $configPath, ?string $subFolder = null
}
}

protected function getNamespacePathParts(string $configPath, ?string $subFolder = null): array
protected function getNamespacePathParts(string $path, ?string $additionalSubFolder = null): array
{
$pathParts = explode('/', $this->getPath($configPath, $subFolder));
$pathParts = explode('/', $this->getPath($path, $additionalSubFolder));

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.");
}
}

return $pathParts;
}

protected function getPath(string $configPath, ?string $subFolder = null): string
protected function getPath(string $path, ?string $subFolder = null): string
{
return when($subFolder, fn () => Str::finish($this->paths[$configPath], '/') . $subFolder, $this->paths[$configPath]);
return when($subFolder, fn () => Str::finish($path, '/') . $subFolder, $path);
}

protected function isFolderHasCorrectCase(string $folder, string $configPath): bool
Expand All @@ -154,7 +151,7 @@ protected function classExists(string $path, string $name, ?string $subFolder =

protected function getClassPath(string $path, string $name, ?string $subFolder = null): string
{
$path = $this->getPath($path, $subFolder);
$path = $this->getPath($this->paths[$path], $subFolder);

return "{$path}/{$name}.php";
}
Expand Down Expand Up @@ -269,7 +266,7 @@ protected function getModelClass(string $model): string
{
$subfolder = when($model === $this->model, $this->modelSubFolder);

$modelNamespace = $this->getNamespace('models', $subfolder);
$modelNamespace = $this->generateNamespace($this->paths['models'], $subfolder);

return "{$modelNamespace}\\{$model}";
}
Expand Down Expand Up @@ -308,4 +305,17 @@ protected function pathToNamespace(string $name): string
{
return ucwords(Str::replace('/', '\\', $name), '\\');
}

protected function checkConfigHasCorrectPaths(): void
{
foreach ($this->paths as $configPath => $path) {
$pathParts = $this->getNamespacePathParts($path);

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.");
}
}
}
}
}
4 changes: 2 additions & 2 deletions src/Generators/FactoryGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ public function generate(): void
$this->createNamespace('factories');

$factoryContent = $this->getStub('factory', [
'namespace' => $this->getNamespace('factories'),
'namespace' => $this->generateNamespace($this->paths['factories']),
'entity' => $this->model,
'fields' => $this->prepareFields(),
'modelNamespace' => $this->getNamespace('models', $this->modelSubFolder),
'modelNamespace' => $this->generateNamespace($this->paths['models'], $this->modelSubFolder),
]);

$this->saveClass('factories', "{$this->model}Factory", $factoryContent);
Expand Down
4 changes: 2 additions & 2 deletions src/Generators/ModelGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected function getNewModelContent(): string
'fields' => Arr::collapse($this->fields),
'relations' => $this->prepareRelations(),
'casts' => $this->getCasts($this->fields),
'namespace' => $this->getNamespace('models', $this->modelSubFolder),
'namespace' => $this->generateNamespace($this->paths['models'], $this->modelSubFolder),
'importRelations' => $this->getImportedRelations(),
'anotationProperties' => $this->generateAnnotationProperties($this->fields),
'hasCarbonField' => !empty($this->fields['timestamp']) || !empty($this->fields['timestamp-required']),
Expand Down Expand Up @@ -189,7 +189,7 @@ protected function shouldImportRelation(string $relation): bool

protected function generateClassNamespace(string $className, ?string $folder = null): string
{
$path = $this->getNamespace('models', $folder);
$path = $this->generateNamespace($this->paths['models'], $folder);
$psrPath = $this->pathToNamespace($className);

return "{$path}\\{$psrPath}";
Expand Down
6 changes: 3 additions & 3 deletions src/Generators/NovaResourceGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function generate(): void
'fields' => $novaFields,
'types' => array_unique(data_get($novaFields, '*.type')),
'imports' => $this->getImports(),
'namespace' => $this->getNamespace('nova', $this->modelSubFolder),
'namespace' => $this->generateNamespace($this->paths['nova'], $this->modelSubFolder),
]);

$this->saveClass('nova', "{$this->model}Resource", $fileContent, $this->modelSubFolder);
Expand Down Expand Up @@ -179,11 +179,11 @@ protected function getColumnList(string $table, ?string $connectionName = null):
protected function getImports(): array
{
$imports = [
"{$this->getNamespace('models', $this->modelSubFolder)}\\{$this->model}",
"{$this->generateNamespace($this->paths['models'], $this->modelSubFolder)}\\{$this->model}",
];

if (!empty($this->modelSubFolder)) {
$imports[] = "{$this->getNamespace('nova')}\\Resource";
$imports[] = "{$this->generateNamespace($this->paths['nova'])}\\Resource";
}

return $imports;
Expand Down
4 changes: 2 additions & 2 deletions src/Generators/NovaTestGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function generateTests(): void
$resourceClass = Str::afterLast($this->novaResourceClassName, '\\');

$fileContent = $this->getStub('nova_test', [
'entity_namespace' => $this->getNamespace('models', $this->modelSubFolder),
'entity_namespace' => $this->generateNamespace($this->paths['models'], $this->modelSubFolder),
'entity' => $this->model,
'resource_name' => $resourceClass,
'resource_namespace' => $this->novaResourceClassName,
Expand All @@ -78,7 +78,7 @@ public function generateTests(): void
'lower_entities' => $this->getPluralName(Str::snake($this->model)),
'actions' => $actions,
'filters' => $filters,
'models_namespace' => $this->getNamespace('models'),
'models_namespace' => $this->generateNamespace($this->paths['models']),
]);

$this->saveClass('tests', "Nova{$this->model}ResourceTest", $fileContent);
Expand Down
4 changes: 2 additions & 2 deletions src/Generators/RepositoryGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public function generate(): void

$repositoryContent = $this->getStub('repository', [
'entity' => $this->model,
'namespace' => $this->getNamespace('repositories'),
'modelNamespace' => $this->getNamespace('models', $this->modelSubFolder)
'namespace' => $this->generateNamespace($this->paths['repositories']),
'modelNamespace' => $this->generateNamespace($this->paths['models'], $this->modelSubFolder)
]);

$this->saveClass('repositories', "{$this->model}Repository", $repositoryContent);
Expand Down
4 changes: 2 additions & 2 deletions src/Generators/RequestsGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ protected function createRequest($method, $needToValidate = true, $parameters =
'parameters' => $parameters,
'needToValidate' => $needToValidate,
'requestsFolder' => $requestsFolder,
'namespace' => $this->getNamespace('requests'),
'servicesNamespace' => $this->getNamespace('services'),
'namespace' => $this->generateNamespace($this->paths['requests']),
'servicesNamespace' => $this->generateNamespace($this->paths['services']),
'entityNamespace' => $this->getModelClass($this->model),
'needToValidateWith' => !is_null(Arr::first($parameters, fn ($parameter) => $parameter['name'] === 'with.*')),
]);
Expand Down
6 changes: 3 additions & 3 deletions src/Generators/ResourceGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function generateCollectionResource(): void
$collectionResourceContent = $this->getStub('collection_resource', [
'singular_name' => $this->model,
'plural_name' => $pluralName,
'namespace' => $this->getNamespace('resources')
'namespace' => $this->generateNamespace($this->paths['resources']),
]);

$this->saveClass('resources', "{$pluralName}CollectionResource", $collectionResourceContent, $this->model);
Expand All @@ -52,8 +52,8 @@ public function generateResource(): void

$resourceContent = $this->getStub('resource', [
'entity' => $this->model,
'namespace' => $this->getNamespace('resources'),
'model_namespace' => $this->getNamespace('models', $this->modelSubFolder),
'namespace' => $this->generateNamespace($this->paths['resources']),
'model_namespace' => $this->generateNamespace($this->paths['models'], $this->modelSubFolder),
'fields' => when($this->fields, fn () => Arr::flatten($this->fields)),
]);

Expand Down
6 changes: 3 additions & 3 deletions src/Generators/SeederGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function generate(): void
protected function createDatabaseSeeder(): void
{
$content = "<?php\n\n" . $this->getStub('database_empty_seeder', [
'namespace' => $this->getNamespace('seeders')
'namespace' => $this->generateNamespace($this->paths['seeders']),
]);

file_put_contents($this->databaseSeederPath, $content);
Expand All @@ -51,8 +51,8 @@ protected function createEntitySeeder(): void
$content = "<?php\n\n" . $this->getStub('seeder', [
'entity' => $this->model,
'relations' => $this->prepareRelations(),
'namespace' => $this->getNamespace('seeders'),
'factoryNamespace' => $this->getNamespace('factories'),
'namespace' => $this->generateNamespace($this->paths['seeders']),
'factoryNamespace' => $this->generateNamespace($this->paths['factories']),
]) . "\n";

$seederPath = "{$this->seedsPath}/{$this->model}Seeder.php";
Expand Down
4 changes: 2 additions & 2 deletions src/Generators/ServiceGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public function generate(): void
$serviceContent = $this->getStub('service', [
'entity' => $this->model,
'fields' => $this->getFields(),
'namespace' => $this->getNamespace('services'),
'repositoriesNamespace' => $this->getNamespace('repositories'),
'namespace' => $this->generateNamespace($this->paths['services']),
'repositoriesNamespace' => $this->generateNamespace($this->paths['repositories']),
]);

$this->saveClass('services', "{$this->model}Service", $serviceContent);
Expand Down
4 changes: 2 additions & 2 deletions src/Generators/TestsGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ protected function generateTests(): void
'databaseTableName' => $this->getTableName($this->model),
'entities' => $this->getTableName($this->model, '-'),
'withAuth' => $this->withAuth,
'entityNamespace' => $this->getNamespace('models', $this->modelSubFolder),
'userNamespace' => $this->getNamespace('models'),
'entityNamespace' => $this->generateNamespace($this->paths['models'], $this->modelSubFolder),
'userNamespace' => $this->generateNamespace($this->paths['models']),
'hasModificationEndpoints' => !empty(array_intersect($this->crudOptions, ['C', 'U', 'D'])),
]);

Expand Down