vendor/symfony/templating/DelegatingEngine.php line 101

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Templating;
  11. /**
  12.  * DelegatingEngine selects an engine for a given template.
  13.  *
  14.  * @author Fabien Potencier <fabien@symfony.com>
  15.  */
  16. class DelegatingEngine implements EngineInterfaceStreamingEngineInterface
  17. {
  18.     /**
  19.      * @var EngineInterface[]
  20.      */
  21.     protected $engines = [];
  22.     /**
  23.      * @param EngineInterface[] $engines An array of EngineInterface instances to add
  24.      */
  25.     public function __construct(array $engines = [])
  26.     {
  27.         foreach ($engines as $engine) {
  28.             $this->addEngine($engine);
  29.         }
  30.     }
  31.     /**
  32.      * {@inheritdoc}
  33.      */
  34.     public function render($name, array $parameters = [])
  35.     {
  36.         return $this->getEngine($name)->render($name$parameters);
  37.     }
  38.     /**
  39.      * {@inheritdoc}
  40.      */
  41.     public function stream($name, array $parameters = [])
  42.     {
  43.         $engine $this->getEngine($name);
  44.         if (!$engine instanceof StreamingEngineInterface) {
  45.             throw new \LogicException(sprintf('Template "%s" cannot be streamed as the engine supporting it does not implement StreamingEngineInterface.'$name));
  46.         }
  47.         $engine->stream($name$parameters);
  48.     }
  49.     /**
  50.      * {@inheritdoc}
  51.      */
  52.     public function exists($name)
  53.     {
  54.         return $this->getEngine($name)->exists($name);
  55.     }
  56.     public function addEngine(EngineInterface $engine)
  57.     {
  58.         $this->engines[] = $engine;
  59.     }
  60.     /**
  61.      * {@inheritdoc}
  62.      */
  63.     public function supports($name)
  64.     {
  65.         try {
  66.             $this->getEngine($name);
  67.         } catch (\RuntimeException $e) {
  68.             return false;
  69.         }
  70.         return true;
  71.     }
  72.     /**
  73.      * Get an engine able to render the given template.
  74.      *
  75.      * @param string|TemplateReferenceInterface $name A template name or a TemplateReferenceInterface instance
  76.      *
  77.      * @return EngineInterface The engine
  78.      *
  79.      * @throws \RuntimeException if no engine able to work with the template is found
  80.      */
  81.     public function getEngine($name)
  82.     {
  83.         foreach ($this->engines as $engine) {
  84.             if ($engine->supports($name)) {
  85.                 return $engine;
  86.             }
  87.         }
  88.         throw new \RuntimeException(sprintf('No engine is able to work with the template "%s".'$name));
  89.     }
  90. }