From dbab7e1ef6553adb57647e5309a33dcf9272ddc6 Mon Sep 17 00:00:00 2001 From: Christophe Coevoet Date: Mon, 2 Apr 2012 18:28:35 +0200 Subject: [PATCH] [TwigBridge] Added a TwigEngine in the bridge This TwigEngine implements the interface available in the component. the TwigBridge in TwigBundle now extends this class and provides only the additional methods for the FrameworkBundle interface. --- src/Symfony/Bridge/Twig/TwigEngine.php | 127 +++++++++++++++++++ src/Symfony/Bundle/TwigBundle/TwigEngine.php | 83 +----------- 2 files changed, 132 insertions(+), 78 deletions(-) create mode 100644 src/Symfony/Bridge/Twig/TwigEngine.php diff --git a/src/Symfony/Bridge/Twig/TwigEngine.php b/src/Symfony/Bridge/Twig/TwigEngine.php new file mode 100644 index 000000000000..5e57b211b2ac --- /dev/null +++ b/src/Symfony/Bridge/Twig/TwigEngine.php @@ -0,0 +1,127 @@ + + * + * 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\HttpFoundation\Response; +use Symfony\Component\Templating\EngineInterface; +use Symfony\Component\Templating\StreamingEngineInterface; +use Symfony\Component\Templating\TemplateNameParserInterface; + +/** + * This engine knows how to render Twig templates. + * + * @author Fabien Potencier + */ +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; + } + + /** + * Renders a template. + * + * @param mixed $name A template name + * @param array $parameters An array of parameters to pass to the template + * + * @return string The evaluated template as a string + * + * @throws \InvalidArgumentException if the template does not exist + * @throws \RuntimeException if the template cannot be rendered + */ + public function render($name, array $parameters = array()) + { + return $this->load($name)->render($parameters); + } + + /** + * Streams a template. + * + * @param mixed $name A template name or a TemplateReferenceInterface instance + * @param array $parameters An array of parameters to pass to the template + * + * @throws \RuntimeException if the template cannot be rendered + */ + public function stream($name, array $parameters = array()) + { + $this->load($name)->display($parameters); + } + + /** + * Returns true if the template exists. + * + * @param mixed $name A template name + * + * @return Boolean true if the template exists, false otherwise + */ + public function exists($name) + { + try { + $this->load($name); + } catch (\InvalidArgumentException $e) { + return false; + } + + return true; + } + + /** + * Returns true if this class is able to render the given template. + * + * @param string $name A template name + * + * @return Boolean True if this class supports the given resource, false otherwise + */ + 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. + * + * @param mixed $name A template name or an instance of Twig_Template + * + * @return \Twig_TemplateInterface A \Twig_TemplateInterface instance + * + * @throws \InvalidArgumentException if the template does not exist + */ + protected function load($name) + { + if ($name instanceof \Twig_Template) { + return $name; + } + + try { + return $this->environment->loadTemplate($name); + } catch (\Twig_Error_Loader $e) { + throw new \InvalidArgumentException($e->getMessage(), $e->getCode(), $e); + } + } +} diff --git a/src/Symfony/Bundle/TwigBundle/TwigEngine.php b/src/Symfony/Bundle/TwigBundle/TwigEngine.php index ef3482fa98e2..99127bd33c62 100644 --- a/src/Symfony/Bundle/TwigBundle/TwigEngine.php +++ b/src/Symfony/Bundle/TwigBundle/TwigEngine.php @@ -11,23 +11,21 @@ namespace Symfony\Bundle\TwigBundle; +use Symfony\Bridge\Twig\TwigEngine as BaseEngine; use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; use Symfony\Bundle\FrameworkBundle\Templating\GlobalVariables; use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference; use Symfony\Component\Templating\TemplateNameParserInterface; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Config\FileLocatorInterface; -use Symfony\Component\Templating\StreamingEngineInterface; /** * This engine knows how to render Twig templates. * * @author Fabien Potencier */ -class TwigEngine implements EngineInterface, StreamingEngineInterface +class TwigEngine extends BaseEngine implements EngineInterface { - protected $environment; - protected $parser; protected $locator; /** @@ -40,8 +38,8 @@ class TwigEngine implements EngineInterface, StreamingEngineInterface */ public function __construct(\Twig_Environment $environment, TemplateNameParserInterface $parser, FileLocatorInterface $locator, GlobalVariables $globals = null) { - $this->environment = $environment; - $this->parser = $parser; + parent::__construct($environment, $parser); + $this->locator = $locator; if (null !== $globals) { @@ -63,7 +61,7 @@ public function __construct(\Twig_Environment $environment, TemplateNameParserIn public function render($name, array $parameters = array()) { try { - return $this->load($name)->render($parameters); + return parent::render($name, $parameters); } catch (\Twig_Error $e) { if ($name instanceof TemplateReference) { try { @@ -77,55 +75,6 @@ public function render($name, array $parameters = array()) } } - /** - * Streams a template. - * - * @param mixed $name A template name or a TemplateReferenceInterface instance - * @param array $parameters An array of parameters to pass to the template - * - * @throws \RuntimeException if the template cannot be rendered - */ - public function stream($name, array $parameters = array()) - { - $this->load($name)->display($parameters); - } - - /** - * Returns true if the template exists. - * - * @param mixed $name A template name - * - * @return Boolean true if the template exists, false otherwise - */ - public function exists($name) - { - try { - $this->load($name); - } catch (\InvalidArgumentException $e) { - return false; - } - - return true; - } - - /** - * Returns true if this class is able to render the given template. - * - * @param string $name A template name - * - * @return Boolean True if this class supports the given resource, false otherwise - */ - public function supports($name) - { - if ($name instanceof \Twig_Template) { - return true; - } - - $template = $this->parser->parse($name); - - return 'twig' === $template->get('engine'); - } - /** * Renders a view and returns a Response. * @@ -145,26 +94,4 @@ public function renderResponse($view, array $parameters = array(), Response $res return $response; } - - /** - * Loads the given template. - * - * @param mixed $name A template name or an instance of Twig_Template - * - * @return \Twig_TemplateInterface A \Twig_TemplateInterface instance - * - * @throws \InvalidArgumentException if the template does not exist - */ - protected function load($name) - { - if ($name instanceof \Twig_Template) { - return $name; - } - - try { - return $this->environment->loadTemplate($name); - } catch (\Twig_Error_Loader $e) { - throw new \InvalidArgumentException($e->getMessage(), $e->getCode(), $e); - } - } }