2014-05-14 23:24:20 +10:00
|
|
|
<?php namespace Cms\Twig;
|
|
|
|
|
2015-06-05 17:43:32 +10:00
|
|
|
use Event;
|
2017-05-13 09:34:20 +10:00
|
|
|
use Twig_Source;
|
2014-05-14 23:24:20 +10:00
|
|
|
use Twig_LoaderInterface;
|
2016-03-09 20:51:04 +11:00
|
|
|
use Cms\Contracts\CmsObject;
|
2016-12-07 20:58:25 +11:00
|
|
|
use System\Twig\Loader as LoaderBase;
|
|
|
|
use Cms\Classes\Partial as CmsPartial;
|
2014-05-14 23:24:20 +10:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This class implements a Twig template loader for the CMS.
|
|
|
|
*
|
|
|
|
* @package october\cms
|
|
|
|
* @author Alexey Bobkov, Samuel Georges
|
|
|
|
*/
|
2016-12-07 20:58:25 +11:00
|
|
|
class Loader extends LoaderBase implements Twig_LoaderInterface
|
2014-05-14 23:24:20 +10:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \Cms\Classes\CmsCompoundObject A CMS object to load the template from.
|
|
|
|
*/
|
|
|
|
protected $obj;
|
|
|
|
|
2016-12-07 20:58:25 +11:00
|
|
|
/**
|
|
|
|
* @var array Cache
|
|
|
|
*/
|
|
|
|
protected $fallbackCache = [];
|
|
|
|
|
2014-05-14 23:24:20 +10:00
|
|
|
/**
|
|
|
|
* Sets a CMS object to load the template from.
|
2016-03-09 20:51:04 +11:00
|
|
|
* @param \Cms\Contracts\CmsObject $obj Specifies the CMS object.
|
2014-05-14 23:24:20 +10:00
|
|
|
*/
|
|
|
|
public function setObject(CmsObject $obj)
|
|
|
|
{
|
|
|
|
$this->obj = $obj;
|
|
|
|
}
|
|
|
|
|
2015-06-05 17:43:32 +10:00
|
|
|
/**
|
|
|
|
* Returns the Twig content string.
|
|
|
|
* This step is cached internally by Twig.
|
|
|
|
*/
|
2017-05-13 09:34:20 +10:00
|
|
|
public function getSourceContext($name)
|
2014-05-14 23:24:20 +10:00
|
|
|
{
|
2016-12-07 20:58:25 +11:00
|
|
|
if (!$this->validateCmsObject($name)) {
|
2017-05-13 09:34:20 +10:00
|
|
|
return parent::getSourceContext($name);
|
2016-12-07 20:58:25 +11:00
|
|
|
}
|
|
|
|
|
2015-06-05 17:43:32 +10:00
|
|
|
$content = $this->obj->getTwigContent();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Extensibility
|
|
|
|
*/
|
|
|
|
$dataHolder = (object) ['content' => $content];
|
|
|
|
|
|
|
|
Event::fire('cms.template.processTwigContent', [$this->obj, $dataHolder]);
|
|
|
|
|
2017-05-13 09:34:20 +10:00
|
|
|
return new Twig_Source($dataHolder->content, $name);
|
2014-05-14 23:24:20 +10:00
|
|
|
}
|
|
|
|
|
2015-06-05 17:43:32 +10:00
|
|
|
/**
|
|
|
|
* Returns the Twig cache key.
|
|
|
|
*/
|
2014-05-14 23:24:20 +10:00
|
|
|
public function getCacheKey($name)
|
|
|
|
{
|
2016-12-07 20:58:25 +11:00
|
|
|
if (!$this->validateCmsObject($name)) {
|
|
|
|
return parent::getCacheKey($name);
|
|
|
|
}
|
|
|
|
|
2016-03-21 17:51:14 +11:00
|
|
|
return $this->obj->getTwigCacheKey();
|
2014-05-14 23:24:20 +10:00
|
|
|
}
|
|
|
|
|
2015-06-05 17:43:32 +10:00
|
|
|
/**
|
|
|
|
* Determines if the content is fresh.
|
|
|
|
*/
|
2014-05-14 23:24:20 +10:00
|
|
|
public function isFresh($name, $time)
|
|
|
|
{
|
2016-12-07 20:58:25 +11:00
|
|
|
if (!$this->validateCmsObject($name)) {
|
|
|
|
return parent::isFresh($name, $time);
|
|
|
|
}
|
|
|
|
|
2016-01-02 13:41:57 +11:00
|
|
|
return $this->obj->mtime <= $time;
|
2014-05-14 23:24:20 +10:00
|
|
|
}
|
2016-12-07 20:58:25 +11:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
2014-10-11 01:42:04 +02:00
|
|
|
}
|