1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Imanghafoori\Widgets; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\GeneratorCommand as LaravelGeneratorCommand; |
6
|
|
|
|
7
|
|
|
class WidgetGenerator extends LaravelGeneratorCommand |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* The name and signature of the console command. |
11
|
|
|
* |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $signature = 'make:widget {name : The name of the widget class} {--p|plain}'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The console command description. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $description = 'Create a new widget class'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* String to store the command type. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $type = 'Widget Class'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Execute the console command. |
32
|
|
|
* |
33
|
|
|
* @return void |
34
|
|
|
*/ |
35
|
|
|
public function fire() |
36
|
|
|
{ |
37
|
|
|
$this->_makeWidgetClass(); |
38
|
|
|
|
39
|
|
|
if (!$this->option('plain')) { |
40
|
|
|
$this->createView(); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Create a new view file for the widget. |
46
|
|
|
* |
47
|
|
|
* return void |
48
|
|
|
*/ |
49
|
|
|
private function createView() |
50
|
|
|
{ |
51
|
|
|
$path = $this->_getViewPath(); |
52
|
|
|
|
53
|
|
|
if ($this->files->exists($path)) { |
54
|
|
|
$this->error($this->qualifyClass($this->getNameInput())."View.blade.php - Already exists! (@_@)"); |
55
|
|
|
|
56
|
|
|
return; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$this->files->put($path, $this->_getViewStub()); |
60
|
|
|
|
61
|
|
|
$this->info(' - '.$this->qualifyClass($this->getNameInput())."View.blade.php - was created. (^_^)"); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get the stub file for the generator. |
66
|
|
|
* |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
protected function getStub() |
70
|
|
|
{ |
71
|
|
|
$stubName = $this->option('plain') ? 'widget_plain' : 'widget'; |
72
|
|
|
return __DIR__ . "/../stubs/$stubName.stub"; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get the default namespace for the class. |
77
|
|
|
* |
78
|
|
|
* @param string $rootNamespace |
79
|
|
|
* |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
protected function getDefaultNamespace($rootNamespace) |
83
|
|
|
{ |
84
|
|
|
return $rootNamespace . '\\Widgets'; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get the console command options. |
90
|
|
|
* |
91
|
|
|
* @return array |
92
|
|
|
*/ |
93
|
|
|
protected function getOptions() |
94
|
|
|
{ |
95
|
|
|
return [ |
96
|
|
|
['plain', null, InputOption::VALUE_NONE, 'No docs on widget class. No view is being created too.'], |
97
|
|
|
]; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return mixed|string |
102
|
|
|
*/ |
103
|
|
|
private function _getViewPath() |
104
|
|
|
{ |
105
|
|
|
$name = $this->qualifyClass($this->getNameInput()); |
106
|
|
|
$path = $this->getPath($name); |
107
|
|
|
$path = str_replace('.php', 'View.blade.php', $path); |
108
|
|
|
return $path; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Creates the widget class |
113
|
|
|
* @return bool |
114
|
|
|
*/ |
115
|
|
|
private function _makeWidgetClass() |
116
|
|
|
{ |
117
|
|
|
$name = $this->qualifyClass($this->getNameInput()); |
118
|
|
|
|
119
|
|
|
$path = $this->getPath($name); |
120
|
|
|
|
121
|
|
|
// First we will check to see if the class already exists. If it does, we don't want |
122
|
|
|
// to create the class and overwrite the user's code. So, we will bail out so the |
123
|
|
|
// code is untouched. Otherwise, we will continue generating this class' files. |
124
|
|
|
if ($this->alreadyExists($this->getNameInput())) { |
125
|
|
|
$this->error($this->qualifyClass($this->getNameInput()).".php - Already exists (@_@)"); |
126
|
|
|
|
127
|
|
|
return false; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
// Next, we will generate the path to the location where this class' file should get |
131
|
|
|
// written. Then, we will build the class and make the proper replacements on the |
132
|
|
|
// stub files so that it gets the correctly formatted namespace and class name. |
133
|
|
|
$this->makeDirectory($path); |
134
|
|
|
|
135
|
|
|
$this->files->put($path, $this->buildClass($name)); |
136
|
|
|
|
137
|
|
|
$this->info(' - '.$name.'.php - was created. (^_^)'); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
|
|
private function _getViewStub() |
144
|
|
|
{ |
145
|
|
|
return 'Note that you can reference partials within "Widgets" folder like this: @include("Widgets::somePartial")'; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|