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