1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace controllers; |
4
|
|
|
|
5
|
|
|
use generators\AddonXml\AddonXmlGenerator; |
6
|
|
|
use generators\Language\LanguageGenerator; |
7
|
|
|
use generators\MultipleFileGenerator; |
8
|
|
|
use terminal\Terminal; |
9
|
|
|
use filesystem\Filesystem; |
10
|
|
|
use autocomplete\Autocomplete; |
11
|
|
|
use mediators\GeneratorMediator; |
12
|
|
|
use \Config; |
13
|
|
|
|
14
|
|
|
class AddonXml extends AbstractController |
15
|
|
|
{ |
16
|
|
|
private $mfGenerator; |
17
|
|
|
private static $allowedMethods = [ |
18
|
|
|
'help', |
19
|
|
|
'create', |
20
|
|
|
'remove', |
21
|
|
|
'update' |
22
|
|
|
]; |
23
|
|
|
|
24
|
|
|
use HelpTrait; |
25
|
|
|
|
26
|
|
|
function __construct( |
27
|
|
|
Config $config, |
28
|
|
|
Terminal $terminal, |
29
|
|
|
Filesystem $filesystem, |
30
|
|
|
Autocomplete $autocomplete |
31
|
|
|
) |
32
|
|
|
{ |
33
|
|
|
$this->config = $config; |
34
|
|
|
$this->terminal = $terminal; |
35
|
|
|
$this->filesystem = $filesystem; |
36
|
|
|
$this->autocomplete = $autocomplete; |
37
|
|
|
|
38
|
|
|
$addonXmlGenerator = new AddonXmlGenerator($this->config); |
39
|
|
|
$languageGenerator = new LanguageGenerator($this->config); |
40
|
|
|
$generatorMediator = new GeneratorMediator(); |
41
|
|
|
|
42
|
|
|
$generatorMediator |
43
|
|
|
->addGenerator($addonXmlGenerator) |
44
|
|
|
->addGenerator($languageGenerator); |
45
|
|
|
|
46
|
|
|
$this->mfGenerator = new MultipleFileGenerator($this->filesystem); |
47
|
|
|
$this->mfGenerator |
48
|
|
|
->addGenerator($addonXmlGenerator) |
49
|
|
|
->addGenerator($languageGenerator); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @inheritdoc |
54
|
|
|
*/ |
55
|
|
|
public static function getAllowedMethods(): array |
56
|
|
|
{ |
57
|
|
|
return self::$allowedMethods; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* help: |
62
|
|
|
* addon-xml create |
63
|
|
|
* creates addonXml structure and write it to file |
64
|
|
|
* @throws Exception if file already exists |
65
|
|
|
*/ |
66
|
|
|
public function create() |
67
|
|
|
{ |
68
|
|
|
$addonXmlFileGenerator = $this->mfGenerator |
69
|
|
|
->find(AddonXmlGenerator::class); |
70
|
|
|
|
71
|
|
|
$addonXmlGenerator = $addonXmlFileGenerator |
72
|
|
|
->throwIfExists('Such addon.xml already exists. Remove it first if you want to replace it.') |
73
|
|
|
->extract(); |
74
|
|
|
|
75
|
|
|
$addonXmlGenerator |
76
|
|
|
->create(); |
77
|
|
|
|
78
|
|
|
$addonXmlFileGenerator |
79
|
|
|
->write() |
80
|
|
|
->throwIfNotExists($addonXmlGenerator->getPath() . ' cannot be created.'); |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* results |
84
|
|
|
*/ |
85
|
|
|
$this->terminal->success($addonXmlGenerator->getPath() . ' was created'); |
86
|
|
|
$this->terminal->diff( |
87
|
|
|
\Diff::toString(\Diff::compare('', $addonXmlGenerator->toString())) |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* help: |
93
|
|
|
* addon-xml remove |
94
|
|
|
* removes file addon.xml |
95
|
|
|
* @throws Exception if file doesn't exists |
96
|
|
|
*/ |
97
|
|
|
public function remove() |
98
|
|
|
{ |
99
|
|
|
$addonXmlGenerator = $this->mfGenerator |
100
|
|
|
->find(AddonXmlGenerator::class) |
101
|
|
|
->read() |
102
|
|
|
->remove() |
103
|
|
|
->throwIfExists('File cannot be removed.') |
104
|
|
|
->extract(); |
105
|
|
|
|
106
|
|
|
$this->terminal->success($addonXmlGenerator->getPath() . ' was removed'); |
107
|
|
|
$this->terminal->diff( |
108
|
|
|
\Diff::toString(\Diff::compare($addonXmlGenerator->toString(), '')) |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* help: |
114
|
|
|
* addon-xml/update --addon.id <addon_id> <item> [remove] [...args] |
115
|
|
|
* Sets additional field to addon xml file |
116
|
|
|
* addon.id - id of the addon |
117
|
|
|
* --- |
118
|
|
|
* --settings-item - <item id="date">...</item> |
119
|
|
|
* args: section=<section_id> type=<type> id=<id> [dv=<default_value>] [v=<variants>] |
120
|
|
|
* section - id for the settings section |
121
|
|
|
* type - type of the item id: input, textarea, password, checkbox, selectbox, multiple select, multiple checkboxes, countries list, states list, file, info, header, template |
122
|
|
|
* id - id of the setting item |
123
|
|
|
* default_value - (df) - default value for setting item |
124
|
|
|
* variants - (v) - list of item value variants comma separated |
125
|
|
|
* --- |
126
|
|
|
* |
127
|
|
|
* see more @link [https://www.cs-cart.ru/docs/4.9.x/developer_guide/addons/scheme/scheme3.0_structure.html] |
128
|
|
|
* @throws Exception if file doesn't exists |
129
|
|
|
*/ |
130
|
|
|
public function update() |
131
|
|
|
{ |
132
|
|
|
$this->mfGenerator |
133
|
|
|
->throwIfNotExists('Some addon file not found.') |
134
|
|
|
->read(); |
135
|
|
|
|
136
|
|
|
$old_content = []; |
137
|
|
|
$this->mfGenerator->each(function($generator) use (&$old_content) { |
138
|
|
|
$old_content[get_class($generator->extract())] = $generator->extract()->toString(); |
139
|
|
|
}); |
140
|
|
|
|
141
|
|
|
$addonXmlGenerator = $this->mfGenerator |
142
|
|
|
->find(AddonXmlGenerator::class) |
143
|
|
|
->extract(); |
|
|
|
|
144
|
|
|
|
145
|
|
|
$method = $this->getMethodName(); |
146
|
|
|
$class_name = get_class($this); |
147
|
|
|
if (method_exists($class_name, $method)) { |
148
|
|
|
$this->{$method}($addonXmlGenerator); |
149
|
|
|
} else { |
150
|
|
|
throw new \BadMethodCallException('There is no such command'); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$this->mfGenerator |
154
|
|
|
->write() |
155
|
|
|
->throwIfNotExists(); |
156
|
|
|
|
157
|
|
|
$this->mfGenerator->each(function($generator) use ($old_content) { |
158
|
|
|
$this->terminal->diff( |
159
|
|
|
\Diff::toString(\Diff::compare($old_content[get_class($generator->extract())], $generator->extract()->toString())) |
160
|
|
|
); |
161
|
|
|
}); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* |
166
|
|
|
*/ |
167
|
|
|
public function setSettingsItem($addonXmlGenerator) |
168
|
|
|
{ |
169
|
|
|
$addonXmlGenerator->setSetting( |
170
|
|
|
$this->config->get('section'), |
171
|
|
|
$this->config->get('type'), |
172
|
|
|
$this->config->get('id'), |
173
|
|
|
$this->config->getOr('default_value', 'dv') ?: '', |
174
|
|
|
(function($config) { |
175
|
|
|
$variants = $config->getOr('variants', 'v'); |
176
|
|
|
|
177
|
|
|
return $variants ? explode(',', $variants) : []; |
178
|
|
|
})($this->config) |
179
|
|
|
); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public function removeSettingsItem($addonXmlGenerator) |
183
|
|
|
{ |
184
|
|
|
$addonXmlGenerator->removeSetting( |
185
|
|
|
$this->config->get('id') |
186
|
|
|
); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Autocomplete addon param |
191
|
|
|
*/ |
192
|
|
|
public function createAutocomplete($prev = null, $cur = null, $arguments = []) |
193
|
|
|
{ |
194
|
|
|
$generator = $this->mfGenerator |
195
|
|
|
->find(AddonXmlGenerator::class) |
196
|
|
|
->extract(); |
197
|
|
|
|
198
|
|
|
return $this->autocomplete->combineQueueParam( |
199
|
|
|
$this->autocomplete->queueArgument('addon.id'), |
200
|
|
|
$this->autocomplete->queueArgument('addon.scheme', function() use ($generator) { |
201
|
|
|
return $generator->getVariants('scheme'); |
202
|
|
|
}), |
203
|
|
|
$this->autocomplete->queueArgument('addon.status', function() use ($generator) { |
204
|
|
|
return $generator->getVariants('status'); |
205
|
|
|
}), |
206
|
|
|
$this->autocomplete->queueArgument('addon.edition_type'), |
207
|
|
|
$this->autocomplete->queueArgument('addon.priority'), |
208
|
|
|
$this->autocomplete->queueArgument('addon.position') |
209
|
|
|
); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
public function updateAutocomplete() |
213
|
|
|
{ |
214
|
|
|
$autocomplete = $this->autocomplete; |
215
|
|
|
$arguments = $this->terminal->getArguments(); |
216
|
|
|
|
217
|
|
|
if (!empty($arguments['set']) && $arguments['set'] === 'settings-item') { |
218
|
|
|
return $this->setSettingsItemAutocomplete(); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
return $this->autocomplete->combineQueueParam( |
222
|
|
|
$this->autocomplete->queueArgument('addon.id', function() use ($autocomplete) { |
223
|
|
|
return $autocomplete->getAddonsList(); |
224
|
|
|
}), |
225
|
|
|
$this->autocomplete->queueArgument('set', ['settings-item']) |
226
|
|
|
); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
public function setSettingsItemAutocomplete() |
230
|
|
|
{ |
231
|
|
|
$autocomplete = $this->autocomplete; |
|
|
|
|
232
|
|
|
$generator = $this->mfGenerator |
233
|
|
|
->find(AddonXmlGenerator::class) |
234
|
|
|
->extract(); |
235
|
|
|
|
236
|
|
|
return $this->autocomplete->combineQueueParam( |
237
|
|
|
$this->autocomplete->queueArgument('type', function() use ($generator) { |
238
|
|
|
return $generator->getVariants('item'); |
239
|
|
|
}), |
240
|
|
|
$this->autocomplete->queueArgument('section'), |
241
|
|
|
$this->autocomplete->queueArgument('id') |
242
|
|
|
); |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|