commit 538adc85a41df3394cfea91322df1029851e9bfc Author: Fabien Potencier Date: Fri Dec 28 11:41:35 2012 +0100 - diff --git a/core/lib/Drupal/Core/ControllerResolver.php b/core/lib/Drupal/Core/ControllerResolver.php index abb9889..e4f6b36 100644 --- a/core/lib/Drupal/Core/ControllerResolver.php +++ b/core/lib/Drupal/Core/ControllerResolver.php @@ -15,9 +15,17 @@ /** * ControllerResolver to enhance controllers beyond Symfony's basic handling. * - * When creating a new object-based controller that implements - * ContainerAwareInterface, inject the container into it. While not always - * necessary, that allows a controller to vary the services it needs at runtime. + * It adds two behaviors: + * + * * When creating a new object-based controller that implements + * ContainerAwareInterface, inject the container into it. While not always + * necessary, that allows a controller to vary the services it needs at + * runtime. + * + * * By default, a controller name follows the class::method notation. This + * class adds the possibility to use a service from the container as a + * controller by using a service:method notation (Symfony uses the same + * convention). */ class ControllerResolver extends BaseControllerResolver { @@ -50,17 +58,33 @@ public function __construct(ContainerInterface $container, LoggerInterface $logg * * @return mixed * A PHP callable. + * + * @throws \LogicException + * If the controller cannot be parsed */ protected function createController($controller) { - $controller = parent::createController($controller); + // class::method + if (false !== strpos($controller, '::')) { + list($class, $method) = explode('::', $controller, 2); + + if (!class_exists($class)) { + throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class)); + } + + $controller = new $class(); + if ($controller instanceof ContainerAwareInterface) { + $controller->setContainer($this->container); + } + return array($controller, $method); + } - // $controller will be an array of object and method name, per PHP's - // definition of a callable. Index 0 therefore is the object we want to - // enhance. - if ($controller[0] instanceof ContainerAwareInterface) { - $controller[0]->setContainer($this->container); + // service_name:method + if (1 == $count = substr_count($controller, ':')) { + // controller in the service:method notation + list($service, $method) = explode(':', $controller, 2); + return array($this->container->get($service), $method); } - return $controller; + throw new \LogicException(sprintf('Unable to parse the controller name "%s".', $controller)); } }