Skip to content

Commit 48aeafa

Browse files
authored
Merge pull request #141 from karandatwani92/button-cmd
Create a php artisan backpack:button command
2 parents 77594cd + bd24540 commit 48aeafa

File tree

4 files changed

+145
-0
lines changed

4 files changed

+145
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ php artisan backpack:view {Entity_name}
8282
php artisan backpack:config {Entity_name}
8383
```
8484

85+
- Generate a button
86+
87+
``` bash
88+
php artisan backpack:button {Entity_name}
89+
```
90+
8591
## Change log
8692

8793
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?php
2+
3+
namespace Backpack\Generators\Console\Commands;
4+
5+
use Illuminate\Console\GeneratorCommand;
6+
use Illuminate\Support\Str;
7+
8+
class ButtonBackpackCommand extends GeneratorCommand
9+
{
10+
use \Backpack\CRUD\app\Console\Commands\Traits\PrettyCommandOutput;
11+
/**
12+
* The console command name.
13+
*
14+
* @var string
15+
*/
16+
protected $name = 'backpack:button';
17+
18+
/**
19+
* The name and signature of the console command.
20+
*
21+
* @var string
22+
*/
23+
protected $signature = 'backpack:button {name}';
24+
25+
/**
26+
* The console command description.
27+
*
28+
* @var string
29+
*/
30+
protected $description = 'Generate a Backpack button';
31+
32+
/**
33+
* The type of class being generated.
34+
*
35+
* @var string
36+
*/
37+
protected $type = 'Button';
38+
39+
/**
40+
* Get the stub file for the generator.
41+
*
42+
* @return string
43+
*/
44+
protected function getStub()
45+
{
46+
return __DIR__.'/../stubs/button.stub';
47+
}
48+
49+
/**
50+
* Alias for the fire method.
51+
*
52+
* In Laravel 5.5 the fire() method has been renamed to handle().
53+
* This alias provides support for both Laravel 5.4 and 5.5.
54+
*/
55+
public function handle()
56+
{
57+
$this->fire();
58+
}
59+
60+
/**
61+
* Execute the console command.
62+
*
63+
* @return bool|null
64+
*/
65+
public function fire()
66+
{
67+
$name = Str::of($this->getNameInput());
68+
$path = $this->getPath($name);
69+
70+
if ($this->alreadyExists($this->getNameInput())) {
71+
$this->error($this->type.' already existed!');
72+
73+
return false;
74+
}
75+
76+
$this->infoBlock("Creating {$name->replace('_', ' ')->title()} {$this->type}");
77+
$this->progressBlock("Creating view <fg=blue>resources/views/vendor/backpack/crud/buttons/${name}.blade.php</>");
78+
79+
$this->makeDirectory($path);
80+
$this->files->put($path, $this->buildClass($name));
81+
82+
$this->closeProgressBlock();
83+
$this->newLine();
84+
$this->info($this->type.' created successfully.');
85+
}
86+
87+
/**
88+
* Determine if the class already exists.
89+
*
90+
* @param string $name
91+
* @return bool
92+
*/
93+
protected function alreadyExists($name)
94+
{
95+
return $this->files->exists($this->getPath($name));
96+
}
97+
98+
/**
99+
* Get the destination class path.
100+
*
101+
* @param string $name
102+
* @return string
103+
*/
104+
protected function getPath($name)
105+
{
106+
return resource_path("views/vendor/backpack/crud/buttons/$name.blade.php");
107+
}
108+
109+
/**
110+
* Build the class with the given name.
111+
*
112+
* @param string $name
113+
* @return string
114+
*/
115+
protected function buildClass($name)
116+
{
117+
$stub = $this->files->get($this->getStub());
118+
$stub = str_replace('dummy', $name, $stub);
119+
120+
return $stub;
121+
}
122+
123+
/**
124+
* Get the console command options.
125+
*
126+
* @return array
127+
*/
128+
protected function getOptions()
129+
{
130+
return [
131+
132+
];
133+
}
134+
}

src/Console/stubs/button.stub

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@if ($crud->hasAccess('dummy'))
2+
<a href="{{ url($crud->route.'/'.$entry->getKey().'/dummy') }}" class="btn btn-sm btn-link text-capitalize"><i class="la la-question"></i> dummy</a>
3+
@endif

src/GeneratorsServiceProvider.php

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

55
use Backpack\Generators\Console\Commands\BuildBackpackCommand;
6+
use Backpack\Generators\Console\Commands\ButtonBackpackCommand;
67
use Backpack\Generators\Console\Commands\ChartBackpackCommand;
78
use Backpack\Generators\Console\Commands\ChartControllerBackpackCommand;
89
use Backpack\Generators\Console\Commands\ConfigBackpackCommand;
@@ -22,6 +23,7 @@ class GeneratorsServiceProvider extends ServiceProvider
2223
{
2324
protected $commands = [
2425
BuildBackpackCommand::class,
26+
ButtonBackpackCommand::class,
2527
ConfigBackpackCommand::class,
2628
CrudModelBackpackCommand::class,
2729
CrudControllerBackpackCommand::class,

0 commit comments

Comments
 (0)