|
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\PhpEngine as BasePhpEngine; |
|
15 use Symfony\Component\Templating\Loader\LoaderInterface; |
|
16 use Symfony\Component\Templating\TemplateNameParserInterface; |
|
17 use Symfony\Component\DependencyInjection\ContainerInterface; |
|
18 use Symfony\Component\HttpFoundation\Response; |
|
19 |
|
20 /** |
|
21 * This engine knows how to render Symfony templates. |
|
22 * |
|
23 * @author Fabien Potencier <fabien@symfony.com> |
|
24 */ |
|
25 class PhpEngine extends BasePhpEngine implements EngineInterface |
|
26 { |
|
27 protected $container; |
|
28 |
|
29 /** |
|
30 * Constructor. |
|
31 * |
|
32 * @param TemplateNameParserInterface $parser A TemplateNameParserInterface instance |
|
33 * @param ContainerInterface $container The DI container |
|
34 * @param LoaderInterface $loader A loader instance |
|
35 * @param GlobalVariables|null $globals A GlobalVariables instance or null |
|
36 */ |
|
37 public function __construct(TemplateNameParserInterface $parser, ContainerInterface $container, LoaderInterface $loader, GlobalVariables $globals = null) |
|
38 { |
|
39 $this->container = $container; |
|
40 |
|
41 parent::__construct($parser, $loader); |
|
42 |
|
43 if (null !== $globals) { |
|
44 $this->addGlobal('app', $globals); |
|
45 } |
|
46 } |
|
47 |
|
48 /** |
|
49 * @throws \InvalidArgumentException When the helper is not defined |
|
50 */ |
|
51 public function get($name) |
|
52 { |
|
53 if (!isset($this->helpers[$name])) { |
|
54 throw new \InvalidArgumentException(sprintf('The helper "%s" is not defined.', $name)); |
|
55 } |
|
56 |
|
57 if (is_string($this->helpers[$name])) { |
|
58 $this->helpers[$name] = $this->container->get($this->helpers[$name]); |
|
59 $this->helpers[$name]->setCharset($this->charset); |
|
60 } |
|
61 |
|
62 return $this->helpers[$name]; |
|
63 } |
|
64 |
|
65 /** |
|
66 * {@inheritdoc} |
|
67 */ |
|
68 public function setHelpers(array $helpers) |
|
69 { |
|
70 $this->helpers = $helpers; |
|
71 } |
|
72 |
|
73 /** |
|
74 * Renders a view and returns a Response. |
|
75 * |
|
76 * @param string $view The view name |
|
77 * @param array $parameters An array of parameters to pass to the view |
|
78 * @param Response $response A Response instance |
|
79 * |
|
80 * @return Response A Response instance |
|
81 */ |
|
82 public function renderResponse($view, array $parameters = array(), Response $response = null) |
|
83 { |
|
84 if (null === $response) { |
|
85 $response = new Response(); |
|
86 } |
|
87 |
|
88 $response->setContent($this->render($view, $parameters)); |
|
89 |
|
90 return $response; |
|
91 } |
|
92 } |