diff --git a/HISTORY.md b/HISTORY.md index 307d9e2c..4d1b81d2 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,6 +1,6 @@ ## 2.5.0 (19.08.2019) * [Add] Изменение работы idType=parents в onetable для связанных таблиц (Issue #349) -* [Refactor] Переделана работа с шаблонами Twig в DLTemplate. +* [Refactor] Переделана работа с шаблонами Twig и Blade в DLTemplate. * [Add] Получение групп пользователей в экстендере user. * [Refactor] Оптимизация картинок в Helpers/PHPThumb работает только при наличии библиотеки spatie/image-optimizer (Issue #299) * [Fix] Ошибка подсчета [+from+] && [+to+] при пустом результате (Issue #344) diff --git a/assets/snippets/DocLister/lib/DLTemplate.class.php b/assets/snippets/DocLister/lib/DLTemplate.class.php index 3f1cc8e6..adb242c4 100644 --- a/assets/snippets/DocLister/lib/DLTemplate.class.php +++ b/assets/snippets/DocLister/lib/DLTemplate.class.php @@ -24,9 +24,13 @@ class DLTemplate protected $templateExtension = 'html'; /** - * @var null|Twig_Environment twig object + * @var null|Twig_Environment */ protected $twig; + /* + * @var Illuminate\View\Factory + */ + protected $blade; protected $twigEnabled = false; protected $bladeEnabled = false; @@ -101,6 +105,11 @@ public function setTemplatePath($path, $supRoot = false) if ($this->twigEnabled) { $this->twig->setLoader(new Twig_Loader_Filesystem(MODX_BASE_PATH . $path)); } + if ($this->bladeEnabled) { + $filesystem = new Illuminate\Filesystem\Filesystem; + $viewFinder = new Illuminate\View\FileViewFinder($filesystem, [MODX_BASE_PATH . $path]); + $this->blade->setFinder($viewFinder); + } } return $this; @@ -205,7 +214,6 @@ public function getChunk($name) ) && isset($tmp[2], $tmp[3])) ? $tmp[2] : false; $subTmp = (isset($tmp[3])) ? trim($tmp[3]) : null; if ($this->bladeEnabled) { - $mode = '@' . substr($mode, 3); $ext = $this->getTemplateExtension(); $this->setTemplateExtension('blade.php'); } @@ -228,6 +236,26 @@ public function getChunk($name) $tpl = $this->twig->createTemplate($tpl); } break; + case '@B_FILE': + if ($subTmp != '' && $this->bladeEnabled) { + $real = realpath(MODX_BASE_PATH . $this->templatePath); + $path = realpath(MODX_BASE_PATH . $this->templatePath . $this->cleanPath($subTmp) . '.' . $this->templateExtension); + if (basename($path, '.' . $this->templateExtension) !== '' && + 0 === strpos($path, $real) && + file_exists($path) + ) { + $tpl = $this->cleanPath($subTmp); + } + } + break; + case '@B_CODE': + $cache = md5($name). '-'. sha1($subTmp); + $path = MODX_BASE_PATH . '/assets/cache/blade/' . $cache . '.blade.php'; + if (! file_exists($path)) { + file_put_contents($path, $tpl); + } + $tpl = 'cache::' . $cache; + break; case '@FILE': if ($subTmp != '') { $real = realpath(MODX_BASE_PATH . $this->templatePath); @@ -406,13 +434,14 @@ public function getTemplate($id) public function parseChunk($name, $data = array(), $parseDocumentSource = false, $disablePHx = false) { $out = $this->getChunk($name); - $twig = strpos($name, '@T_') === 0; + $twig = strpos($name, '@T_') === 0 && $this->twigEnabled; + $blade = strpos($name, '@B_') === 0 && $this->bladeEnabled; switch (true) { case $twig: $out = $out->render($this->getTemplateData($data)); break; - case $this->bladeEnabled && $out !== '' && ($blade = $this->getBlade($name, $out)): - $out = $blade->with($this->getTemplateData($data))->render(); + case $blade: + $out = $this->blade->make($out)->with($this->getTemplateData($data))->render(); break; case is_array($data) && ($out != ''): if (preg_match("/\[\+[A-Z0-9\.\_\-]+\+\]/is", $out)) { @@ -429,7 +458,7 @@ public function parseChunk($name, $data = array(), $parseDocumentSource = false, } break; } - if ($parseDocumentSource && !$twig) { + if ($parseDocumentSource && !$twig && !$blade) { $out = $this->parseDocumentSource($out); } @@ -462,32 +491,11 @@ public function loadTwig() { } } - /** - * Return clone of blade - * - * @param string $name - * @param string $tpl - * @return Illuminate\View\Factory - */ - protected function getBlade($name, $tpl) - { - $out = null; - try { - /** - * Illuminate\View\Factory $blade - */ - $blade = $this->modx->blade; - $cache = md5($name). '-'. sha1($tpl); - $path = MODX_BASE_PATH . '/assets/cache/blade/' . $cache . '.blade.php'; - if (! file_exists($path)) { - file_put_contents($path, $tpl); - } - $out = $blade->make('cache::' . $cache); - } catch (\Exception $exception) { - $this->modx->messageQuit($exception->getMessage()); + public function loadBlade() { + if (is_null($this->blade) && isset($this->modx->blade)) { + $this->blade = clone $this->modx->blade; + $this->bladeEnabled = true; } - - return $out; } /** diff --git a/assets/snippets/DocLister/snippet.DocLister.php b/assets/snippets/DocLister/snippet.DocLister.php index 2cfe564c..34918743 100644 --- a/assets/snippets/DocLister/snippet.DocLister.php +++ b/assets/snippets/DocLister/snippet.DocLister.php @@ -34,9 +34,12 @@ } } +$DLTemplate = DLTemplate::getInstance($modx); +$templatePath = $DLTemplate->getTemplatePath(); +$templateExtension = $DLTemplate->getTemplateExtension(); if (class_exists($class) && is_subclass_of($class, '\\DocLister', true)) { $DocLister = new $class($modx, $modx->Event->params, $_time); - if ($DocLister->getCFGDef('returnDLObject')) { + if ($DocLister->getCFGDef('returnObject')) { return $DocLister; } $data = $DocLister->getDocs(); @@ -63,5 +66,6 @@ $modx->setPlaceholder($saveDLObject, $DocLister); } } +$DLTemplate->setTemplatePath($templatePath)->setTemplateExtension($templateExtension); return $out;