Skip to content

Commit a5fbf10

Browse files
committed
!410 - update
1 parent e38fe77 commit a5fbf10

File tree

9 files changed

+998
-66
lines changed

9 files changed

+998
-66
lines changed

apps/Core/Components/Devtools/Modules/ModulesComponent.php

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,21 @@ public function viewAction()
2626

2727
$modules = [];
2828

29+
try {
30+
$coreJson = $this->modulesPackage->validateJson(
31+
[
32+
'json' => $this->localContent->read('system/Base/Installer/Packages/Setup/Register/Modules/Packages/Providers/Core/package.json'),
33+
'returnJson' => 'array'
34+
]
35+
);
36+
37+
$coreJson['id'] = $this->core->core['id'];
38+
} catch (\throwable $e) {
39+
throw new \Exception($e->getMessage());
40+
}
41+
2942
$modules['core']['value'] = 'Core';
30-
$modules['core']['childs'][1] = $this->core->core;
43+
$modules['core']['childs'][1] = $coreJson;
3144

3245
$componentsArr = msort($this->modules->components->components, 'name');
3346
foreach ($componentsArr as $key => &$component) {
@@ -117,13 +130,14 @@ public function viewAction()
117130

118131
if ($this->getData()['id'] != 0) {
119132
if ($type === 'core') {
120-
$core = $this->core->core;
121-
122-
if (is_array($core['settings'])) {
123-
$core['settings'] = Json::encode($core['settings'], JSON_UNESCAPED_SLASHES);
133+
if (is_array($coreJson['settings'])) {
134+
$coreJson['settings'] = $this->modulesPackage->formatJson(['json' => $coreJson['settings']]);
135+
}
136+
if (is_array($coreJson['dependencies'])) {
137+
$coreJson['dependencies'] = $this->modulesPackage->formatJson(['json' => $coreJson['dependencies']]);
124138
}
125139

126-
$this->view->module = $core;
140+
$this->view->module = $coreJson;
127141
} else if ($type === 'components') {
128142
$component = $this->modules->components->getById($this->getData()['id']);
129143

@@ -167,4 +181,38 @@ public function updateAction()
167181
$this->addResponse('Method Not Allowed', 1);
168182
}
169183
}
170-
}
184+
185+
public function validateJsonAction()
186+
{
187+
if ($this->request->isPost()) {
188+
if (!$this->checkCSRF()) {
189+
return;
190+
}
191+
$this->modulesPackage->updateModules($this->postData());
192+
193+
$this->addResponse($this->modulesPackage->packagesData->responseMessage, $this->modulesPackage->packagesData->responseCode);
194+
} else {
195+
$this->addResponse('Method Not Allowed', 1);
196+
}
197+
}
198+
199+
public function formatJsonAction()
200+
{
201+
if ($this->request->isPost()) {
202+
if (!$this->checkCSRF()) {
203+
return;
204+
}
205+
206+
$this->modulesPackage->formatJson($this->postData());
207+
208+
209+
$this->addResponse(
210+
$this->modulesPackage->packagesData->responseMessage,
211+
$this->modulesPackage->packagesData->responseCode,
212+
$this->modulesPackage->packagesData->responseData
213+
);
214+
} else {
215+
$this->addResponse('Method Not Allowed', 1);
216+
}
217+
}
218+
}

apps/Core/Packages/Devtools/Modules/Modules.php

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use Phalcon\Helper\Arr;
1010
use Phalcon\Helper\Json;
1111
use Phalcon\Helper\Str;
12+
use Seld\JsonLint\JsonParser;
13+
use Seld\JsonLint\ParsingException;
1214
use System\Base\BasePackage;
1315

1416
class Modules extends BasePackage
@@ -228,4 +230,114 @@ protected function updateView(array $data)
228230
throw $exception;
229231
}
230232
}
233+
234+
public function validateJson($data)
235+
{
236+
if (!isset($data['json'])) {
237+
$this->addResponse('Json data not provided.', 1);
238+
239+
return;
240+
}
241+
242+
try {
243+
$parser = new JsonParser();
244+
245+
$result = $parser->lint($data['json']);
246+
247+
$parser->parse($data['json'], JsonParser::DETECT_KEY_CONFLICTS);
248+
} catch (ParsingException | \throwable $e) {
249+
if ($result === null) {
250+
if (defined('JSON_ERROR_UTF8') && JSON_ERROR_UTF8 === json_last_error()) {
251+
$this->addResponse('Json is not UTF-8, could not parse json data.', 1);
252+
253+
return;
254+
}
255+
}
256+
257+
$this->addResponse($e->getDetails(), 1);
258+
259+
throw $e;
260+
}
261+
262+
if (isset($data['returnJson']) && $data['returnJson'] === 'array') {
263+
$data['json'] = Json::decode($data['json'], true);
264+
} else if (isset($data['returnJson']) && $data['returnJson'] === 'formatted') {
265+
$data['json'] = $this->formatJson($data);
266+
}
267+
268+
$this->addResponse('Success', 0, ['json' => $data['json']]);
269+
270+
return $data['json'];
271+
}
272+
273+
public function formatJson($data)
274+
{
275+
if (!isset($data['json'])) {
276+
$this->addResponse('Json data not provided.', 1);
277+
278+
return;
279+
}
280+
281+
if (is_array($data['json'])) {
282+
$data['json'] = Json::encode($data['json'], JSON_UNESCAPED_SLASHES);
283+
}
284+
285+
$return = "\n";
286+
$indent = "\t";
287+
$formatted_json = '';
288+
$quotes = false;
289+
$arrayLevel = 0;
290+
291+
for ($i = 0; $i < strlen($data['json']); $i++) {
292+
$prefix = '';
293+
$suffix = '';
294+
295+
switch ($data['json'][$i]) {
296+
case '"':
297+
$quotes = !$quotes;
298+
break;
299+
300+
case '[':
301+
$arrayLevel++;
302+
break;
303+
304+
case ']':
305+
$arrayLevel--;
306+
$prefix = $return;
307+
$prefix .= str_repeat($indent, $arrayLevel);
308+
break;
309+
310+
case '{':
311+
$arrayLevel++;
312+
$suffix = $return;
313+
$suffix .= str_repeat($indent, $arrayLevel);
314+
break;
315+
316+
case ':':
317+
$suffix = ' ';
318+
break;
319+
320+
case ',':
321+
if (!$quotes) {
322+
$suffix = $return;
323+
$suffix .= str_repeat($indent, $arrayLevel);
324+
}
325+
break;
326+
327+
case '}':
328+
$arrayLevel--;
329+
330+
case ']':
331+
$prefix = $return;
332+
$prefix .= str_repeat($indent, $arrayLevel);
333+
break;
334+
}
335+
336+
$formatted_json .= $prefix.$data['json'][$i].$suffix;
337+
}
338+
339+
$this->addResponse('Success', 0, ['formatted_json' => $formatted_json]);
340+
341+
return $formatted_json;
342+
}
231343
}

0 commit comments

Comments
 (0)