Skip to content

Commit 7dfd671

Browse files
committed
Support for comments in json files when rendering json templates
1 parent fe040dc commit 7dfd671

File tree

3 files changed

+43
-20
lines changed

3 files changed

+43
-20
lines changed

system/component/component.php

+18-12
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
namespace Vvveb\System\Component;
2424

2525
use function Vvveb\dashesToCamelCase;
26+
use function Vvveb\removeJsonComments;
2627
use Vvveb\System\Cache;
2728
use Vvveb\System\Core\View;
2829

@@ -333,28 +334,33 @@ function generateRequiredComponents() {
333334
$extension = strtolower(trim(substr($template, -4), '.'));
334335
$this->documentType = $extension;
335336

336-
if (strpos($template, 'plugins/') === 0) {
337-
$template = str_replace('plugins/', '', $template);
338-
$p = strpos($template, '/');
339-
$pluginName = substr($template, 0, $p);
340-
$nameSpace = substr($template, $p + 1);
341-
$app = '';
337+
if ($template[0] == '/') {
338+
} else {
339+
if (strpos($template, 'plugins/') === 0) {
340+
$template = str_replace('plugins/', '', $template);
341+
$p = strpos($template, '/');
342+
$pluginName = substr($template, 0, $p);
343+
$nameSpace = substr($template, $p + 1);
344+
$app = '';
342345

343-
if (APP != 'app') {
344-
$app = APP . DS;
346+
if (APP != 'app') {
347+
$app = APP . DS;
348+
}
349+
$template = DIR_PLUGINS . $pluginName . DS . 'public' . DS . $app . $nameSpace;
350+
} else {
351+
$template = $view->getTemplatePath() . $template;
345352
}
346-
$template = DIR_PLUGINS . $pluginName . DS . 'public' . DS . $app . $nameSpace;
347-
} else {
348-
$template = $view->getTemplatePath() . $template;
349353
}
350354

351355
if ($this->documentType == 'html') {
352356
@$document->loadHTMLFile($template,
353357
LIBXML_NOWARNING | LIBXML_NOERROR);
354358
} else {
355359
$content = file_get_contents($template);
356-
360+
357361
if ($this->documentType == 'json') {
362+
//remove json comments from line start
363+
$content = removeJsonComments($content);
358364
$json = json_decode($content, true);
359365
$json = \Vvveb\prepareJson($json);
360366
$xml = \Vvveb\array2xml($json);

system/core/frontcontroller.php

+12-3
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ static function notFound($service = false, $message = false, $statusCode = 404)
139139
*/
140140
//header(' ', true, $statusCode);
141141
$response = Response::getInstance();
142-
$view->setType($response->getType('json'));
142+
$view->setType($response->getType());
143143

144144
//return $response->output();
145145
PageCache::getInstance()->cleanUp();
@@ -216,11 +216,20 @@ static function call($controllerClass, $actionName, $file = false) {
216216
$template = str_replace('/', DS, strtolower(self :: $moduleName));
217217
$theme = $controller->view->getTheme();
218218
$path = DIR_THEME . $theme . DS;
219+
$pluginName = false;
219220

220221
if ($actionName && $actionName != 'index') {
221222
$html = $path . $template . DS . strtolower($actionName) . '.html';
222223

223-
if (file_exists($html)) {
224+
if (is_file($html)) {
225+
if ($pluginName) {
226+
$template .= 'plugins' . DS . $nameSpace . DS . strtolower($actionName);
227+
} else {
228+
$template .= DS . strtolower($actionName);
229+
}
230+
}
231+
232+
if ($pluginName) {
224233
$template .= DS . strtolower($actionName);
225234
}
226235
}
@@ -246,7 +255,7 @@ static function call($controllerClass, $actionName, $file = false) {
246255
//return $controller->view->render();
247256
$response = Response::getInstance();
248257

249-
$controller->view->setType($response->getType('json'));
258+
$controller->view->setType($response->getType());
250259

251260
$return = $response->output();
252261
self :: closeConnections();

system/vtpl/vtpl.php

+13-5
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,9 @@ private function processVariables() {
309309
preg_match_all('/(?<!["\'\[])(\$.+)/', $this->template, $variables);
310310

311311
//$variables[0] = array_values($variables[0]);
312-
313-
$variables[1] = array_unique($variables[1]);
312+
$patternsVariables = [];
313+
$placeholdersVariables = [];
314+
$variables[1] = array_unique($variables[1]);
314315
//usort($variables[1],fn($a,$b) => strlen($b) <=> strlen($a));
315316
usort($variables[1],function ($a,$b) {return strlen($b) <=> strlen($a); });
316317

@@ -864,6 +865,7 @@ private function processAttributeFilters($value, &$node) {
864865
}
865866
}
866867
}
868+
867869
//if ($class && preg_match_all('@filter_([^ :$]+)(:\'[^\']+\'|:[^ $]+)*@', $class, $matches, PREG_SET_ORDER) > 0)
868870
if ($filters) {
869871
$chain = '_$variable';
@@ -1790,6 +1792,7 @@ function loadHtmlTemplate($filename) {
17901792

17911793
return false;
17921794
}
1795+
17931796
$this->htmlSourceFile = $filename;
17941797

17951798
$this->debug->log('LOAD', $filename);
@@ -1830,6 +1833,8 @@ function loadHtmlTemplate($filename) {
18301833
} else {
18311834
//convert json
18321835
if ($extension == 'json') {
1836+
//remove json comments from line start
1837+
$html = Vvveb\removeJsonComments($html);
18331838
$json = json_decode($html, true);
18341839
$json = Vvveb\prepareJson($json);
18351840
$xml = Vvveb\array2xml($json);
@@ -1959,9 +1964,12 @@ function ($matches) {
19591964
$text = \Vvveb\stripExtraSpaces($text);
19601965
$trimmed = trim($text);
19611966

1962-
if (strlen($trimmed) < 2 ||
1963-
(substr_compare($trimmed, '{$', 0, 2) == 0) ||
1964-
(substr_compare($trimmed, 'http', 0, 4) == 0) ||
1967+
//skip untranslatable text
1968+
if (strlen($trimmed) < 2 || //too small
1969+
(substr_compare($trimmed, '{$', 0, 2) == 0) || //starts with variable
1970+
(substr_compare($trimmed, 'http', 0, 4) == 0) || //is url
1971+
(strpos($trimmed, '{$') !== false) || //string has variable
1972+
(strpos($trimmed, '<?php') !== false) || //string has php code
19651973
(is_numeric($trimmed[0]))
19661974
) {
19671975
continue;

0 commit comments

Comments
 (0)