Skip to content

Commit 0e98c96

Browse files
author
Admin
committed
Add decorated stubs and fixed model camel case
1 parent 01856fa commit 0e98c96

10 files changed

+227
-8
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ Library for [laravel-crud-wizard-free](https://github.com/macropay-solutions/lar
1616

1717
## Usage
1818

19-
``` php artisan make:api-resource {resourceName} ```
19+
``` php artisan make:api-resource {resourceName} {--decorated}```
2020

21-
This will create a template for **controller**, **service** and **model** and will print instructions on what is left to be done manually.
21+
This will create a template for **controller**, **service** and **model** (optionally, with the --decorated flag it will create also a **decorator** and **middleware**) and will print instructions on what is left to be done manually.
2222

2323
Example:
2424
```
@@ -31,6 +31,12 @@ TODO:
3131
- Fill the model's properties,
3232
- Define validations in controller,
3333
- Expose resource in DbCrudMap::MODEL_FQN_TO_CONTROLLER_MAP.
34+
Created Decorator: /var/www/html/project/app/Decorators/OperationDecorator.php
35+
Created Middleware: /var/www/html/project/app/Http/Midleware/Decorators/OperationsMiddleware.php
36+
TODO:
37+
- Fill the decorator,
38+
- Register middleware decorator as route middleware,
39+
- Use the middleware alias as middleware in your crud route definition for each method.
3440
```
3541

3642

src/Console/MakeLaravelCrudWizard.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use Illuminate\Console\Command;
66
use MacropaySolutions\LaravelCrudWizardGenerator\Services\MakeControllerService;
7+
use MacropaySolutions\LaravelCrudWizardGenerator\Services\MakeDecoratorService;
8+
use MacropaySolutions\LaravelCrudWizardGenerator\Services\MakeMiddlewareService;
79
use MacropaySolutions\LaravelCrudWizardGenerator\Services\MakeModelService;
810
use MacropaySolutions\LaravelCrudWizardGenerator\Services\MakeServiceService;
911

@@ -14,7 +16,7 @@ class MakeLaravelCrudWizard extends Command
1416
*
1517
* @var string
1618
*/
17-
protected $signature = 'make:api-resource {resourceName}';
19+
protected $signature = 'make:api-resource {resourceName} {--decorated}';
1820

1921
/**
2022
* The console command description.
@@ -26,7 +28,9 @@ class MakeLaravelCrudWizard extends Command
2628
public function __construct(
2729
protected MakeControllerService $makeControllerService,
2830
protected MakeModelService $makeModelService,
29-
protected MakeServiceService $makeServiceService
31+
protected MakeServiceService $makeServiceService,
32+
protected MakeDecoratorService $makeDecoratorService,
33+
protected MakeMiddlewareService $makeMiddlewareService
3034
) {
3135
parent::__construct();
3236
}
@@ -40,5 +44,12 @@ public function handle(): void
4044
$this->makeModelService->makeCompleteModelFile($resourceName, $ns = $this->laravel->getNamespace());
4145
$this->makeServiceService->makeCompleteServiceFile($resourceName, $ns);
4246
$this->makeControllerService->makeCompleteControllerFile($resourceName, $ns);
47+
48+
if (!$this->option('decorated')) {
49+
return;
50+
}
51+
52+
$this->makeDecoratorService->makeCompleteDecoratorFile($resourceName, $ns);
53+
$this->makeMiddlewareService->makeCompleteMiddlewareFile($resourceName, $ns);
4354
}
4455
}

src/Services/MakeControllerService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ protected function replaceContentControllerStub(string $resourceName, string $la
3737
'DummyClass',
3838
], [
3939
\trim($laravelNamespace, '\\'),
40-
\ucfirst(Str::singular($resourceName)),
40+
\ucfirst(Str::camel(Str::singular($resourceName))),
4141
($plural = \ucfirst(Str::camel(Str::plural($resourceName)))) . 'Service',
4242
$plural . 'Controller',
4343
], File::get($this->pathsAndNamespacesService->getControllerStubPath()));
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace MacropaySolutions\LaravelCrudWizardGenerator\Services;
4+
5+
use Illuminate\Console\Concerns\InteractsWithIO;
6+
use Illuminate\Support\Facades\File;
7+
use Illuminate\Support\Str;
8+
use Symfony\Component\Console\Output\ConsoleOutput;
9+
10+
class MakeDecoratorService
11+
{
12+
use InteractsWithIO;
13+
14+
public function __construct(
15+
protected PathsAndNamespacesService $pathsAndNamespacesService,
16+
ConsoleOutput $consoleOutput,
17+
) {
18+
$this->output = $consoleOutput;
19+
}
20+
21+
public function makeCompleteDecoratorFile(string $resourceName, string $laravelNamespace): void
22+
{
23+
$this->createDecoratorFile($this->replaceContentDecoratorStub($resourceName, $laravelNamespace), $resourceName);
24+
}
25+
26+
protected function replaceContentDecoratorStub(string $resourceName, string $laravelNamespace): string
27+
{
28+
return \str_replace([
29+
'DummyNamespace',
30+
'DummyClass',
31+
], [
32+
\trim($laravelNamespace, '\\'),
33+
\ucfirst(Str::camel(Str::singular($resourceName))) . 'Decorator',
34+
], File::get($this->pathsAndNamespacesService->getDecoratorStubPath()));
35+
}
36+
37+
protected function createDecoratorFile(string $decoratorStub, string $resourceName): void
38+
{
39+
if (!File::exists($path = $this->pathsAndNamespacesService->getRealpathBaseDecorator())) {
40+
File::makeDirectory($path);
41+
}
42+
43+
if (!File::exists($path = $this->pathsAndNamespacesService->getRealpathBaseCustomDecorator($resourceName))) {
44+
File::put($path, $decoratorStub);
45+
$this->line('<info>Created Decorator:</info> ' . $path);
46+
47+
return;
48+
}
49+
50+
$this->error('Decorator ' . $path . ' already exists');
51+
}
52+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace MacropaySolutions\LaravelCrudWizardGenerator\Services;
4+
5+
use Illuminate\Console\Concerns\InteractsWithIO;
6+
use Illuminate\Contracts\Foundation\Application;
7+
use Illuminate\Support\Facades\File;
8+
use Illuminate\Support\Str;
9+
use Symfony\Component\Console\Output\ConsoleOutput;
10+
11+
class MakeMiddlewareService
12+
{
13+
use InteractsWithIO;
14+
15+
public function __construct(
16+
protected PathsAndNamespacesService $pathsAndNamespacesService,
17+
protected Application $application,
18+
ConsoleOutput $consoleOutput,
19+
) {
20+
$this->output = $consoleOutput;
21+
}
22+
23+
public function makeCompleteMiddlewareFile(string $resourceName, string $laravelNamespace): void
24+
{
25+
$this->createMiddlewareFile(
26+
$this->replaceContentMiddlewareStub($resourceName, $laravelNamespace),
27+
$resourceName
28+
);
29+
}
30+
31+
protected function replaceContentMiddlewareStub(string $resourceName, string $laravelNamespace): string
32+
{
33+
return \str_replace([
34+
'DummyNamespace',
35+
'DummyModel',
36+
'DummyClass',
37+
], [
38+
\trim($laravelNamespace, '\\'),
39+
\ucfirst(Str::camel(Str::singular($resourceName))),
40+
\ucfirst(Str::camel(Str::plural($resourceName))) . 'Middleware',
41+
], File::get($this->pathsAndNamespacesService->getMiddlewareStubPath()));
42+
}
43+
44+
protected function createMiddlewareFile(string $middlewareStub, string $resourceName): void
45+
{
46+
if (!File::exists($path = $this->pathsAndNamespacesService->getRealpathBaseMiddleware())) {
47+
File::makeDirectory($path);
48+
}
49+
50+
if (!File::exists($path = $this->pathsAndNamespacesService->getRealpathBaseCustomMiddleware($resourceName))) {
51+
File::put($path, $middlewareStub);
52+
$this->line('<info>Created Middleware:</info> ' . $path);
53+
$this->info('TODO:');
54+
$this->info('- Fill the decorator,');
55+
$this->info('- Register middleware decorator as route middleware,');
56+
$this->info('- Use the middleware alias as middleware in your crud route definition for each method.');
57+
58+
return;
59+
}
60+
61+
$this->error('Middleware ' . $path . ' already exists');
62+
}
63+
}

src/Services/MakeModelService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ protected function replaceContentModelStub(string $laravelNamespace, string $res
3434
\trim($laravelNamespace, '\\'),
3535
Str::snake(Str::plural($resourceName), '-'),
3636
Str::snake(Str::plural($resourceName)),
37-
\ucfirst(Str::singular($resourceName)),
37+
\ucfirst(Str::camel(Str::singular($resourceName))),
3838
], File::get($this->pathsAndNamespacesService->getModelStubPath()));
3939
}
4040

src/Services/MakeServiceService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ protected function replaceContentServiceStub(string $resourceName, string $larav
3131
'DummyClass',
3232
], [
3333
\trim($laravelNamespace, '\\'),
34-
\ucfirst(Str::singular($resourceName)),
34+
\ucfirst(Str::camel(Str::singular($resourceName))),
3535
\ucfirst(Str::camel(Str::plural($resourceName))) . 'Service',
3636
], File::get($this->pathsAndNamespacesService->getServiceStubPath()));
3737
}

src/Services/PathsAndNamespacesService.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,25 @@ public function getControllerStubPath(): string
3131
return $this->getStubPath() . DIRECTORY_SEPARATOR . 'Controller.stub';
3232
}
3333

34+
public function getDecoratorStubPath(): string
35+
{
36+
return $this->getStubPath() . DIRECTORY_SEPARATOR . 'Decorator.stub';
37+
}
38+
39+
public function getMiddlewareStubPath(): string
40+
{
41+
return $this->getStubPath() . DIRECTORY_SEPARATOR . 'Middleware.stub';
42+
}
43+
3444
public function getRealpathBaseModel(): string
3545
{
3646
return $this->getRealpathBase('app') . DIRECTORY_SEPARATOR . 'Models';
3747
}
3848

3949
public function getRealpathBaseCustomModel(string $resourceName): string
4050
{
41-
return $this->getRealpathBaseModel() . DIRECTORY_SEPARATOR . \ucfirst(Str::singular($resourceName)) . '.php';
51+
return $this->getRealpathBaseModel() . DIRECTORY_SEPARATOR .
52+
\ucfirst(Str::camel(Str::singular($resourceName))) . '.php';
4253
}
4354

4455
public function getRealpathBaseService(): string
@@ -62,4 +73,26 @@ public function getRealpathBaseCustomController(string $resourceName): string
6273
return $this->getRealpathBaseController() . DIRECTORY_SEPARATOR .
6374
\ucfirst(Str::camel(Str::plural($resourceName))) . 'Controller.php';
6475
}
76+
77+
public function getRealpathBaseDecorator(): string
78+
{
79+
return $this->getRealpathBase('app') . DIRECTORY_SEPARATOR . 'Decorators';
80+
}
81+
82+
public function getRealpathBaseCustomDecorator(string $resourceName): string
83+
{
84+
return $this->getRealpathBaseDecorator() . DIRECTORY_SEPARATOR .
85+
\ucfirst(Str::camel(Str::singular($resourceName))) . 'Decorator.php';
86+
}
87+
88+
public function getRealpathBaseMiddleware(): string
89+
{
90+
return $this->getRealpathBase('app' . DIRECTORY_SEPARATOR . 'Http' . DIRECTORY_SEPARATOR . 'Middleware' . DIRECTORY_SEPARATOR . 'Decorators');
91+
}
92+
93+
public function getRealpathBaseCustomMiddleware(string $resourceName): string
94+
{
95+
return $this->getRealpathBaseMiddleware() . DIRECTORY_SEPARATOR .
96+
\ucfirst(Str::camel(Str::plural($resourceName))) . 'Middleware.php';
97+
}
6598
}

src/stubs/Decorator.stub

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace DummyNamespace\Decorators;
4+
5+
use MacropaySolutions\LaravelCrudWizardDecorator\Decorators\AbstractResourceDecorator
6+
7+
class DummyClass extends AbstractResourceDecorator
8+
{
9+
public function getResourceMappings(): array
10+
{
11+
return [
12+
// 'col' => 'decoratedCol',
13+
];
14+
}
15+
16+
/**
17+
* @inheritDoc
18+
*/
19+
public function getRelationMappings(): array
20+
{
21+
return [
22+
// 'relationName' => [
23+
// 'relColumn' => 'decoratedRelColumn',
24+
// ],
25+
];
26+
}
27+
28+
/**
29+
* @inheritDoc
30+
*/
31+
public function getComposedColumns(): array
32+
{
33+
return [
34+
// 'custom' => fn(array $row): string => '',
35+
];
36+
}
37+
}

src/stubs/Middleware.stub

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace DummyNamespace\Http\Middleware\Decorators;
4+
5+
use DummyNamespace\Models\DummyModel;
6+
use DummyNamespace\Decorators\DummyModelDecorator;
7+
use MacropaySolutions\LaravelCrudWizardDecorator\Http\Middleware\Decorators
8+
9+
class DummyClass extends AbstractDecoratorMiddleware
10+
{
11+
protected string $decoratorClass = DummyModelDecorator::class;
12+
13+
public function setResourceModel(): void
14+
{
15+
$this->resourceModel = new DummyModel();
16+
}
17+
}

0 commit comments

Comments
 (0)