Skip to content

Commit

Permalink
refactor DLTemplate for blade
Browse files Browse the repository at this point in the history
  • Loading branch information
Pathologic committed Aug 25, 2019
1 parent 1b4f1b6 commit e9c3abc
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 33 deletions.
2 changes: 1 addition & 1 deletion 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)
Expand Down
70 changes: 39 additions & 31 deletions assets/snippets/DocLister/lib/DLTemplate.class.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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');
}
Expand All @@ -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);
Expand Down Expand Up @@ -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)) {
Expand All @@ -429,7 +458,7 @@ public function parseChunk($name, $data = array(), $parseDocumentSource = false,
}
break;
}
if ($parseDocumentSource && !$twig) {
if ($parseDocumentSource && !$twig && !$blade) {
$out = $this->parseDocumentSource($out);
}

Expand Down Expand Up @@ -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;
}

/**
Expand Down
6 changes: 5 additions & 1 deletion assets/snippets/DocLister/snippet.DocLister.php
Expand Up @@ -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();
Expand All @@ -63,5 +66,6 @@
$modx->setPlaceholder($saveDLObject, $DocLister);
}
}
$DLTemplate->setTemplatePath($templatePath)->setTemplateExtension($templateExtension);

return $out;

0 comments on commit e9c3abc

Please sign in to comment.