Skip to content

Commit 66d5db5

Browse files
committed
Create Page Command
Also includes create page controller
1 parent 4599134 commit 66d5db5

File tree

5 files changed

+362
-0
lines changed

5 files changed

+362
-0
lines changed
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<?php
2+
3+
namespace Backpack\Generators\Console\Commands;
4+
5+
use Illuminate\Console\GeneratorCommand;
6+
use Illuminate\Support\Str;
7+
8+
class PageBackpackCommand extends GeneratorCommand
9+
{
10+
use \Backpack\CRUD\app\Console\Commands\Traits\PrettyCommandOutput;
11+
12+
/**
13+
* The console command name.
14+
*
15+
* @var string
16+
*/
17+
protected $name = 'backpack:page';
18+
19+
/**
20+
* The name and signature of the console command.
21+
*
22+
* @var string
23+
*/
24+
protected $signature = 'backpack:page {name}
25+
{--view-path=admin : Path for the view, after resources/views/}
26+
{--layout= : Base layout for the page}';
27+
28+
/**
29+
* The console command description.
30+
*
31+
* @var string
32+
*/
33+
protected $description = 'Generate a Backpack Page';
34+
35+
/**
36+
* The type of class being generated.
37+
*
38+
* @var string
39+
*/
40+
protected $type = 'Resource';
41+
42+
/**
43+
* Execute the console command.
44+
*
45+
* @return bool|null
46+
*
47+
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
48+
*/
49+
public function handle()
50+
{
51+
$filePath = Str::of($this->getNameInput())
52+
->replace('\\', '/')
53+
->replace('.', '/')
54+
->trim('/')
55+
->start('/')
56+
->prepend($this->option('view-path'));
57+
58+
$name = $filePath->afterLast('/');
59+
$path = $filePath->beforeLast('/');
60+
$fullpath = $this->getPath($filePath);
61+
$layout = $this->option('layout');
62+
63+
$this->infoBlock("Creating {$name->title()} page");
64+
65+
$this->progressBlock("Creating view <fg=blue>resources/views/${filePath}.php</>");
66+
67+
// check if the file already exists
68+
if ((! $this->hasOption('force') || ! $this->option('force')) && $this->alreadyExists($fullpath)) {
69+
$this->closeProgressBlock('Already existed', 'yellow');
70+
71+
return false;
72+
}
73+
74+
$this->makeDirectory($fullpath);
75+
76+
// create page view
77+
$stub = $this->buildClass($filePath);
78+
$stub = str_replace('layout', $layout, $stub);
79+
$stub = str_replace('DummyName', $name->studly(), $stub);
80+
$this->files->put($fullpath, $stub);
81+
82+
$this->closeProgressBlock();
83+
84+
// create controller
85+
$this->call('backpack:page-controller', [
86+
'name' => $name,
87+
'--view-path' => $path,
88+
]);
89+
90+
// create route
91+
$this->call('backpack:add-custom-route', [
92+
'code' => "Route::get('{$name->kebab()}', '{$name->studly()}Controller@index')->name('page.{$name->kebab()}.index');",
93+
]);
94+
95+
// create the sidebar item
96+
$this->call('backpack:add-sidebar-content', [
97+
'code' => "<li class=\"nav-item\"><a class=\"nav-link\" href=\"{{ backpack_url('{$name->kebab()}') }}\"><i class=\"nav-icon la la-question\"></i> {$name->title()}</a></li>",
98+
]);
99+
100+
$url = Str::of(config('app.url'))->finish('/')->append("admin/{$name->kebab()}");
101+
102+
$this->newLine();
103+
$this->note("Page {$name->title()} created.");
104+
$this->note("Go to <fg=blue>$url</> to access your new page.");
105+
$this->newLine();
106+
}
107+
108+
/**
109+
* Get the stub file for the generator.
110+
*
111+
* @return string
112+
*/
113+
protected function getStub()
114+
{
115+
return __DIR__.'/../stubs/page.stub';
116+
}
117+
118+
/**
119+
* Determine if the class already exists.
120+
*
121+
* @param string $name
122+
*
123+
* @return bool
124+
*/
125+
protected function alreadyExists($name)
126+
{
127+
return $this->files->exists($this->getPath($name));
128+
}
129+
130+
/**
131+
* Get the destination class path.
132+
*
133+
* @param string $name
134+
*
135+
* @return string
136+
*/
137+
protected function getPath($name)
138+
{
139+
return resource_path("views/$name.blade.php");
140+
}
141+
}
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
<?php
2+
3+
namespace Backpack\Generators\Console\Commands;
4+
5+
use Illuminate\Console\GeneratorCommand;
6+
use Illuminate\Support\Str;
7+
8+
class PageControllerBackpackCommand extends GeneratorCommand
9+
{
10+
use \Backpack\CRUD\app\Console\Commands\Traits\PrettyCommandOutput;
11+
12+
/**
13+
* The console command name.
14+
*
15+
* @var string
16+
*/
17+
protected $name = 'backpack:page-controller';
18+
19+
/**
20+
* The name and signature of the console command.
21+
*
22+
* @var string
23+
*/
24+
protected $signature = 'backpack:page-controller {name} {--view-path=}';
25+
26+
/**
27+
* The console command description.
28+
*
29+
* @var string
30+
*/
31+
protected $description = 'Generate a Backpack PageController';
32+
33+
/**
34+
* The type of class being generated.
35+
*
36+
* @var string
37+
*/
38+
protected $type = 'Controller';
39+
40+
/**
41+
* Execute the console command.
42+
*
43+
* @return bool|null
44+
*
45+
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
46+
*/
47+
public function handle()
48+
{
49+
$name = $this->getNameInput();
50+
$this->progressBlock("Creating controller <fg=blue>${name}Controller</>");
51+
52+
if ($this->isReservedName($this->getNameInput())) {
53+
$this->errorProgressBlock();
54+
$this->note("The name '$name' is reserved by PHP.", 'red');
55+
56+
return false;
57+
}
58+
59+
$name = $this->qualifyClass($this->getNameInput());
60+
61+
$path = $this->getPath($name);
62+
63+
// Next, We will check to see if the class already exists. If it does, we don't want
64+
// to create the class and overwrite the user's code. So, we will bail out so the
65+
// code is untouched. Otherwise, we will continue generating this class' files.
66+
if ((! $this->hasOption('force') ||
67+
! $this->option('force')) &&
68+
$this->alreadyExists($this->getNameInput())) {
69+
$this->closeProgressBlock('Already existed', 'yellow');
70+
71+
return false;
72+
}
73+
74+
// Next, we will generate the path to the location where this class' file should get
75+
// written. Then, we will build the class and make the proper replacements on the
76+
// stub files so that it gets the correctly formatted namespace and class name.
77+
$this->makeDirectory($path);
78+
79+
$this->files->put($path, $this->sortImports($this->buildClass($name)));
80+
81+
$this->closeProgressBlock();
82+
}
83+
84+
/**
85+
* Get the desired class name from the input.
86+
*
87+
* @return string
88+
*/
89+
protected function getNameInput()
90+
{
91+
return Str::of($this->argument('name'))->trim()->studly();
92+
}
93+
94+
/**
95+
* Get the destination class path.
96+
*
97+
* @param string $name
98+
* @return string
99+
*/
100+
protected function getPath($name)
101+
{
102+
return str_replace('.php', 'Controller.php', parent::getPath($name));
103+
}
104+
105+
/**
106+
* Get the stub file for the generator.
107+
*
108+
* @return string
109+
*/
110+
protected function getStub()
111+
{
112+
return __DIR__.'/../stubs/page-controller.stub';
113+
}
114+
115+
/**
116+
* Get the default namespace for the class.
117+
*
118+
* @param string $rootNamespace
119+
* @return string
120+
*/
121+
protected function getDefaultNamespace($rootNamespace)
122+
{
123+
return $rootNamespace.'\Http\Controllers\Admin';
124+
}
125+
126+
/**
127+
* Replace the path name for the given stub.
128+
*
129+
* @param string $stub
130+
* @param string $name
131+
* @return string
132+
*/
133+
protected function replacePathStrings(&$stub)
134+
{
135+
$pathDot = Str::of($this->option('view-path'))
136+
->replace('/', '.')
137+
->replace('\\', '.')
138+
->append('.'.$this->getNameInput()->lcfirst())
139+
->trim('.');
140+
$pathSlash = $pathDot->replace('.', '/');
141+
142+
$stub = str_replace('dummy.path', $pathDot, $stub);
143+
$stub = str_replace('dummy/path', $pathSlash, $stub);
144+
145+
return $this;
146+
}
147+
148+
/**
149+
* Replace the name for the given stub.
150+
*
151+
* @param string $stub
152+
* @param string $name
153+
* @return string
154+
*/
155+
protected function replaceNameStrings(&$stub)
156+
{
157+
$name = $this->getNameInput();
158+
159+
$stub = str_replace('DummyName', $name, $stub);
160+
$stub = str_replace('dummyName', $name->lcfirst(), $stub);
161+
162+
return $this;
163+
}
164+
165+
/**
166+
* Build the class with the given name.
167+
*
168+
* @param string $name
169+
* @return string
170+
*/
171+
protected function buildClass($name)
172+
{
173+
$stub = $this->files->get($this->getStub());
174+
175+
return $this
176+
->replaceNamespace($stub, $name)
177+
->replacePathStrings($stub)
178+
->replaceNameStrings($stub)
179+
->replaceClass($stub, $name);
180+
}
181+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace DummyNamespace;
4+
5+
use Backpack\CRUD\app\Http\Controllers\BaseController;
6+
7+
/**
8+
* Class DummyClassController
9+
* @package App\Http\Controllers\Admin
10+
* @property-read \Backpack\CRUD\app\Library\CrudPanel\CrudPanel $crud
11+
*/
12+
class DummyClassController extends BaseController
13+
{
14+
public function index()
15+
{
16+
return view('dummy.path', [
17+
'title' => 'DummyName',
18+
'breadcrumbs' => [
19+
trans('backpack::crud.admin') => backpack_url('dashboard'),
20+
'DummyName' => false,
21+
],
22+
'page' => 'resources/views/dummy/path.blade.php',
23+
'controller' => 'app/Http/Controllers/Admin/DummyNameController.php',
24+
]);
25+
}
26+
}

src/Console/stubs/page.stub

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
@extends(backpack_view('blank'))
2+
3+
@section('content')
4+
<div class="jumbotron">
5+
<h1 class="mb-4">DummyName</h1>
6+
7+
<p>Go to <code>{{ $page }}</code> to edit this page.</p>
8+
<p>To add logic to it, go to <code>{{ $controller }}</code> to edit the controller.</p>
9+
</div>
10+
@endsection

src/GeneratorsServiceProvider.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
use Backpack\Generators\Console\Commands\CrudOperationBackpackCommand;
1313
use Backpack\Generators\Console\Commands\CrudRequestBackpackCommand;
1414
use Backpack\Generators\Console\Commands\ModelBackpackCommand;
15+
use Backpack\Generators\Console\Commands\PageBackpackCommand;
16+
use Backpack\Generators\Console\Commands\PageControllerBackpackCommand;
1517
use Backpack\Generators\Console\Commands\RequestBackpackCommand;
1618
use Backpack\Generators\Console\Commands\ViewBackpackCommand;
1719
use Illuminate\Support\ServiceProvider;
@@ -29,6 +31,8 @@ class GeneratorsServiceProvider extends ServiceProvider
2931
CrudBackpackCommand::class,
3032
ChartBackpackCommand::class,
3133
ModelBackpackCommand::class,
34+
PageBackpackCommand::class,
35+
PageControllerBackpackCommand::class,
3236
RequestBackpackCommand::class,
3337
ViewBackpackCommand::class,
3438
];

0 commit comments

Comments
 (0)