|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @package Skeleton module |
|
5
|
|
|
* @author Iurii Makukh <[email protected]> |
|
6
|
|
|
* @copyright Copyright (c) 2015, Iurii Makukh |
|
7
|
|
|
* @license https://www.gnu.org/licenses/gpl.html GNU/GPLv3 |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace gplcart\modules\skeleton\controllers; |
|
11
|
|
|
|
|
12
|
|
|
use gplcart\core\controllers\backend\Controller; |
|
13
|
|
|
use gplcart\modules\skeleton\models\Extractor; |
|
14
|
|
|
use gplcart\modules\skeleton\models\Generator; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Handles incoming requests and outputs data related to Skeleton module |
|
18
|
|
|
*/ |
|
19
|
|
|
class Skeleton extends Controller |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Extractor model instance |
|
24
|
|
|
* @var \gplcart\modules\skeleton\models\Extractor $extractor |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $extractor; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Module generator instance |
|
30
|
|
|
* @var \gplcart\modules\skeleton\models\Generator $generator |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $generator; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Skeleton constructor. |
|
36
|
|
|
* @param Extractor $extractor |
|
37
|
|
|
* @param Generator $generator |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct(Extractor $extractor, Generator $generator) |
|
40
|
|
|
{ |
|
41
|
|
|
parent::__construct(); |
|
42
|
|
|
|
|
43
|
|
|
$this->extractor = $extractor; |
|
44
|
|
|
$this->generator = $generator; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Displays the generate skeleton page |
|
49
|
|
|
*/ |
|
50
|
|
|
public function editSkeleton() |
|
51
|
|
|
{ |
|
52
|
|
|
$this->downloadSkeleton(); |
|
53
|
|
|
$this->setTitleEditSkeleton(); |
|
54
|
|
|
$this->setBreadcrumbEditSkeleton(); |
|
55
|
|
|
|
|
56
|
|
|
$hooks = gplcart_array_split($this->extractor->getHookScopes(), 2); |
|
57
|
|
|
|
|
58
|
|
|
$this->setData('hooks', $hooks); |
|
59
|
|
|
$this->setData('licenses', $this->generator->getLicenses()); |
|
60
|
|
|
|
|
61
|
|
|
$author = $this->getUser('name') . ' <' . $this->getUser('email') . '>'; |
|
62
|
|
|
$this->setData('author', $author); |
|
63
|
|
|
|
|
64
|
|
|
$this->submitSkeleton(); |
|
65
|
|
|
$this->generateFromJobSkeleton(); |
|
66
|
|
|
$this->outputEditSkeleton(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Generate a module skeleton after hook extraction |
|
71
|
|
|
*/ |
|
72
|
|
|
protected function generateFromJobSkeleton() |
|
73
|
|
|
{ |
|
74
|
|
|
if ($this->isQuery('skeleton_hooks_extracted')) { |
|
75
|
|
|
|
|
76
|
|
|
$job = $this->job->get('skeleton'); |
|
77
|
|
|
|
|
78
|
|
|
if (isset($job['context']['extracted'])) { |
|
79
|
|
|
|
|
80
|
|
|
$data = $job['data']['submitted']; |
|
81
|
|
|
$data['hooks'] = $job['context']['extracted']; |
|
82
|
|
|
|
|
83
|
|
|
if (!empty($job['errors'])) { |
|
84
|
|
|
$this->setMessage($this->text('@num errors occurred during hook extraction'), 'warning', true); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
$this->generateSkeleton($data); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Handles submitted data |
|
94
|
|
|
*/ |
|
95
|
|
|
protected function submitSkeleton() |
|
96
|
|
|
{ |
|
97
|
|
|
if ($this->isPosted('create') && $this->validateSkeleton()) { |
|
98
|
|
|
$this->createSkeleton(); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Validates an array of submitted data |
|
104
|
|
|
*/ |
|
105
|
|
|
protected function validateSkeleton() |
|
106
|
|
|
{ |
|
107
|
|
|
$this->setSubmitted('skeleton', null, false); |
|
108
|
|
|
|
|
109
|
|
|
$this->validateElement('module.id', 'required'); |
|
110
|
|
|
$this->validateElement('module.core', 'required'); |
|
111
|
|
|
$this->validateElement('module.author', 'required'); |
|
112
|
|
|
$this->validateElement('module.version', 'required'); |
|
113
|
|
|
|
|
114
|
|
|
if ($this->hasErrors()) { |
|
115
|
|
|
return false; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
$module_id = $this->getSubmitted('module.id'); |
|
119
|
|
|
|
|
120
|
|
|
if (!$this->module->isValidId($module_id)) { |
|
121
|
|
|
$error = $this->text('@field has invalid value', array('@field' => $this->text('ID'))); |
|
122
|
|
|
$this->setError('module.id', $error); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
$core = $this->getSubmitted('module.core'); |
|
126
|
|
|
|
|
127
|
|
|
if (preg_match('/^\d/', $core) !== 1) { |
|
128
|
|
|
$error = $this->text('@field has invalid value', array('@field' => $this->text('Core'))); |
|
129
|
|
|
$this->setError('module.core', $error); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
if ($this->hasErrors()) { |
|
133
|
|
|
return false; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
$name = $this->getSubmitted('module.name'); |
|
137
|
|
|
|
|
138
|
|
|
if (empty($name)) { |
|
139
|
|
|
$this->setSubmitted('module.name', $module_id); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
return true; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Creates a skeleton for a module |
|
147
|
|
|
*/ |
|
148
|
|
|
protected function createSkeleton() |
|
149
|
|
|
{ |
|
150
|
|
|
if ($this->isSubmitted('hooks')) { |
|
151
|
|
|
$this->setExtractionJobSkeleton(); |
|
152
|
|
|
} else { |
|
153
|
|
|
$this->generateSkeleton($this->getSubmitted()); |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Generate skeleton files |
|
159
|
|
|
* @param array $data |
|
160
|
|
|
*/ |
|
161
|
|
|
protected function generateSkeleton(array $data) |
|
162
|
|
|
{ |
|
163
|
|
|
if (!$this->generator->generate($data)) { |
|
164
|
|
|
$this->redirect('', $this->text('An error occurred'), 'warning'); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
$file = gplcart_string_encode($this->generator->getZip()); |
|
168
|
|
|
$vars = array('@url' => $this->url('', array('download' => $file))); |
|
169
|
|
|
$this->redirect('', $this->text('Module has been generated. <a href="@url">Download</a>', $vars), 'success'); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Output generated skeleton to download |
|
174
|
|
|
*/ |
|
175
|
|
|
protected function downloadSkeleton() |
|
176
|
|
|
{ |
|
177
|
|
|
$path = $this->getQuery('download'); |
|
178
|
|
|
|
|
179
|
|
|
if (!empty($path)) { |
|
180
|
|
|
$this->download(gplcart_string_decode($path)); |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Set up hook extraction job |
|
186
|
|
|
*/ |
|
187
|
|
|
protected function setExtractionJobSkeleton() |
|
188
|
|
|
{ |
|
189
|
|
|
$job = array( |
|
190
|
|
|
'id' => 'skeleton', |
|
191
|
|
|
'total' => $this->getTotalExtractSkeleton(), |
|
192
|
|
|
'data' => array('submitted' => $this->getSubmitted()), |
|
193
|
|
|
'redirect' => array( |
|
194
|
|
|
'finish' => $this->url('', array('skeleton_hooks_extracted' => 1)) |
|
195
|
|
|
), |
|
196
|
|
|
'message' => array( |
|
197
|
|
|
'process' => $this->text('Extracting hooks from the source files...'), |
|
198
|
|
|
), |
|
199
|
|
|
); |
|
200
|
|
|
|
|
201
|
|
|
$this->job->submit($job); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Returns a total number of scanned files to extract hooks from |
|
206
|
|
|
* @return integer |
|
207
|
|
|
*/ |
|
208
|
|
|
protected function getTotalExtractSkeleton() |
|
209
|
|
|
{ |
|
210
|
|
|
return (int) $this->extractor->scan(GC_DIR_CORE, true); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* Set title on the edit module page |
|
215
|
|
|
*/ |
|
216
|
|
|
protected function setBreadcrumbEditSkeleton() |
|
217
|
|
|
{ |
|
218
|
|
|
$breadcrumb = array( |
|
219
|
|
|
'text' => $this->text('Dashboard'), |
|
220
|
|
|
'url' => $this->url('admin') |
|
221
|
|
|
); |
|
222
|
|
|
|
|
223
|
|
|
$this->setBreadcrumb($breadcrumb); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* Set title on the generate skeleton page |
|
228
|
|
|
*/ |
|
229
|
|
|
protected function setTitleEditSkeleton() |
|
230
|
|
|
{ |
|
231
|
|
|
$this->setTitle($this->text('Skeleton')); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* Render and output the generate skeleton page |
|
236
|
|
|
*/ |
|
237
|
|
|
protected function outputEditSkeleton() |
|
238
|
|
|
{ |
|
239
|
|
|
$this->output('skeleton|edit'); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
} |
|
243
|
|
|
|