1 | <?php namespace Distilleries\Expendable\Console; |
||
9 | class ComponentMakeCommand extends \Illuminate\Console\GeneratorCommand { |
||
10 | |||
11 | use DetectsApplicationNamespace; |
||
12 | /** |
||
13 | * The console command name. |
||
14 | * |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $name = 'expendable:component.make'; |
||
18 | |||
19 | |||
20 | protected $states; |
||
21 | protected $model; |
||
22 | protected $form; |
||
23 | protected $datatable; |
||
24 | protected $template; |
||
25 | |||
26 | /** |
||
27 | * The console command description. |
||
28 | * |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $description = 'Creates a controller for a backend component builder class.'; |
||
32 | |||
33 | /** |
||
34 | * @var ComponentGenerator |
||
35 | */ |
||
36 | protected $formGenerator; |
||
37 | |||
38 | 576 | public function __construct(Filesystem $files, ComponentGenerator $formGenerator) |
|
39 | { |
||
40 | 576 | parent::__construct($files); |
|
41 | 576 | $this->formGenerator = $formGenerator; |
|
42 | } |
||
43 | |||
44 | 24 | public function handle() |
|
45 | { |
||
46 | 24 | $this->initOptions(); |
|
47 | 24 | parent::handle(); |
|
48 | } |
||
49 | |||
50 | |||
51 | |||
52 | /** |
||
53 | * Get the console command arguments. |
||
54 | * |
||
55 | * @return array |
||
56 | */ |
||
57 | 576 | protected function getArguments() |
|
58 | { |
||
59 | return array( |
||
60 | 576 | array('name', InputArgument::REQUIRED, 'Full path for Component class.'), |
|
61 | ); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Get the console command options. |
||
66 | * |
||
67 | * @return array |
||
68 | */ |
||
69 | 576 | protected function getOptions() |
|
70 | { |
||
71 | return array( |
||
72 | 576 | array('states', [], InputOption::VALUE_OPTIONAL, 'States use on the controller', ''), |
|
73 | array('template', null, InputOption::VALUE_OPTIONAL, 'Template use to generate the controller', ''), |
||
74 | array('model', null, InputOption::VALUE_OPTIONAL, 'Model use', ''), |
||
75 | array('datatable', null, InputOption::VALUE_OPTIONAL, 'Datatable use', ''), |
||
76 | array('form', null, InputOption::VALUE_OPTIONAL, 'Form Use', ''), |
||
77 | |||
78 | ); |
||
79 | } |
||
80 | |||
81 | 24 | protected function initOptions() |
|
82 | { |
||
83 | 24 | $states = $this->option('states'); |
|
84 | |||
85 | 24 | if (!empty($states)) |
|
86 | { |
||
87 | 16 | $this->states = explode(',', $states); |
|
88 | } else |
||
89 | { |
||
90 | 8 | $this->states = []; |
|
91 | } |
||
92 | |||
93 | 24 | $this->template = $this->option('template'); |
|
94 | 24 | $this->model = $this->option('model'); |
|
95 | 24 | $this->datatable = $this->option('datatable'); |
|
96 | 24 | $this->form = $this->option('form'); |
|
97 | |||
98 | } |
||
99 | |||
100 | |||
101 | 24 | protected function getTemplate() |
|
102 | { |
||
103 | |||
104 | 24 | if (empty($this->template)) |
|
105 | { |
||
106 | 20 | $defaultComponent = 0; |
|
107 | |||
108 | |||
109 | 20 | foreach ($this->states as $state) |
|
110 | { |
||
111 | 16 | if (strpos($state, 'DatatableStateContract') !== false || (strpos($state, 'FormStateContract') !== false)) |
|
112 | { |
||
113 | 16 | $defaultComponent++; |
|
114 | |||
115 | } |
||
116 | } |
||
117 | |||
118 | |||
119 | 20 | if ($defaultComponent == 2 && !empty($this->model)) |
|
120 | { |
||
121 | 8 | $template = 'controller-base-component-class-template'; |
|
122 | 12 | } else if (!empty($this->model)) |
|
123 | { |
||
124 | 4 | $template = 'controller-base-model-class-template'; |
|
125 | } else |
||
126 | { |
||
127 | 8 | $template = 'controller-base-class-template'; |
|
128 | } |
||
129 | |||
130 | 20 | return $template; |
|
131 | } |
||
132 | |||
133 | 4 | return $this->template; |
|
134 | |||
135 | } |
||
136 | |||
137 | /** |
||
138 | * Replace the class name for the given stub. |
||
139 | * |
||
140 | * @param string $stub |
||
141 | * @param string $name |
||
142 | * @return string |
||
143 | */ |
||
144 | 24 | protected function replaceClass($stub, $name) |
|
194 | |||
195 | /** |
||
196 | * Replace the namespace for the given stub. |
||
197 | * |
||
198 | * @param string $stub |
||
199 | * @param string $name |
||
200 | * @return $this |
||
201 | */ |
||
202 | 24 | protected function replaceNamespace(&$stub, $name) |
|
212 | |||
213 | /** |
||
214 | * Get the desired class name from the input. |
||
215 | * |
||
216 | * @return string |
||
217 | */ |
||
218 | 24 | protected function getNameInput() |
|
222 | |||
223 | /** |
||
224 | * Get the stub file for the generator. |
||
225 | * |
||
226 | * @return string |
||
227 | */ |
||
228 | 24 | protected function getStub() |
|
232 | } |