2012-04-02 17:28:35 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Symfony\Bridge\Twig;
|
|
|
|
|
|
|
|
use Symfony\Component\Templating\EngineInterface;
|
|
|
|
use Symfony\Component\Templating\StreamingEngineInterface;
|
|
|
|
use Symfony\Component\Templating\TemplateNameParserInterface;
|
2013-09-04 13:01:22 +01:00
|
|
|
use Symfony\Component\Templating\TemplateReferenceInterface;
|
2012-04-02 17:28:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This engine knows how to render Twig templates.
|
|
|
|
*
|
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
|
|
|
*/
|
|
|
|
class TwigEngine implements EngineInterface, StreamingEngineInterface
|
|
|
|
{
|
|
|
|
protected $environment;
|
|
|
|
protected $parser;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* @param \Twig_Environment $environment A \Twig_Environment instance
|
|
|
|
* @param TemplateNameParserInterface $parser A TemplateNameParserInterface instance
|
|
|
|
*/
|
|
|
|
public function __construct(\Twig_Environment $environment, TemplateNameParserInterface $parser)
|
|
|
|
{
|
|
|
|
$this->environment = $environment;
|
|
|
|
$this->parser = $parser;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-09-04 13:01:22 +01:00
|
|
|
* {@inheritdoc}
|
2012-04-02 17:28:35 +01:00
|
|
|
*
|
2013-09-04 13:01:22 +01:00
|
|
|
* It also supports \Twig_Template as name parameter.
|
2012-04-02 17:28:35 +01:00
|
|
|
*
|
2013-09-04 13:01:22 +01:00
|
|
|
* @throws \Twig_Error if something went wrong like a thrown exception while rendering the template
|
2012-04-02 17:28:35 +01:00
|
|
|
*/
|
|
|
|
public function render($name, array $parameters = array())
|
|
|
|
{
|
|
|
|
return $this->load($name)->render($parameters);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-09-04 13:01:22 +01:00
|
|
|
* {@inheritdoc}
|
2012-04-02 17:28:35 +01:00
|
|
|
*
|
2013-09-04 13:01:22 +01:00
|
|
|
* It also supports \Twig_Template as name parameter.
|
2012-04-02 17:28:35 +01:00
|
|
|
*
|
2013-09-04 13:01:22 +01:00
|
|
|
* @throws \Twig_Error if something went wrong like a thrown exception while rendering the template
|
2012-04-02 17:28:35 +01:00
|
|
|
*/
|
|
|
|
public function stream($name, array $parameters = array())
|
|
|
|
{
|
|
|
|
$this->load($name)->display($parameters);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-09-04 13:01:22 +01:00
|
|
|
* {@inheritdoc}
|
2012-04-02 17:28:35 +01:00
|
|
|
*
|
2013-09-04 13:01:22 +01:00
|
|
|
* It also supports \Twig_Template as name parameter.
|
2012-04-02 17:28:35 +01:00
|
|
|
*/
|
|
|
|
public function exists($name)
|
|
|
|
{
|
2013-07-23 13:30:22 +01:00
|
|
|
if ($name instanceof \Twig_Template) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$loader = $this->environment->getLoader();
|
|
|
|
|
2016-11-07 18:42:43 +00:00
|
|
|
if ($loader instanceof \Twig_ExistsLoaderInterface || method_exists($loader, 'exists')) {
|
2013-09-04 13:01:22 +01:00
|
|
|
return $loader->exists((string) $name);
|
2013-07-23 13:30:22 +01:00
|
|
|
}
|
|
|
|
|
2012-04-02 17:28:35 +01:00
|
|
|
try {
|
2013-09-04 13:01:22 +01:00
|
|
|
// cast possible TemplateReferenceInterface to string because the
|
|
|
|
// EngineInterface supports them but Twig_LoaderInterface does not
|
2016-11-07 18:42:43 +00:00
|
|
|
$loader->getSourceContext((string) $name)->getCode();
|
2013-07-23 13:30:22 +01:00
|
|
|
} catch (\Twig_Error_Loader $e) {
|
2012-04-02 17:28:35 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-09-04 13:01:22 +01:00
|
|
|
* {@inheritdoc}
|
2012-04-02 17:28:35 +01:00
|
|
|
*
|
2013-09-04 13:01:22 +01:00
|
|
|
* It also supports \Twig_Template as name parameter.
|
2012-04-02 17:28:35 +01:00
|
|
|
*/
|
|
|
|
public function supports($name)
|
|
|
|
{
|
|
|
|
if ($name instanceof \Twig_Template) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$template = $this->parser->parse($name);
|
|
|
|
|
|
|
|
return 'twig' === $template->get('engine');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads the given template.
|
|
|
|
*
|
2013-09-04 13:01:22 +01:00
|
|
|
* @param string|TemplateReferenceInterface|\Twig_Template $name A template name or an instance of
|
|
|
|
* TemplateReferenceInterface or \Twig_Template
|
2012-04-02 17:28:35 +01:00
|
|
|
*
|
2017-02-28 12:44:06 +00:00
|
|
|
* @return \Twig_Template A \Twig_Template instance
|
2012-04-02 17:28:35 +01:00
|
|
|
*
|
|
|
|
* @throws \InvalidArgumentException if the template does not exist
|
|
|
|
*/
|
|
|
|
protected function load($name)
|
|
|
|
{
|
|
|
|
if ($name instanceof \Twig_Template) {
|
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2013-09-04 13:01:22 +01:00
|
|
|
return $this->environment->loadTemplate((string) $name);
|
2012-04-02 17:28:35 +01:00
|
|
|
} catch (\Twig_Error_Loader $e) {
|
|
|
|
throw new \InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|