Skip to content

Commit 6f1c0d7

Browse files
authored
Merge pull request #189 from Laravel-Backpack/v6
v6
2 parents 679a8bc + 91c76e0 commit 6f1c0d7

10 files changed

+284
-28
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.

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525
}
2626
],
2727
"require": {
28-
"php": "^7.4|^8.0",
29-
"backpack/crud": "^5.3.11"
28+
"backpack/crud": "^6.0"
3029
},
3130
"require-dev": {
3231
"phpunit/phpunit" : "^9.0||^7.0",

src/Console/Commands/CrudBackpackCommand.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ public function handle()
6767
'code' => "Route::crud('$nameKebab', '{$this->convertSlashesForNamespace($nameTitle)}CrudController');",
6868
]);
6969

70-
// Create the sidebar item
71-
$this->call('backpack:add-sidebar-content', [
72-
'code' => "<li class=\"nav-item\"><a class=\"nav-link\" href=\"{{ backpack_url('$nameKebab') }}\"><i class=\"nav-icon la la-question\"></i> $fullNameWithSpaces</a></li>",
70+
// Create the menu item
71+
$this->call('backpack:add-menu-content', [
72+
'code' => '<x-backpack::menu-item title="'.$fullNameWithSpaces."\" icon=\"la la-question\" :link=\"backpack_url('".$nameKebab."')\" />",
7373
]);
7474

7575
// if the application uses cached routes, we should rebuild the cache so the previous added route will
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

src/Console/Commands/PageBackpackCommand.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@ public function handle()
100100
'code' => "Route::get('{$route}', '{$nameTitle->studly()}Controller@index')->name('page.{$nameSnake}.index');",
101101
]);
102102

103-
// create the sidebar item
104-
$this->call('backpack:add-sidebar-content', [
105-
'code' => "<li class=\"nav-item\"><a class=\"nav-link\" href=\"{{ backpack_url('{$route}') }}\"><i class=\"nav-icon la la-question\"></i> {$nameTitle}</a></li>",
103+
// create the menu item
104+
$this->call('backpack:add-menu-content', [
105+
'code' => '<x-backpack::menu-item title="'.$nameTitle."\" icon=\"la la-question\" :link=\"backpack_url('".$route."')\" />",
106106
]);
107107

108108
$url = backpack_url($route);

src/Console/Commands/Views/PublishOrCreateViewBackpackCommand.php

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,30 @@ abstract class PublishOrCreateViewBackpackCommand extends GeneratorCommand
1010
{
1111
use \Backpack\CRUD\app\Console\Commands\Traits\PrettyCommandOutput;
1212

13+
/**
14+
* The source file to copy from.
15+
*/
16+
public ?string $sourceFile = null;
17+
18+
/**
19+
* The source file view namespace.
20+
*/
21+
public ?string $sourceViewNamespace = null;
22+
23+
/**
24+
* Stub file name.
25+
*
26+
* @var string
27+
*/
28+
protected $stub = '';
29+
30+
/**
31+
* View Namespace.
32+
*
33+
* @var string
34+
*/
35+
protected $viewNamespace = '';
36+
1337
/**
1438
* Get the stub file for the generator.
1539
*
@@ -27,6 +51,12 @@ protected function getStub()
2751
*/
2852
public function handle()
2953
{
54+
$this->setupSourceFile();
55+
56+
if ($this->sourceFile === false) {
57+
return false;
58+
}
59+
3060
$name = Str::of($this->getNameInput());
3161
$path = Str::of($this->getPath($name));
3262
$pathRelative = $path->after(base_path())->replace('\\', '/')->trim('/');
@@ -40,45 +70,49 @@ public function handle()
4070
return false;
4171
}
4272

43-
$source = null;
73+
$this->makeDirectory($path);
74+
75+
if ($this->sourceFile) {
76+
$this->files->copy($this->sourceFile, $path);
77+
} else {
78+
$this->files->put($path, $this->buildClass($name));
79+
}
80+
81+
$this->closeProgressBlock();
82+
}
83+
84+
private function setupSourceFile()
85+
{
4486
if ($this->option('from')) {
4587
$from = $this->option('from');
4688
$namespaces = ViewNamespaces::getFor($this->viewNamespace);
4789
foreach ($namespaces as $namespace) {
4890
$viewPath = "$namespace.$from";
91+
4992
if (view()->exists($viewPath)) {
50-
$source = view($viewPath)->getPath();
93+
$this->sourceFile = view($viewPath)->getPath();
94+
$this->sourceViewNamespace = $viewPath;
5195
break;
5296
}
5397
}
5498

5599
// full or relative file path may be provided
56100
if (file_exists($from)) {
57-
$source = realpath($from);
101+
$this->sourceFile = realpath($from);
58102
}
59103
// remove the first slash to make absolute paths relative in unix systems
60104
elseif (file_exists(substr($from, 1))) {
61-
$source = realpath(substr($from, 1));
105+
$this->sourceFile = realpath(substr($from, 1));
62106
}
63107

64-
if (! $source) {
108+
if (! $this->sourceFile) {
65109
$this->errorProgressBlock();
66110
$this->note("$this->type '$from' does not exist!", 'red');
67111
$this->newLine();
68112

69-
return false;
113+
$this->sourceFile = false;
70114
}
71115
}
72-
73-
$this->makeDirectory($path);
74-
75-
if ($source) {
76-
$this->files->copy($source, $path);
77-
} else {
78-
$this->files->put($path, $this->buildClass($name));
79-
}
80-
81-
$this->closeProgressBlock();
82116
}
83117

84118
/**

src/Console/Commands/Views/WidgetBackpackCommand.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Backpack\Generators\Console\Commands\Views;
44

5+
use Illuminate\Support\Str;
6+
57
class WidgetBackpackCommand extends PublishOrCreateViewBackpackCommand
68
{
79
/**
@@ -54,6 +56,20 @@ class WidgetBackpackCommand extends PublishOrCreateViewBackpackCommand
5456
*/
5557
protected function getPath($name)
5658
{
57-
return resource_path("views/vendor/backpack/base/{$this->viewNamespace}/$name.blade.php");
59+
if ($this->sourceViewNamespace) {
60+
$themePath = Str::contains($this->sourceViewNamespace, '::') ?
61+
Str::before($this->sourceViewNamespace, '::') :
62+
Str::beforeLast($this->sourceViewNamespace, '.');
63+
64+
$themePath = Str::replace('.', '/', $themePath);
65+
66+
$path = 'views/vendor/'.$themePath.'/'.$this->viewNamespace.'/'.$name.'.blade.php';
67+
68+
return resource_path($path);
69+
}
70+
71+
$path = 'views/vendor/backpack/ui/'.$this->viewNamespace.'/'.$name.'.blade.php';
72+
73+
return resource_path($path);
5874
}
5975
}

0 commit comments

Comments
 (0)