PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/bundles/Sensio/Bundle/FrameworkExtraBundle/EventListener/TemplateListener.php

https://github.com/nguyennamtien/TaskBoxx
PHP | 160 lines | 84 code | 26 blank | 50 comment | 12 complexity | 4c0c8385aeaa7b019923e034651cfe05 MD5 | raw file
  1. <?php
  2. namespace Sensio\Bundle\FrameworkExtraBundle\EventListener;
  3. use Symfony\Component\DependencyInjection\ContainerInterface;
  4. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  5. use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference;
  9. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  10. /*
  11. * This file is part of the Symfony framework.
  12. *
  13. * (c) Fabien Potencier <fabien@symfony.com>
  14. *
  15. * This source file is subject to the MIT license that is bundled
  16. * with this source code in the file LICENSE.
  17. */
  18. /**
  19. * The TemplateListener class handles the @Template annotation.
  20. *
  21. * @author Fabien Potencier <fabien@symfony.com>
  22. */
  23. class TemplateListener
  24. {
  25. /**
  26. * @var Symfony\Component\DependencyInjection\ContainerInterface
  27. */
  28. protected $container;
  29. /**
  30. * Constructor.
  31. *
  32. * @param ContainerInterface $container The service container instance
  33. */
  34. public function __construct(ContainerInterface $container)
  35. {
  36. $this->container = $container;
  37. }
  38. /**
  39. * Guesses the template name to render and its variables and adds them to
  40. * the request object.
  41. *
  42. * @param FilterControllerEvent $event A FilterControllerEvent instance
  43. */
  44. public function onCoreController(FilterControllerEvent $event)
  45. {
  46. if (!is_array($controller = $event->getController())) {
  47. return;
  48. }
  49. $request = $event->getRequest();
  50. if (!$configuration = $request->attributes->get('_template')) {
  51. return;
  52. }
  53. if (!$configuration->getTemplate()) {
  54. $configuration->setTemplate($this->guessTemplateName($controller, $request));
  55. }
  56. $request->attributes->set('_template', $configuration->getTemplate());
  57. $request->attributes->set('_template_vars', $configuration->getVars());
  58. // all controller method arguments
  59. if (!$configuration->getVars()) {
  60. $r = new \ReflectionObject($controller[0]);
  61. $vars = array();
  62. foreach ($r->getMethod($controller[1])->getParameters() as $param) {
  63. $vars[] = $param->getName();
  64. }
  65. $request->attributes->set('_template_default_vars', $vars);
  66. }
  67. }
  68. /**
  69. * Renders the template and initializes a new response object with the
  70. * rendered template content.
  71. *
  72. * @param GetResponseForControllerResultEvent $event A GetResponseForControllerResultEvent instance
  73. */
  74. public function onCoreView(GetResponseForControllerResultEvent $event)
  75. {
  76. $request = $event->getRequest();
  77. $parameters = $event->getControllerResult();
  78. if (null === $parameters) {
  79. if (!$vars = $request->attributes->get('_template_vars')) {
  80. if (!$vars = $request->attributes->get('_template_default_vars')) {
  81. return;
  82. }
  83. }
  84. $parameters = array();
  85. foreach ($vars as $var) {
  86. $parameters[$var] = $request->attributes->get($var);
  87. }
  88. }
  89. if (!is_array($parameters)) {
  90. return $parameters;
  91. }
  92. if (!$template = $request->attributes->get('_template')) {
  93. return $parameters;
  94. }
  95. $event->setResponse(new Response($this->container->get('templating')->render($template, $parameters)));
  96. }
  97. /**
  98. * Guesses and returns the template name to render based on the controller
  99. * and action names.
  100. *
  101. * @param array $controller An array storing the controller object and action method
  102. * @param Request $request A Request instance
  103. * @return TemplateReference template reference
  104. * @throws \InvalidArgumentException
  105. */
  106. protected function guessTemplateName($controller, Request $request)
  107. {
  108. if (!preg_match('/Controller\\\(.+)Controller$/', get_class($controller[0]), $matchController)) {
  109. throw new \InvalidArgumentException(sprintf('The "%s" class does not look like a controller class (it must be in a "Controller" sub-namespace and the class name must end with "Controller")', get_class($controller[0])));
  110. }
  111. if (!preg_match('/^(.+)Action$/', $controller[1], $matchAction)) {
  112. throw new \InvalidArgumentException(sprintf('The "%s" method does not look like an action method (it does not end with Action)', $controller[1]));
  113. }
  114. $bundle = $this->getBundleForClass(get_class($controller[0]));
  115. return new TemplateReference($bundle->getName(), $matchController[1], $matchAction[1], $request->getRequestFormat(), 'twig');
  116. }
  117. /**
  118. * Returns the Bundle instance in which the given class name is located.
  119. *
  120. * @param string $class A fully qualified controller class name
  121. * @param Bundle $bundle A Bundle instance
  122. * @throws \InvalidArgumentException
  123. */
  124. protected function getBundleForClass($class)
  125. {
  126. $namespace = strtr(dirname(strtr($class, '\\', '/')), '/', '\\');
  127. foreach ($this->container->get('kernel')->getBundles() as $bundle) {
  128. if (0 === strpos($namespace, $bundle->getNamespace())) {
  129. return $bundle;
  130. }
  131. }
  132. throw new \InvalidArgumentException(sprintf('The "%s" class does not belong to a registered bundle.', $class));
  133. }
  134. }