parent
04d54089f8
commit
8724566849
|
|
@ -3,6 +3,8 @@
|
|||
use Event;
|
||||
use Twig_LoaderInterface;
|
||||
use Cms\Contracts\CmsObject;
|
||||
use System\Twig\Loader as LoaderBase;
|
||||
use Cms\Classes\Partial as CmsPartial;
|
||||
|
||||
/**
|
||||
* This class implements a Twig template loader for the CMS.
|
||||
|
|
@ -10,13 +12,18 @@ use Cms\Contracts\CmsObject;
|
|||
* @package october\cms
|
||||
* @author Alexey Bobkov, Samuel Georges
|
||||
*/
|
||||
class Loader implements Twig_LoaderInterface
|
||||
class Loader extends LoaderBase implements Twig_LoaderInterface
|
||||
{
|
||||
/**
|
||||
* @var \Cms\Classes\CmsCompoundObject A CMS object to load the template from.
|
||||
*/
|
||||
protected $obj;
|
||||
|
||||
/**
|
||||
* @var array Cache
|
||||
*/
|
||||
protected $fallbackCache = [];
|
||||
|
||||
/**
|
||||
* Sets a CMS object to load the template from.
|
||||
* @param \Cms\Contracts\CmsObject $obj Specifies the CMS object.
|
||||
|
|
@ -32,6 +39,10 @@ class Loader implements Twig_LoaderInterface
|
|||
*/
|
||||
public function getSource($name)
|
||||
{
|
||||
if (!$this->validateCmsObject($name)) {
|
||||
return parent::getSource($name);
|
||||
}
|
||||
|
||||
$content = $this->obj->getTwigContent();
|
||||
|
||||
/*
|
||||
|
|
@ -49,6 +60,10 @@ class Loader implements Twig_LoaderInterface
|
|||
*/
|
||||
public function getCacheKey($name)
|
||||
{
|
||||
if (!$this->validateCmsObject($name)) {
|
||||
return parent::getCacheKey($name);
|
||||
}
|
||||
|
||||
return $this->obj->getTwigCacheKey();
|
||||
}
|
||||
|
||||
|
|
@ -57,6 +72,71 @@ class Loader implements Twig_LoaderInterface
|
|||
*/
|
||||
public function isFresh($name, $time)
|
||||
{
|
||||
if (!$this->validateCmsObject($name)) {
|
||||
return parent::isFresh($name, $time);
|
||||
}
|
||||
|
||||
return $this->obj->mtime <= $time;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the file name of the loaded template.
|
||||
*/
|
||||
public function getFilename($name)
|
||||
{
|
||||
if (!$this->validateCmsObject($name)) {
|
||||
return parent::getFilename($name);
|
||||
}
|
||||
|
||||
return $this->obj->getFilePath();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks that the template exists.
|
||||
*/
|
||||
public function exists($name)
|
||||
{
|
||||
if (!$this->validateCmsObject($name)) {
|
||||
return parent::exists($name);
|
||||
}
|
||||
|
||||
return $this->obj->exists;
|
||||
}
|
||||
|
||||
/**
|
||||
* Internal method that checks if the template name matches
|
||||
* the loaded object, with fallback support to partials.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateCmsObject($name)
|
||||
{
|
||||
if ($name == $this->obj->getFilePath()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($fallbackObj = $this->findFallbackObject($name)) {
|
||||
$this->obj = $fallbackObj;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Looks up a fallback CMS partial object.
|
||||
* @return Cms\Classes\Partial
|
||||
*/
|
||||
protected function findFallbackObject($name)
|
||||
{
|
||||
if (strpos($name, '::') !== false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (array_key_exists($name, $this->fallbackCache)) {
|
||||
return $this->fallbackCache[$name];
|
||||
}
|
||||
|
||||
return $this->fallbackCache[$name] = CmsPartial::find($name);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue