|
0
|
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\Bundle\FrameworkBundle\Templating; |
|
|
13 |
|
|
|
14 |
use Symfony\Component\Templating\DelegatingEngine as BaseDelegatingEngine; |
|
|
15 |
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
|
16 |
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; |
|
|
17 |
use Symfony\Component\HttpFoundation\Response; |
|
|
18 |
|
|
|
19 |
/** |
|
|
20 |
* DelegatingEngine selects an engine for a given template. |
|
|
21 |
* |
|
|
22 |
* @author Fabien Potencier <fabien@symfony.com> |
|
|
23 |
*/ |
|
|
24 |
class DelegatingEngine extends BaseDelegatingEngine implements EngineInterface |
|
|
25 |
{ |
|
|
26 |
protected $container; |
|
|
27 |
|
|
|
28 |
/** |
|
|
29 |
* Constructor. |
|
|
30 |
* |
|
|
31 |
* @param ContainerInterface $container The DI container |
|
|
32 |
* @param array $engineIds An array of engine Ids |
|
|
33 |
*/ |
|
|
34 |
public function __construct(ContainerInterface $container, array $engineIds) |
|
|
35 |
{ |
|
|
36 |
$this->container = $container; |
|
|
37 |
$this->engines = $engineIds; |
|
|
38 |
} |
|
|
39 |
|
|
|
40 |
/** |
|
|
41 |
* {@inheritdoc} |
|
|
42 |
*/ |
|
|
43 |
public function supports($name) |
|
|
44 |
{ |
|
|
45 |
foreach ($this->engines as $i => $engine) { |
|
|
46 |
if (is_string($engine)) { |
|
|
47 |
$engine = $this->engines[$i] = $this->container->get($engine); |
|
|
48 |
} |
|
|
49 |
|
|
|
50 |
if ($engine->supports($name)) { |
|
|
51 |
return true; |
|
|
52 |
} |
|
|
53 |
} |
|
|
54 |
|
|
|
55 |
return false; |
|
|
56 |
} |
|
|
57 |
|
|
|
58 |
/** |
|
|
59 |
* {@inheritdoc} |
|
|
60 |
*/ |
|
|
61 |
protected function getEngine($name) |
|
|
62 |
{ |
|
|
63 |
foreach ($this->engines as $i => $engine) { |
|
|
64 |
if (is_string($engine)) { |
|
|
65 |
$engine = $this->engines[$i] = $this->container->get($engine); |
|
|
66 |
} |
|
|
67 |
|
|
|
68 |
if ($engine->supports($name)) { |
|
|
69 |
return $engine; |
|
|
70 |
} |
|
|
71 |
} |
|
|
72 |
|
|
|
73 |
throw new \RuntimeException(sprintf('No engine is able to work with the template "%s".', $name)); |
|
|
74 |
} |
|
|
75 |
|
|
|
76 |
/** |
|
|
77 |
* Renders a view and returns a Response. |
|
|
78 |
* |
|
|
79 |
* @param string $view The view name |
|
|
80 |
* @param array $parameters An array of parameters to pass to the view |
|
|
81 |
* @param Response $response A Response instance |
|
|
82 |
* |
|
|
83 |
* @return Response A Response instance |
|
|
84 |
*/ |
|
|
85 |
public function renderResponse($view, array $parameters = array(), Response $response = null) |
|
|
86 |
{ |
|
|
87 |
if (null === $response) { |
|
|
88 |
$response = new Response(); |
|
|
89 |
} |
|
|
90 |
|
|
|
91 |
$response->setContent($this->render($view, $parameters)); |
|
|
92 |
|
|
|
93 |
return $response; |
|
|
94 |
} |
|
|
95 |
} |