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