PageRenderTime 27ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/core/lib/Drupal/Core/Controller/ControllerResolver.php

https://bitbucket.org/aswinvk28/smartpan-stock-drupal
PHP | 173 lines | 80 code | 23 blank | 70 comment | 20 complexity | 7dab4b31311fd2ad44db9326c27c0cbd MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * @file
  4. * Contains \Drupal\Core\Controller\ControllerResolver.
  5. */
  6. namespace Drupal\Core\Controller;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpKernel\Controller\ControllerResolver as BaseControllerResolver;
  9. use Symfony\Component\HttpKernel\Log\LoggerInterface;
  10. use Symfony\Component\DependencyInjection\ContainerAwareInterface;
  11. use Symfony\Component\DependencyInjection\ContainerInterface;
  12. /**
  13. * ControllerResolver to enhance controllers beyond Symfony's basic handling.
  14. *
  15. * It adds two behaviors:
  16. *
  17. * - When creating a new object-based controller that implements
  18. * ContainerAwareInterface, inject the container into it. While not always
  19. * necessary, that allows a controller to vary the services it needs at
  20. * runtime.
  21. *
  22. * - By default, a controller name follows the class::method notation. This
  23. * class adds the possibility to use a service from the container as a
  24. * controller by using a service:method notation (Symfony uses the same
  25. * convention).
  26. */
  27. class ControllerResolver extends BaseControllerResolver implements ControllerResolverInterface {
  28. /**
  29. * The injection container that should be injected into all controllers.
  30. *
  31. * @var \Symfony\Component\DependencyInjection\ContainerInterface
  32. */
  33. protected $container;
  34. /**
  35. * The PSR-3 logger. (optional)
  36. *
  37. * @var \Psr\Log\LoggerInterface;
  38. */
  39. protected $logger;
  40. /**
  41. * Constructs a new ControllerResolver.
  42. *
  43. * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
  44. * A ContainerInterface instance.
  45. * @param \Symfony\Component\HttpKernel\Log\LoggerInterface $logger
  46. * (optional) A LoggerInterface instance.
  47. */
  48. public function __construct(ContainerInterface $container, LoggerInterface $logger = NULL) {
  49. $this->container = $container;
  50. parent::__construct($logger);
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function getControllerFromDefinition($controller, $path = '') {
  56. if (is_array($controller) || (is_object($controller) && method_exists($controller, '__invoke'))) {
  57. return $controller;
  58. }
  59. if (strpos($controller, ':') === FALSE) {
  60. if (method_exists($controller, '__invoke')) {
  61. return new $controller;
  62. }
  63. elseif (function_exists($controller)) {
  64. return $controller;
  65. }
  66. }
  67. $callable = $this->createController($controller);
  68. if (!is_callable($callable)) {
  69. throw new \InvalidArgumentException(sprintf('The controller for URI "%s" is not callable.', $path));
  70. }
  71. return $callable;
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. public function getController(Request $request) {
  77. if (!$controller = $request->attributes->get('_controller')) {
  78. if ($this->logger !== NULL) {
  79. $this->logger->warning('Unable to look for the controller as the "_controller" parameter is missing');
  80. }
  81. return FALSE;
  82. }
  83. return $this->getControllerFromDefinition($controller, $request->getPathInfo());
  84. }
  85. /**
  86. * Returns a callable for the given controller.
  87. *
  88. * @param string $controller
  89. * A Controller string.
  90. *
  91. * @return mixed
  92. * A PHP callable.
  93. *
  94. * @throws \LogicException
  95. * If the controller cannot be parsed
  96. *
  97. * @throws \InvalidArgumentException
  98. * If the controller class does not exist
  99. */
  100. protected function createController($controller) {
  101. // Controller in the service:method notation.
  102. $count = substr_count($controller, ':');
  103. if ($count == 1) {
  104. list($service, $method) = explode(':', $controller, 2);
  105. return array($this->container->get($service), $method);
  106. }
  107. // Controller in the class::method notation.
  108. if (strpos($controller, '::') !== FALSE) {
  109. list($class, $method) = explode('::', $controller, 2);
  110. if (!class_exists($class)) {
  111. throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
  112. }
  113. // @todo Remove the second in_array() once that interface has been removed.
  114. if (in_array('Drupal\Core\DependencyInjection\ContainerInjectionInterface', class_implements($class))) {
  115. $controller = $class::create($this->container);
  116. }
  117. else {
  118. $controller = new $class();
  119. }
  120. }
  121. else {
  122. throw new \LogicException(sprintf('Unable to parse the controller name "%s".', $controller));
  123. }
  124. if ($controller instanceof ContainerAwareInterface) {
  125. $controller->setContainer($this->container);
  126. }
  127. return array($controller, $method);
  128. }
  129. /**
  130. * {@inheritdoc}
  131. */
  132. protected function doGetArguments(Request $request, $controller, array $parameters) {
  133. $arguments = parent::doGetArguments($request, $controller, $parameters);
  134. // The parameter converter overrides the raw request attributes with the
  135. // upcasted objects. However, it keeps a backup copy of the original, raw
  136. // values in a special request attribute ('_raw_variables'). If a controller
  137. // argument has a type hint, we pass it the upcasted object, otherwise we
  138. // pass it the original, raw value.
  139. if ($request->attributes->has('_raw_variables') && $raw = $request->attributes->get('_raw_variables')->all()) {
  140. foreach ($parameters as $parameter) {
  141. // Use the raw value if a parameter has no typehint.
  142. if (!$parameter->getClass() && isset($raw[$parameter->name])) {
  143. $position = $parameter->getPosition();
  144. $arguments[$position] = $raw[$parameter->name];
  145. }
  146. }
  147. }
  148. return $arguments;
  149. }
  150. }