1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package CLI |
5
|
|
|
* @author Iurii Makukh <[email protected]> |
6
|
|
|
* @copyright Copyright (c) 2018, Iurii Makukh <[email protected]> |
7
|
|
|
* @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+ |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace gplcart\modules\cli\controllers; |
11
|
|
|
|
12
|
|
|
use gplcart\core\interfaces\Crud as CrudInterface; |
13
|
|
|
use gplcart\core\models\Alias as AliasModel; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Handles commands related to URL aliases |
17
|
|
|
*/ |
18
|
|
|
class Alias extends Base |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Alias model instance |
23
|
|
|
* @var \gplcart\core\models\Alias $alias |
24
|
|
|
*/ |
25
|
|
|
protected $alias; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param AliasModel $alias |
29
|
|
|
*/ |
30
|
|
|
public function __construct(AliasModel $alias) |
31
|
|
|
{ |
32
|
|
|
parent::__construct(); |
33
|
|
|
|
34
|
|
|
$this->alias = $alias; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Callback for "alias-get" command |
39
|
|
|
*/ |
40
|
|
|
public function cmdGetAlias() |
41
|
|
|
{ |
42
|
|
|
$result = $this->getListAlias(); |
43
|
|
|
$this->outputFormat($result); |
44
|
|
|
$this->outputFormatTableAlias($result); |
45
|
|
|
$this->output(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Callback for "alias-add" command |
50
|
|
|
*/ |
51
|
|
|
public function cmdAddAlias() |
52
|
|
|
{ |
53
|
|
|
$params = $this->getParam(); |
54
|
|
|
|
55
|
|
|
if (count($params) != 3) { |
56
|
|
|
$this->errorAndExit($this->text('Invalid command')); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
list($entity, $entity_id, $alias) = $params; |
60
|
|
|
|
61
|
|
|
if (empty($entity) || empty($entity_id) || empty($alias) || !is_numeric($entity_id)) { |
62
|
|
|
$this->errorAndExit($this->text('Invalid argument')); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if (!$this->alias->loadEntity($entity, $entity_id)) { |
66
|
|
|
$this->errorAndExit($this->text('Invalid argument')); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$result = $this->addAlias($entity, $entity_id, $alias); |
70
|
|
|
|
71
|
|
|
if (empty($result)) { |
72
|
|
|
$this->errorAndExit($this->text('Unexpected result')); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$this->line($result); |
76
|
|
|
$this->output(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Callback for "alias-generate" command |
81
|
|
|
*/ |
82
|
|
|
public function cmdGenerateAlias() |
83
|
|
|
{ |
84
|
|
|
$entity = $this->getParam(0); |
85
|
|
|
$entity_id = $this->getParam(1); |
86
|
|
|
$all = $this->getParam('all'); |
87
|
|
|
|
88
|
|
|
if (empty($entity)) { |
89
|
|
|
$this->errorAndExit($this->text('Invalid command')); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if (!isset($entity_id) && empty($all)) { |
93
|
|
|
$this->errorAndExit($this->text('Invalid command')); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$result = false; |
97
|
|
|
|
98
|
|
|
if (isset($entity_id)) { |
99
|
|
|
|
100
|
|
|
if (!is_numeric($entity_id)) { |
101
|
|
|
$this->errorAndExit($this->text('Invalid argument')); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$data = $this->alias->loadEntity($entity, $entity_id); |
105
|
|
|
|
106
|
|
|
if (empty($data)) { |
107
|
|
|
$this->errorAndExit($this->text('Invalid argument')); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$alias = $this->alias->generateEntity($entity, $data); |
111
|
|
|
|
112
|
|
|
if (empty($alias)) { |
113
|
|
|
$this->errorAndExit($this->text('Unexpected result')); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$result = $this->addAlias($entity, $entity_id, $alias); |
117
|
|
|
|
118
|
|
|
} else if (!empty($all)) { |
119
|
|
|
$result = $this->generateListAlias($entity); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if (empty($result)) { |
123
|
|
|
$this->errorAndExit($this->text('Unexpected result')); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$this->output(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** Add a URL alias |
130
|
|
|
* @param string $entity |
131
|
|
|
* @param int $entity_id |
132
|
|
|
* @param string $alias |
133
|
|
|
* @return int |
134
|
|
|
*/ |
135
|
|
|
protected function addAlias($entity, $entity_id, $alias) |
136
|
|
|
{ |
137
|
|
|
$this->alias->delete(array('entity' => $entity, 'entity_id' => $entity_id)); |
138
|
|
|
|
139
|
|
|
$data = array( |
140
|
|
|
'alias' => $alias, |
141
|
|
|
'entity' => $entity, |
142
|
|
|
'entity_id' => $entity_id |
143
|
|
|
); |
144
|
|
|
|
145
|
|
|
return $this->alias->add($data); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Mass generate URL aliases for the given entity |
150
|
|
|
* @param string $entity |
151
|
|
|
* @return bool |
152
|
|
|
*/ |
153
|
|
|
protected function generateListAlias($entity) |
154
|
|
|
{ |
155
|
|
|
if (!$this->alias->isSupportedEntity($entity)) { |
156
|
|
|
$this->errorAndExit($this->text('Invalid argument')); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
$model = gplcart_instance_model($entity); |
160
|
|
|
|
161
|
|
|
if (!$model instanceof CrudInterface) { |
|
|
|
|
162
|
|
|
$this->errorAndExit($this->text('Invalid argument')); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
ini_set('memory_limit', '-1'); |
166
|
|
|
|
167
|
|
|
$added = $count = 0; |
168
|
|
|
|
169
|
|
|
foreach ($model->getList() as $item) { |
170
|
|
|
|
171
|
|
|
if (empty($item["{$entity}_id"])) { |
172
|
|
|
continue; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
$count++; |
176
|
|
|
$entity_id = $item["{$entity}_id"]; |
177
|
|
|
|
178
|
|
|
$this->alias->delete(array('entity' => $entity, 'entity_id' => $entity_id)); |
179
|
|
|
|
180
|
|
|
$data = array( |
181
|
|
|
'entity' => $entity, |
182
|
|
|
'entity_id' => $entity_id, |
183
|
|
|
'alias' => $this->alias->generateEntity($entity, $item) |
184
|
|
|
); |
185
|
|
|
|
186
|
|
|
if ($this->alias->add($data)) { |
187
|
|
|
$added++; |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return $count && $count == $added; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Callback for "alias-delete" command |
197
|
|
|
*/ |
198
|
|
|
public function cmdDeleteAlias() |
199
|
|
|
{ |
200
|
|
|
$id = $this->getParam(0); |
201
|
|
|
$all = $this->getParam('all'); |
202
|
|
|
|
203
|
|
|
if (!isset($id) && !$all) { |
204
|
|
|
$this->errorAndExit($this->text('Invalid command')); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
$options = $result = null; |
208
|
|
|
|
209
|
|
|
if (isset($id)) { |
210
|
|
|
|
211
|
|
|
if ($this->getParam('entity')) { |
212
|
|
|
$options = array('entity' => $id); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
} else if (!empty($all)) { |
216
|
|
|
$options = array(); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
if (isset($options)) { |
220
|
|
|
|
221
|
|
|
$deleted = $count = 0; |
222
|
|
|
foreach ($this->alias->getList($options) as $item) { |
223
|
|
|
$count++; |
224
|
|
|
$deleted += (int) $this->alias->delete($item['alias_id']); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
$result = $count && $count == $deleted; |
228
|
|
|
|
229
|
|
|
} else if (isset($id)) { |
230
|
|
|
|
231
|
|
|
if (!is_numeric($id)) { |
232
|
|
|
$this->errorAndExit($this->text('Invalid argument')); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
$result = $this->alias->delete($id); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
if (empty($result)) { |
239
|
|
|
$this->errorAndExit($this->text('Unexpected result')); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
$this->output(); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Returns an array of URL aliases |
247
|
|
|
* @return array |
248
|
|
|
*/ |
249
|
|
|
protected function getListAlias() |
250
|
|
|
{ |
251
|
|
|
$id = $this->getParam(0); |
252
|
|
|
|
253
|
|
|
if (!isset($id)) { |
254
|
|
|
return $this->alias->getList(array('limit' => $this->getLimit())); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
if ($this->getParam('entity')) { |
258
|
|
|
return $this->alias->getList(array('entity' => $id, 'limit' => $this->getLimit())); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
if (!is_numeric($id)) { |
262
|
|
|
$this->errorAndExit($this->text('Invalid argument')); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
$result = $this->alias->get($id); |
266
|
|
|
|
267
|
|
|
if (empty($result)) { |
268
|
|
|
$this->errorAndExit($this->text('Unexpected result')); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
return array($result); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Output table format |
276
|
|
|
* @param array $items |
277
|
|
|
*/ |
278
|
|
|
protected function outputFormatTableAlias(array $items) |
279
|
|
|
{ |
280
|
|
|
$header = array( |
281
|
|
|
$this->text('ID'), |
282
|
|
|
$this->text('Alias'), |
283
|
|
|
$this->text('Entity'), |
284
|
|
|
$this->text('Entity ID') |
285
|
|
|
); |
286
|
|
|
|
287
|
|
|
$rows = array(); |
288
|
|
|
|
289
|
|
|
foreach ($items as $item) { |
290
|
|
|
$rows[] = array( |
291
|
|
|
$item['alias_id'], |
292
|
|
|
$item['alias'], |
293
|
|
|
$item['entity'], |
294
|
|
|
$item['entity_id'] |
295
|
|
|
); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
$this->outputFormatTable($rows, $header); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
} |
302
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.