Skip to content

Commit ed513f2

Browse files
authored
Merge pull request #191 from Laravel-Backpack/add-form-operation-command
added command to generate a custom form operation
2 parents 0dd9438 + 48cb2d1 commit ed513f2

File tree

5 files changed

+210
-3
lines changed

5 files changed

+210
-3
lines changed

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Quickly generate Backpack templated Models, Requests, Views and Config files for
1818
Via Composer
1919

2020
``` bash
21-
composer require --dev backpack/generators
21+
composer require --dev backpack/generators
2222
```
2323

2424
## Usage
@@ -74,7 +74,7 @@ php artisan backpack:request {Entity_name}
7474

7575
``` bash
7676
php artisan backpack:view {Entity_name}
77-
```
77+
```
7878

7979
- Generate a config file
8080

@@ -124,6 +124,18 @@ php artisan backpack:widget {widget_name}
124124
php artisan backpack:widget {widget_name} --from={original_widget_name}
125125
```
126126

127+
- Generate a custom operation
128+
129+
``` bash
130+
php artisan backpack:crud-operation {OperationName}
131+
```
132+
133+
- Generate a custom form operation
134+
135+
``` bash
136+
php artisan backpack:crud-form-operation {OperationName}
137+
```
138+
127139
## Change log
128140

129141
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
3+
namespace Backpack\Generators\Console\Commands;
4+
5+
use Illuminate\Console\GeneratorCommand;
6+
use Illuminate\Support\Str;
7+
8+
class CrudFormOperationBackpackCommand extends GeneratorCommand
9+
{
10+
/**
11+
* The console command name.
12+
*
13+
* @var string
14+
*/
15+
protected $name = 'backpack:crud-form-operation';
16+
17+
/**
18+
* The name and signature of the console command.
19+
*
20+
* @var string
21+
*/
22+
protected $signature = 'backpack:crud-form-operation {name}';
23+
24+
/**
25+
* The console command description.
26+
*
27+
* @var string
28+
*/
29+
protected $description = 'Generate an operation trait with a Backpack form';
30+
31+
/**
32+
* The type of class being generated.
33+
*
34+
* @var string
35+
*/
36+
protected $type = 'Trait';
37+
38+
/**
39+
* Get the destination class path.
40+
*
41+
* @param string $name
42+
* @return string
43+
*/
44+
protected function getPath($name)
45+
{
46+
$name = str_replace($this->laravel->getNamespace(), '', $name);
47+
48+
return $this->laravel['path'].'/'.str_replace('\\', '/', $name).'Operation.php';
49+
}
50+
51+
/**
52+
* Get the stub file for the generator.
53+
*
54+
* @return string
55+
*/
56+
protected function getStub()
57+
{
58+
return __DIR__.'/../stubs/crud-form-operation.stub';
59+
}
60+
61+
/**
62+
* Get the default namespace for the class.
63+
*
64+
* @param string $rootNamespace
65+
* @return string
66+
*/
67+
protected function getDefaultNamespace($rootNamespace)
68+
{
69+
return $rootNamespace.'\Http\Controllers\Admin\Operations';
70+
}
71+
72+
/**
73+
* Replace the table name for the given stub.
74+
*
75+
* @param string $stub
76+
* @param string $name
77+
* @return string
78+
*/
79+
protected function replaceNameStrings(&$stub, $name)
80+
{
81+
$name = Str::of($name)->afterLast('\\');
82+
83+
$stub = str_replace('DummyClass', $name->studly(), $stub);
84+
$stub = str_replace('dummyClass', $name->lcfirst(), $stub);
85+
$stub = str_replace('Dummy Class', $name->snake()->replace('_', ' ')->title(), $stub);
86+
$stub = str_replace('dummy-class', $name->snake('-'), $stub);
87+
$stub = str_replace('dummy_class', $name->snake(), $stub);
88+
89+
return $this;
90+
}
91+
92+
/**
93+
* Build the class with the given name.
94+
*
95+
* @param string $name
96+
* @return string
97+
*/
98+
protected function buildClass($name)
99+
{
100+
$stub = $this->files->get($this->getStub());
101+
102+
return $this
103+
->replaceNamespace($stub, $name)
104+
->replaceNameStrings($stub, $name)
105+
->replaceClass($stub, $name);
106+
}
107+
108+
/**
109+
* Get the desired class name from the input.
110+
*
111+
* @return string
112+
*/
113+
protected function getNameInput()
114+
{
115+
return Str::of($this->argument('name'))
116+
->trim()
117+
->studly();
118+
}
119+
}

src/Console/Commands/CrudModelBackpackCommand.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Backpack\Generators\Console\Commands;
44

55
use Backpack\Generators\Services\BackpackCommand;
6-
use Illuminate\Support\Facades\File;
76
use Illuminate\Support\Str;
87

98
class CrudModelBackpackCommand extends BackpackCommand
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace DummyNamespace;
4+
5+
use Backpack\CRUD\app\Http\Controllers\Operations\Concerns\HasForm;
6+
7+
trait DummyClassOperation
8+
{
9+
use HasForm;
10+
11+
/**
12+
* Define which routes are needed for this operation.
13+
*
14+
* @param string $segment Name of the current entity (singular). Used as first URL segment.
15+
* @param string $routeName Prefix of the route name.
16+
* @param string $controller Name of the current CrudController.
17+
*/
18+
protected function setupDummyClassRoutes(string $segment, string $routeName, string $controller): void
19+
{
20+
$this->formRoutes(
21+
operationName: 'dummyClass',
22+
routesHaveIdSegment: true,
23+
segment: $segment,
24+
routeName: $routeName,
25+
controller: $controller
26+
);
27+
}
28+
29+
/**
30+
* Add the default settings, buttons, etc that this operation needs.
31+
*/
32+
protected function setupDummyClassDefaults(): void
33+
{
34+
$this->formDefaults(
35+
operationName: 'dummyClass',
36+
// buttonStack: 'line', // alternatives: top, bottom
37+
// buttonMeta: [
38+
// 'icon' => 'la la-home',
39+
// 'label' => 'Dummy Class',
40+
// 'wrapper' => [
41+
// 'target' => '_blank',
42+
// ],
43+
// ],
44+
);
45+
}
46+
47+
/**
48+
* Method to handle the GET request and display the View with a Backpack form
49+
*
50+
*/
51+
public function getDummyClassForm(?int $id = null) : \Illuminate\Contracts\View\View
52+
{
53+
$this->crud->hasAccessOrFail('dummyClass');
54+
55+
return $this->formView($id);
56+
}
57+
58+
/**
59+
* Method to handle the POST request and perform the operation
60+
*
61+
* @return array|\Illuminate\Http\RedirectResponse
62+
*/
63+
public function postDummyClassForm(?int $id = null)
64+
{
65+
$this->crud->hasAccessOrFail('dummyClass');
66+
67+
return $this->formAction($id, function ($inputs, $entry) {
68+
// You logic goes here...
69+
// dd('got to ' . __METHOD__, $inputs, $entry);
70+
71+
// show a success message
72+
\Alert::success('Something was done!')->flash();
73+
});
74+
}
75+
}

src/GeneratorsServiceProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Backpack\Generators\Console\Commands\ConfigBackpackCommand;
99
use Backpack\Generators\Console\Commands\CrudBackpackCommand;
1010
use Backpack\Generators\Console\Commands\CrudControllerBackpackCommand;
11+
use Backpack\Generators\Console\Commands\CrudFormOperationBackpackCommand;
1112
use Backpack\Generators\Console\Commands\CrudModelBackpackCommand;
1213
use Backpack\Generators\Console\Commands\CrudOperationBackpackCommand;
1314
use Backpack\Generators\Console\Commands\CrudRequestBackpackCommand;
@@ -34,6 +35,7 @@ class GeneratorsServiceProvider extends ServiceProvider
3435
CrudControllerBackpackCommand::class,
3536
ChartControllerBackpackCommand::class,
3637
CrudOperationBackpackCommand::class,
38+
CrudFormOperationBackpackCommand::class,
3739
CrudRequestBackpackCommand::class,
3840
CrudBackpackCommand::class,
3941
ChartBackpackCommand::class,

0 commit comments

Comments
 (0)