Skip to content
Merged

Crud #10

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
10 changes: 5 additions & 5 deletions code_stubs/AddAction.stub
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ final class {name}
{
public function handle(array $data): {model_name}
{
$model = new {model_name}();
${entity_singular} = new {model_name}();

$model->fill($data);
${entity_singular}->fill($data);

$model->save();
${entity_singular}->save();

$model->refresh();
${entity_singular}->refresh();

return $model;
return ${entity_singular};
}
}
34 changes: 31 additions & 3 deletions code_stubs/Controller.stub
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,55 @@ declare(strict_types=1);
namespace {namespace};

use Illuminate\Http\RedirectResponse;
use Illuminate\Contracts\View\View;
{use_controller}
use {add_action_namespace};
use {edit_action_namespace};
use {request_namespace};
use {model_namespace};

class {name} extends Controller
{
public function index(): View
{
${entity_plural} = {model}::query()->get();

return view('{entity_singular}.table', compact('{entity_plural}'));
}

public function create(): View
{
return view('{entity_singular}.form');
}

public function store({request_name} $request, {add_action_name} $action): RedirectResponse
{
$data = $request->validated();

$model = $action->handle($data);
${entity_singular} = $action->handle($data);

return back();
}

public function edit(string $id, {request_name} $request, {edit_action_name} $action): RedirectResponse
public function edit(string $id)
{
${entity_singular} = {model}::query()->where('id', $id)->firstOrFail();

return view('{entity_singular}.form', compact('{entity_singular}'));
}

public function update(string $id, {request_name} $request, {edit_action_name} $action): RedirectResponse
{
$data = $request->validated();

$model = $action->handle((int) $id, $data);
${entity_singular} = $action->handle((int) $id, $data);

return back();
}

public function destroy(string $id)
{
{model}::query()->where('id', $id)->delete();

return back();
}
Expand Down
18 changes: 11 additions & 7 deletions code_stubs/EditAction.stub
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@ use {model_namespace};

final class {name}
{
public function handle(int $id, array $data): {model_name}
public function handle(int $id, array $data): ?{model_name}
{
$model = {model_name}::query()->where('id', $id)->first();

$model->fill($data);
${entity_singular} = {model_name}::query()->where('id', $id)->first();

$model->save();
if(is_null(${entity_singular})) {
return null;
}

$model->refresh();
${entity_singular}->fill($data);

return $model;
${entity_singular}->save();

${entity_singular}->refresh();

return ${entity_singular};
}
}
2 changes: 1 addition & 1 deletion code_stubs/Form.stub
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<form action="{{ route('{name}.store') }}" method="POST">
<form action="{{ route('{entity_plural}.store') }}" method="POST">
@csrf{inputs}
<button type="submit">Submit</button>
</form>
10 changes: 7 additions & 3 deletions code_stubs/Route.stub
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ declare(strict_types=1);
use {controller_namespace};
use Illuminate\Support\Facades\Route;

Route::prefix('{name}')->controller({controller_name}::class)->group(function (): void {
Route::post('/', 'store')->name('{name}.store');
Route::post('/{id}', 'edit')->name('{name}.edit');
Route::prefix('{entity_plural}')->controller({controller_name}::class)->group(function (): void {
Route::get('/', 'index')->name('{entity_plural}.index');
Route::get('/create', 'create')->name('{entity_plural}.create');
Route::post('/', 'store')->name('{entity_plural}.store');
Route::get('/{id}/edit', 'edit')->name('{entity_plural}.edit');
Route::put('/{id}', 'update')->name('{entity_plural}.update');
Route::delete('/{id}', 'destroy')->name('{entity_plural}.destroy');
});
12 changes: 12 additions & 0 deletions code_stubs/Table.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<table>
<thead>
<tr>{thead}
</tr>
</thead>
<tbody>
@foreach(${entity_plural} as ${entity_singular})
<tr>{tbody}
</tr>
@endforeach
</tbody>
</table>
1 change: 1 addition & 0 deletions config/code_builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
BuildType::ROUTE,
BuildType::FORM,
BuildType::DTO,
BuildType::TABLE,
// Additionally
],

Expand Down
72 changes: 39 additions & 33 deletions docs/for_contributors.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@
- [ ] Livewire support
- [ ] Inertia support
- [ ] SpatieLaravelData support
- [ ] MoonShine support


### How to add support for new code generation
1. Add a new type to Enums/BuildType.php, `job` for example.
2. Add new CodePath type in Services/CodePath, for example:
1. Add a new type to Enums/BuildType.php, `JOB` for example.
2. Add new CodePath type in Services/CodePath/Advanced, for example:
```php
<?php

Expand All @@ -29,32 +28,31 @@ namespace DevLnk\LaravelCodeBuilder\Services\CodePath;

readonly class JobPath extends AbstractPath
{

}
```
3. Add a new method to Services/CodePath/CodePath.php for your `job`
```php
public function job(string $name, string $dir, string $namespace): self
{
if(isset($this->paths[BuildType::JOB->value])) {
return $this;
public function getBuildType(): BuildType
{
return BuildType::JOB;
}
$this->paths[BuildType::JOB->value] = new JobPath($name, $dir, $namespace);
return $this;
}
```
4. In the `prepareGeneration` method of the `Commands/LaravelCodeBuildCommand.php` file, find the place where all the directories for your builder are written:
3. Add a new code to Services/CodePath/CodePath.php for your `job`
```php
$this->codePath
->job(
$this->codeStructure->entity()->ucFirstSingular() . '.php',
$isGenerationDir ? $genPath . "/Jobs" : app_path('Jobs'),
$isGenerationDir ? 'App\\' . str_replace('/', '\\', $path) . '\\Jobs' : 'App\\Models'
->setPath(
new JobPath(
'filename.php',
'filedir',
'namespace'
)
)
```
5. Create a new builder in `Services/Builders`
4. Create a new contract in the `Services/Builders/Advanced/Contracts`:
```php
final class JobBuilder extends AbstractBuilder
interface JobBuilderContract extends BuilderContract
{
}
```
Create a new builder in the `Services/Builders/Advanced` and implement your contract in the builder:
```php
final class JobBuilder extends AbstractBuilder implements JobBuilderContract
{
/**
* @throws NotFoundCodePathException
Expand All @@ -66,19 +64,27 @@ final class JobBuilder extends AbstractBuilder
}
}
```
and add its call to the factory `Services/Builders/BuildFactory.php`
Bind your contract in the LaravelCodeBuilderProvider:
```php
public function register(): void
{
//...
$this->app->bind(JobBuilderContract::class, JobBuilder::class);
}
```

Add code to the factory `Services/Builders/BuildFactory.php`
```php
public function call(string $buildType, string $stub): void
{
match($buildType) {
//...
BuildType::JOB->value => JobBuilder::make(
$this->codeStructure,
$this->codePath,
$stub,
)->build()
BuildType::JOB->value => app(
JobBuilderContract::class,
$classParameters
),
```
6. Write your new builder in the configuration file
5. Write your new builder in the configuration file
```php
<?php

Expand All @@ -92,7 +98,7 @@ return [
BuildType::JOB
],
```
7. Create `Job.stub` in `code_stubs`
8. Write the logic in your `build()` method of the `Services/Builders/JobBuilder.php` file
9. Write tests
10. During PR the code will be analyzed on phpstan with level 4
6. Create `Job.stub` in `code_stubs`
7. Write the logic in your `build()` method of the `Services/Builders/Advanced/JobBuilder.php` file
8. Write tests
9. During PR the code will be analyzed on phpstan with level 4
3 changes: 2 additions & 1 deletion src/Commands/LaravelCodeBuildCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

class LaravelCodeBuildCommand extends Command
{
protected $signature = 'code:build {entity} {table?} {--model} {--request} {--addAction} {--editAction} {--request} {--controller} {--route} {--form} {--DTO} {--builders} {--has-many=*} {--has-one=*} {--belongs-to-many=*}';
protected $signature = 'code:build {entity} {table?} {--model} {--request} {--addAction} {--editAction} {--request} {--controller} {--route} {--form} {--DTO} {--table} {--builders} {--has-many=*} {--has-one=*} {--belongs-to-many=*}';

private CodePath $codePath;

Expand Down Expand Up @@ -161,6 +161,7 @@ protected function prepareBuilders(): void
BuildType::ROUTE,
BuildType::FORM,
BuildType::DTO,
BuildType::TABLE,
];

foreach ($builders as $builder) {
Expand Down
3 changes: 3 additions & 0 deletions src/Enums/BuildType.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ enum BuildType: string

case DTO = 'DTO';

case TABLE = 'table';

public function stub(): string
{
return match ($this) {
Expand All @@ -33,6 +35,7 @@ public function stub(): string
self::ROUTE => 'Route',
self::FORM => 'Form',
self::DTO => 'DTO',
self::TABLE => 'Table',
};
}
}
3 changes: 3 additions & 0 deletions src/Providers/LaravelCodeBuilderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
use DevLnk\LaravelCodeBuilder\Services\Builders\Core\Contracts\ModelBuilderContract;
use DevLnk\LaravelCodeBuilder\Services\Builders\Core\Contracts\RequestBuilderContract;
use DevLnk\LaravelCodeBuilder\Services\Builders\Core\Contracts\RouteBuilderContract;
use DevLnk\LaravelCodeBuilder\Services\Builders\Core\Contracts\TableBuilderContract;
use DevLnk\LaravelCodeBuilder\Services\Builders\Core\ControllerBuilder;
use DevLnk\LaravelCodeBuilder\Services\Builders\Core\DTOBuilder;
use DevLnk\LaravelCodeBuilder\Services\Builders\Core\EditActionBuilder;
use DevLnk\LaravelCodeBuilder\Services\Builders\Core\FormBuilder;
use DevLnk\LaravelCodeBuilder\Services\Builders\Core\ModelBuilder;
use DevLnk\LaravelCodeBuilder\Services\Builders\Core\RequestBuilder;
use DevLnk\LaravelCodeBuilder\Services\Builders\Core\RouteBuilder;
use DevLnk\LaravelCodeBuilder\Services\Builders\Core\TableBuilder;
use Illuminate\Support\ServiceProvider;

class LaravelCodeBuilderProvider extends ServiceProvider
Expand All @@ -40,6 +42,7 @@ public function register(): void
$this->app->bind(ModelBuilderContract::class, ModelBuilder::class);
$this->app->bind(RequestBuilderContract::class, RequestBuilder::class);
$this->app->bind(RouteBuilderContract::class, RouteBuilder::class);
$this->app->bind(TableBuilderContract::class, TableBuilder::class);
}

public function boot(): void
Expand Down
5 changes: 5 additions & 0 deletions src/Services/Builders/BuildFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use DevLnk\LaravelCodeBuilder\Services\Builders\Core\Contracts\ModelBuilderContract;
use DevLnk\LaravelCodeBuilder\Services\Builders\Core\Contracts\RequestBuilderContract;
use DevLnk\LaravelCodeBuilder\Services\Builders\Core\Contracts\RouteBuilderContract;
use DevLnk\LaravelCodeBuilder\Services\Builders\Core\Contracts\TableBuilderContract;
use DevLnk\LaravelCodeBuilder\Services\CodePath\CodePath;
use DevLnk\LaravelCodeBuilder\Services\CodeStructure\CodeStructure;

Expand Down Expand Up @@ -72,6 +73,10 @@ public function call(string $buildType, string $stub): void
DTOBuilderContract::class,
$classParameters
),
BuildType::TABLE->value => app(
TableBuilderContract::class,
$classParameters
),
default => throw new NotFoundBuilderException()
};

Expand Down
1 change: 1 addition & 0 deletions src/Services/Builders/Core/AddActionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function build(): void
'{model_namespace}' => $modelPath->namespace() . '\\' . $modelPath->rawName(),
'{name}' => $actionPath->rawName(),
'{model_name}' => $modelPath->rawName(),
'{entity_singular}' => $this->codeStructure->entity()->singular(),
]);
}
}
12 changes: 12 additions & 0 deletions src/Services/Builders/Core/Contracts/TableBuilderContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace DevLnk\LaravelCodeBuilder\Services\Builders\Core\Contracts;

use DevLnk\LaravelCodeBuilder\Services\Builders\BuilderContract;

interface TableBuilderContract extends BuilderContract
{

}
5 changes: 5 additions & 0 deletions src/Services/Builders/Core/ControllerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function build(): void
$addActionPath = $this->codePath->path(BuildType::ADD_ACTION->value);
$editActionPath = $this->codePath->path(BuildType::EDIT_ACTION->value);
$requestPath = $this->codePath->path(BuildType::REQUEST->value);
$modelPath = $this->codePath->path(BuildType::MODEL->value);

StubBuilder::make($this->stubFile)
->setKey(
Expand All @@ -35,8 +36,12 @@ public function build(): void
'{namespace}' => $controllerPath->namespace(),
'{add_action_namespace}' => $addActionPath->namespace() . '\\' . $addActionPath->rawName(),
'{edit_action_namespace}' => $editActionPath->namespace() . '\\' . $editActionPath->rawName(),
'{model_namespace}' => $modelPath->namespace() . '\\' . $modelPath->rawName(),
'{model}' => $modelPath->rawName(),
'{request_namespace}' => $requestPath->namespace() . '\\' . $requestPath->rawName(),
'{name}' => $controllerPath->rawName(),
'{entity_plural}' => $this->codeStructure->entity()->plural(),
'{entity_singular}' => $this->codeStructure->entity()->singular(),
'{request_name}' => $requestPath->rawName(),
'{add_action_name}' => $addActionPath->rawName(),
'{edit_action_name}' => $editActionPath->rawName(),
Expand Down
1 change: 1 addition & 0 deletions src/Services/Builders/Core/EditActionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function build(): void
'{namespace}' => $actionPath->namespace(),
'{model_namespace}' => $modelPath->namespace() . '\\' . $modelPath->rawName(),
'{name}' => $actionPath->rawName(),
'{entity_singular}' => $this->codeStructure->entity()->singular(),
'{model_name}' => $modelPath->rawName(),
]);
}
Expand Down
Loading