<?php
namespace AWCmsBundle\Templating;
/**
* {@inheritdoc}
*/
class FilesystemLoader extends \Symfony\Bundle\TwigBundle\Loader\FilesystemLoader
{
/**
* {@inheritdoc}
*/
protected function findTemplate($template, $throw = true)
{
$customTemplate = $this->getFormatedTemplate($template);
$twigTemplate = parent::findTemplate($customTemplate, false);
if (false !== $twigTemplate) {
return $twigTemplate;
}
return parent::findTemplate($template, $throw);
}
/**
* Get the formated template.
*
* @param string $template The original template
*
* @return string The template with a normal path
*/
protected function getFormatedTemplate($template)
{
$customTemplate = (string) $template;
if ('::' === substr($customTemplate, 0, 2)) {
$customTemplate = substr($customTemplate, 2);
}
$customTemplate = str_replace(':', DIRECTORY_SEPARATOR, $customTemplate);
if (0 === strpos($template, '@')) {
$customTemplate = substr($customTemplate, 1);
$templateExplode = explode(DIRECTORY_SEPARATOR, $customTemplate);
// Add Bundle in path if not
if ('bundle' !== strtolower(substr($templateExplode[0], -6))) {
$separatorPosition = strpos($customTemplate, DIRECTORY_SEPARATOR);
$customTemplate = substr($customTemplate, 0, $separatorPosition).'Bundle'.substr($customTemplate, $separatorPosition);
}
}
return $customTemplate;
}
}