1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Arrilot\Widgets\Console; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\GeneratorCommand; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use RuntimeException; |
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
9
|
|
|
|
10
|
|
|
class WidgetMakeCommand extends GeneratorCommand |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The console command name. |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $name = 'make:widget'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The console command description. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $description = 'Create a new widget (arrilot/laravel-widgets)'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The type of class being generated. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $type = 'Widget'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Execute the console command for Laravel >= 5.5 |
35
|
|
|
* |
36
|
|
|
* @return void |
37
|
|
|
*/ |
38
|
|
|
public function handle() |
39
|
|
|
{ |
40
|
|
|
parent::handle(); |
41
|
|
|
|
42
|
|
|
if (!$this->option('plain')) { |
43
|
|
|
$this->createView(); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Execute the console command for Laravel < 5.5 |
49
|
|
|
* |
50
|
|
|
* @return void |
51
|
|
|
*/ |
52
|
|
|
public function fire() |
53
|
|
|
{ |
54
|
|
|
parent::fire(); |
55
|
|
|
|
56
|
|
|
if (!$this->option('plain')) { |
57
|
|
|
$this->createView(); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Build the class with the given name. |
63
|
|
|
* |
64
|
|
|
* @param string $name |
65
|
|
|
* |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
protected function buildClass($name) |
69
|
|
|
{ |
70
|
|
|
$stub = $this->files->get($this->getStub()); |
71
|
|
|
|
72
|
|
|
$stub = $this->replaceNamespace($stub, $name)->replaceClass($stub, $name); |
73
|
|
|
|
74
|
|
|
if (!$this->option('plain')) { |
75
|
|
|
$stub = $this->replaceView($stub); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $stub; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get the stub file for the generator. |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
protected function getStub() |
87
|
|
|
{ |
88
|
|
|
$stubName = $this->option('plain') ? 'widget_plain' : 'widget'; |
89
|
|
|
$stubPath = $this->laravel->make('config')->get('laravel-widgets.'.$stubName.'_stub'); |
90
|
|
|
|
91
|
|
|
// for BC |
92
|
|
|
if (is_null($stubPath)) { |
93
|
|
|
return __DIR__.'/stubs/'.$stubName.'.stub'; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $this->laravel->basePath().'/'.$stubPath; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Replace the namespace for the given stub. |
101
|
|
|
* |
102
|
|
|
* @param string $stub |
103
|
|
|
* @param string $name |
104
|
|
|
* |
105
|
|
|
* @return $this |
106
|
|
|
*/ |
107
|
|
|
protected function replaceNamespace(&$stub, $name) |
108
|
|
|
{ |
109
|
|
|
$stub = str_replace( |
110
|
|
|
'{{namespace}}', $this->getNamespace($name), $stub |
111
|
|
|
); |
112
|
|
|
|
113
|
|
|
$stub = str_replace( |
114
|
|
|
'{{rootNamespace}}', $this->laravel->getNamespace(), $stub |
|
|
|
|
115
|
|
|
); |
116
|
|
|
|
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Replace the class name for the given stub. |
122
|
|
|
* |
123
|
|
|
* @param string $stub |
124
|
|
|
* @param string $name |
125
|
|
|
* |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
|
|
protected function replaceClass($stub, $name) |
129
|
|
|
{ |
130
|
|
|
$class = str_replace($this->getNamespace($name).'\\', '', $name); |
131
|
|
|
|
132
|
|
|
return str_replace('{{class}}', $class, $stub); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Replace the view name for the given stub. |
137
|
|
|
* |
138
|
|
|
* @param string $stub |
139
|
|
|
* |
140
|
|
|
* @return string |
141
|
|
|
*/ |
142
|
|
|
protected function replaceView($stub) |
143
|
|
|
{ |
144
|
|
|
$view = 'widgets.'.str_replace('/', '.', $this->makeViewName()); |
145
|
|
|
|
146
|
|
|
return str_replace('{{view}}', $view, $stub); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Get the default namespace for the class. |
151
|
|
|
* |
152
|
|
|
* @param string $rootNamespace |
153
|
|
|
* |
154
|
|
|
* @return string |
155
|
|
|
*/ |
156
|
|
|
protected function getDefaultNamespace($rootNamespace) |
157
|
|
|
{ |
158
|
|
|
$namespace = config('laravel-widgets.default_namespace', $rootNamespace.'\Widgets'); |
159
|
|
|
|
160
|
|
|
if (!Str::startsWith($namespace, $rootNamespace)) { |
161
|
|
|
throw new RuntimeException("You can not use the generator if the default namespace ($namespace) does not start with application namespace ($rootNamespace)"); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return $namespace; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Get the console command options. |
169
|
|
|
* |
170
|
|
|
* @return array |
171
|
|
|
*/ |
172
|
|
|
protected function getOptions() |
173
|
|
|
{ |
174
|
|
|
return [ |
175
|
|
|
['plain', null, InputOption::VALUE_NONE, 'Use plain stub. No view is being created too.'], |
176
|
|
|
]; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Create a new view file for the widget. |
181
|
|
|
* |
182
|
|
|
* return void |
183
|
|
|
*/ |
184
|
|
|
protected function createView() |
185
|
|
|
{ |
186
|
|
|
if ($this->files->exists($path = $this->getViewPath())) { |
187
|
|
|
$this->error('View already exists!'); |
188
|
|
|
|
189
|
|
|
return; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
$this->makeDirectory($path); |
193
|
|
|
|
194
|
|
|
$this->files->put($path, ''); |
195
|
|
|
|
196
|
|
|
$this->info('View created successfully.'); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Get the destination view path. |
201
|
|
|
* |
202
|
|
|
* @return string |
203
|
|
|
*/ |
204
|
|
|
protected function getViewPath() |
205
|
|
|
{ |
206
|
|
|
return base_path('resources/views').'/widgets/'.$this->makeViewName().'.blade.php'; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Get the destination view name without extensions. |
211
|
|
|
* |
212
|
|
|
* @return string |
213
|
|
|
*/ |
214
|
|
|
protected function makeViewName() |
215
|
|
|
{ |
216
|
|
|
$name = str_replace($this->laravel->getNamespace(), '', $this->argument('name')); |
|
|
|
|
217
|
|
|
$name = str_replace('\\', '/', $name); |
218
|
|
|
|
219
|
|
|
// convert to snake_case part by part to avoid unexpected underscores. |
220
|
|
|
$nameArray = explode('/', $name); |
221
|
|
|
array_walk($nameArray, function (&$part) { |
222
|
|
|
$part = snake_case($part); |
223
|
|
|
}); |
224
|
|
|
|
225
|
|
|
return implode('/', $nameArray); |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.