Skip to content

Commit

Permalink
WIP add spatie generate routes file
Browse files Browse the repository at this point in the history
  • Loading branch information
axyr committed Aug 15, 2024
1 parent 3bbc69a commit 118f944
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 13 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"require": {
"php": "^8.2",
"laravel/framework": "^11.0",
"wikimedia/composer-merge-plugin": "^2.1"
"wikimedia/composer-merge-plugin": "^2.1",
"spatie/laravel-permission": "^6.0"
},
"require-dev": {
"roave/security-advisories": "dev-latest",
Expand Down
5 changes: 5 additions & 0 deletions config/crudgenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@
'composer' => [
'vendor_prefix' => 'app',
],

'routes' => [
// Make sure this file is added to bootstrap/app.php
'default_file' => 'routes/web.php',
],
];
File renamed without changes.
2 changes: 1 addition & 1 deletion resources/stubs/Controller.stub
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace {{namespace}};

use App\Http\Controllers\Controller;
use Illuminate\Routing\Controller;
use {{baseNamespace}}\{{moduleName}}\Models\{{modelName}};
use {{baseNamespace}}\{{moduleName}}\Repositories\{{modelName}}Repository;
use {{baseNamespace}}\{{moduleName}}\Http\Requests\{{modelName}}Request;
Expand Down
6 changes: 6 additions & 0 deletions resources/stubs/Routes.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

use {{controllerNamespace}}\{{controllerName}};
use Illuminate\Support\Facades\Route;

Route::apiResource('{{routeName}}', {{controllerName}}::class);
10 changes: 9 additions & 1 deletion src/Generators/AbstractGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@ public function __construct(protected string $name, protected ?string $module =
}
}

abstract public function directory(): string;
public static function new(string $name, ?string $module = null): static
{
return new ControllerGenerator($name, $module);
}

public function directory(): string
{
return '';
}

public function stubName(): string
{
Expand Down
1 change: 1 addition & 0 deletions src/Generators/CombinedGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function generators(): array
RequestGenerator::class,
ResourceGenerator::class,
PermissionSeederGenerator::class,
ModuleServiceProviderGenerator::class,
ComposerGenerator::class,
];
}
Expand Down
5 changes: 0 additions & 5 deletions src/Generators/ComposerGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@

class ComposerGenerator extends AbstractGenerator
{
public function directory(): string
{
return '';
}

public function path(): string
{
$parts = array_filter([$this->basePath(), $this->module(), 'composer.json']);
Expand Down
5 changes: 0 additions & 5 deletions src/Generators/ModuleServiceProviderGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@

class ModuleServiceProviderGenerator extends AbstractGenerator
{
public function directory(): string
{
return '';
}

public function className(): string
{
return sprintf('%sServiceProvider', Str::singular($this->module()));
Expand Down
27 changes: 27 additions & 0 deletions src/Generators/RoutesGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Axyr\CrudGenerator\Generators;

class RoutesGenerator extends AbstractGenerator
{
public function path(): string
{
$parts = array_filter([$this->basePath(), $this->module(), 'routes.php']);

return sprintf('%s', implode('/', $parts));
}

public function controllerGenerator(): ControllerGenerator
{
return ControllerGenerator::new($this->name, $this->module);
}

public function replacements(): array
{
return array_merge(parent::replacements(), [
'{{routeName}}' => $this->variableNamePlural(),
'{{controllerName}}' => $this->controllerGenerator()->className(),
'{{controllerNamespace}}' => $this->controllerGenerator()->namespace(),
]);
}
}
2 changes: 2 additions & 0 deletions tests/Generators/CombinedGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public function testGenerateAllFiles(): void
'app-modules/Posts/src/Http/Requests/CommentRequest.php',
'app-modules/Posts/src/Http/Resources/CommentResource.php',
'app-modules/Posts/src/Seeders/CommentPermissionSeeder.php',
'app-modules/Posts/src/PostServiceProvider.php',
'app-modules/Posts/composer.json',
];

$this->assertEquals($expectedGeneratedFiles, $generator->generatedFiles());
Expand Down
28 changes: 28 additions & 0 deletions tests/Generators/RoutesGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Axyr\CrudGenerator\Tests\Generators;

use Axyr\CrudGenerator\Generators\RoutesGenerator;

class RoutesGeneratorTest extends GeneratorTestAbstract
{
public function generatorClassName(): string
{
return RoutesGenerator::class;
}

public static function dataGenerator(): array
{
return [
[
'name' => 'Comment',
'module' => 'Posts',
'expectedPath' => 'app-modules/Posts/routes.php',
'expectedStrings' => [
'use App\Modules\Posts\Http\Controllers\CommentController;',
"Route::apiResource('comments', CommentController::class);",
],
],
];
}
}

0 comments on commit 118f944

Please sign in to comment.