Skip to content

Commit 739830c

Browse files
authored
Merge pull request #154 from Laravel-Backpack/cmd-new-widget
Artisan command to generate new widgets
2 parents a98ca55 + 7e06acc commit 739830c

File tree

3 files changed

+174
-0
lines changed

3 files changed

+174
-0
lines changed
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
<?php
2+
3+
namespace Backpack\Generators\Console\Commands;
4+
5+
use Backpack\CRUD\ViewNamespaces;
6+
use Illuminate\Console\GeneratorCommand;
7+
use Illuminate\Support\Str;
8+
9+
class WidgetBackpackCommand extends GeneratorCommand
10+
{
11+
use \Backpack\CRUD\app\Console\Commands\Traits\PrettyCommandOutput;
12+
/**
13+
* The console command name.
14+
*
15+
* @var string
16+
*/
17+
protected $name = 'backpack:widget';
18+
19+
/**
20+
* The name and signature of the console command.
21+
*
22+
* @var string
23+
*/
24+
protected $signature = 'backpack:widget {name} {--from=}';
25+
26+
/**
27+
* The console command description.
28+
*
29+
* @var string
30+
*/
31+
protected $description = 'Generate a Backpack widget';
32+
33+
/**
34+
* The type of class being generated.
35+
*
36+
* @var string
37+
*/
38+
protected $type = 'Widget';
39+
40+
/**
41+
* Get the stub file for the generator.
42+
*
43+
* @return string
44+
*/
45+
protected function getStub()
46+
{
47+
return __DIR__.'/../stubs/widget.stub';
48+
}
49+
50+
/**
51+
* Alias for the fire method.
52+
*
53+
* In Laravel 5.5 the fire() method has been renamed to handle().
54+
* This alias provides support for both Laravel 5.4 and 5.5.
55+
*/
56+
public function handle()
57+
{
58+
$this->fire();
59+
}
60+
61+
/**
62+
* Execute the console command.
63+
*
64+
* @return bool|null
65+
*/
66+
public function fire()
67+
{
68+
$name = Str::of($this->getNameInput());
69+
$path = $this->getPath($name);
70+
71+
if ($this->alreadyExists($this->getNameInput())) {
72+
$this->error("Error : $this->type $name already existed!");
73+
74+
return false;
75+
}
76+
77+
$src = null;
78+
if ($this->option('from')) {
79+
$widget = Str::of($this->option('from'));
80+
$arr = ViewNamespaces::getFor('widgets');
81+
foreach ($arr as $key => $value) {
82+
$viewPath = $value.'.'.$widget;
83+
if (view()->exists($viewPath)) {
84+
$src = view($viewPath)->getPath();
85+
break;
86+
}
87+
}
88+
if ($src == null) {
89+
$this->error("Error : $this->type $widget does not exist!");
90+
91+
return false;
92+
}
93+
}
94+
95+
$this->infoBlock("Creating {$name->replace('_', ' ')->title()} {$this->type}");
96+
$this->progressBlock("Creating view <fg=blue>resources/views/vendor/backpack/base/widgets/{$name->snake('_')}.blade.php</>");
97+
98+
$this->makeDirectory($path);
99+
if ($src != null) {
100+
$this->files->copy($src, $path);
101+
} else {
102+
$this->files->put($path, $this->buildClass($name));
103+
}
104+
105+
$this->closeProgressBlock();
106+
$this->newLine();
107+
$this->info($this->type.' created successfully.');
108+
}
109+
110+
/**
111+
* Determine if the class already exists.
112+
*
113+
* @param string $name
114+
* @return bool
115+
*/
116+
protected function alreadyExists($name)
117+
{
118+
return $this->files->exists($this->getPath($name));
119+
}
120+
121+
/**
122+
* Get the destination class path.
123+
*
124+
* @param string $name
125+
* @return string
126+
*/
127+
protected function getPath($name)
128+
{
129+
$file = Str::of($name)->snake('_');
130+
131+
return resource_path("views/vendor/backpack/base/widgets/$file.blade.php");
132+
}
133+
134+
/**
135+
* Build the class with the given name.
136+
*
137+
* @param string $name
138+
* @return string
139+
*/
140+
protected function buildClass($name)
141+
{
142+
$stub = $this->files->get($this->getStub());
143+
$stub = str_replace('dummy_widget', $name->snake('_'), $stub);
144+
$stub = str_replace('dummyWidget', $name->camel(), $stub);
145+
$stub = str_replace('DummyWidget', $name->studly(), $stub);
146+
147+
return $stub;
148+
}
149+
150+
/**
151+
* Get the console command options.
152+
*
153+
* @return array
154+
*/
155+
protected function getOptions()
156+
{
157+
return [];
158+
}
159+
}

src/Console/stubs/widget.stub

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{{-- Include widget wrapper --}}
2+
@includeWhen(!empty($widget['wrapper']), 'backpack::widgets.inc.wrapper_start')
3+
4+
{{-- Define your widget --}}
5+
<div class="alert alert-info">
6+
7+
@if (isset($widget['content']))
8+
<p>{!! $widget['content'] !!}</p>
9+
@endif
10+
11+
</div>
12+
13+
@includeWhen(!empty($widget['wrapper']), 'backpack::widgets.inc.wrapper_end')

src/GeneratorsServiceProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Backpack\Generators\Console\Commands\PageControllerBackpackCommand;
2121
use Backpack\Generators\Console\Commands\RequestBackpackCommand;
2222
use Backpack\Generators\Console\Commands\ViewBackpackCommand;
23+
use Backpack\Generators\Console\Commands\WidgetBackpackCommand;
2324
use Illuminate\Support\ServiceProvider;
2425

2526
class GeneratorsServiceProvider extends ServiceProvider
@@ -43,6 +44,7 @@ class GeneratorsServiceProvider extends ServiceProvider
4344
PageControllerBackpackCommand::class,
4445
RequestBackpackCommand::class,
4546
ViewBackpackCommand::class,
47+
WidgetBackpackCommand::class,
4648
];
4749

4850
/**

0 commit comments

Comments
 (0)