vendor/symfony/src/Symfony/Component/Templating/DelegatingEngine.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * This file is part of the Symfony package.
       
     5  *
       
     6  * (c) Fabien Potencier <fabien@symfony.com>
       
     7  *
       
     8  * For the full copyright and license information, please view the LICENSE
       
     9  * file that was distributed with this source code.
       
    10  */
       
    11 
       
    12 namespace Symfony\Component\Templating;
       
    13 
       
    14 /**
       
    15  * DelegatingEngine selects an engine for a given template.
       
    16  *
       
    17  * @author Fabien Potencier <fabien@symfony.com>
       
    18  *
       
    19  * @api
       
    20  */
       
    21 class DelegatingEngine implements EngineInterface
       
    22 {
       
    23     protected $engines;
       
    24 
       
    25     /**
       
    26      * Constructor.
       
    27      *
       
    28      * @param array $engines An array of EngineInterface instances to add
       
    29      *
       
    30      * @api
       
    31      */
       
    32     public function __construct(array $engines = array())
       
    33     {
       
    34         $this->engines = array();
       
    35         foreach ($engines as $engine) {
       
    36             $this->addEngine($engine);
       
    37         }
       
    38     }
       
    39 
       
    40     /**
       
    41      * Renders a template.
       
    42      *
       
    43      * @param mixed $name       A template name or a TemplateReferenceInterface instance
       
    44      * @param array $parameters An array of parameters to pass to the template
       
    45      *
       
    46      * @return string The evaluated template as a string
       
    47      *
       
    48      * @throws \InvalidArgumentException if the template does not exist
       
    49      * @throws \RuntimeException         if the template cannot be rendered
       
    50      *
       
    51      * @api
       
    52      */
       
    53     public function render($name, array $parameters = array())
       
    54     {
       
    55         return $this->getEngine($name)->render($name, $parameters);
       
    56     }
       
    57 
       
    58     /**
       
    59      * Returns true if the template exists.
       
    60      *
       
    61      * @param mixed $name A template name or a TemplateReferenceInterface instance
       
    62      *
       
    63      * @return Boolean true if the template exists, false otherwise
       
    64      *
       
    65      * @api
       
    66      */
       
    67     public function exists($name)
       
    68     {
       
    69         return $this->getEngine($name)->exists($name);
       
    70     }
       
    71 
       
    72     /**
       
    73      * Adds an engine.
       
    74      *
       
    75      * @param EngineInterface $engine An EngineInterface instance
       
    76      *
       
    77      * @api
       
    78      */
       
    79     public function addEngine(EngineInterface $engine)
       
    80     {
       
    81         $this->engines[] = $engine;
       
    82     }
       
    83 
       
    84     /**
       
    85      * Returns true if this class is able to render the given template.
       
    86      *
       
    87      * @param mixed $name A template name or a TemplateReferenceInterface instance
       
    88      *
       
    89      * @return Boolean true if this class supports the given template, false otherwise
       
    90      *
       
    91      * @api
       
    92      */
       
    93     public function supports($name)
       
    94     {
       
    95         try {
       
    96             $this->getEngine($name);
       
    97         } catch (\RuntimeException $e) {
       
    98             return false;
       
    99         }
       
   100 
       
   101         return true;
       
   102     }
       
   103 
       
   104     /**
       
   105      * Get an engine able to render the given template.
       
   106      *
       
   107      * @param mixed $name A template name or a TemplateReferenceInterface instance
       
   108      *
       
   109      * @return EngineInterface The engine
       
   110      *
       
   111      * @throws \RuntimeException if no engine able to work with the template is found
       
   112      *
       
   113      * @api
       
   114      */
       
   115     protected function getEngine($name)
       
   116     {
       
   117         foreach ($this->engines as $engine) {
       
   118             if ($engine->supports($name)) {
       
   119                 return $engine;
       
   120             }
       
   121         }
       
   122 
       
   123         throw new \RuntimeException(sprintf('No engine is able to work with the template "%s".', $name));
       
   124     }
       
   125 }