PhpEngine.php 2.6 KB

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