Skip to content

Commit

Permalink
[HttpKernel] added Closure support to ControllerResolver
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Sep 16, 2010
1 parent 7734f44 commit 1990fc5
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/Symfony/Component/HttpKernel/Controller/ControllerResolver.php
Expand Up @@ -60,6 +60,10 @@ public function getController(Request $request)
return false;
}

if ($controller instanceof \Closure) {
return $controller;
}

list($controller, $method) = $this->createController($controller);

if (!method_exists($controller, $method)) {
Expand All @@ -85,17 +89,25 @@ public function getArguments(Request $request, $controller)
{
$attributes = $request->attributes->all();

list($controller, $method) = $controller;
if (is_array($controller)) {
list($controller, $method) = $controller;
$r = new \ReflectionObject($controller);
$parameters = $r->getMethod($method)->getParameters();
$repr = sprintf('%s::%s()', get_class($controller), $method);
} else {
$r = new \ReflectionFunction($controller);
$parameters = $r->getParameters();
$repr = 'Closure';
}

$r = new \ReflectionObject($controller);
$arguments = array();
foreach ($r->getMethod($method)->getParameters() as $param) {
foreach ($parameters as $param) {
if (array_key_exists($param->getName(), $attributes)) {
$arguments[] = $attributes[$param->getName()];
} elseif ($param->isDefaultValueAvailable()) {
$arguments[] = $param->getDefaultValue();
} else {
throw new \RuntimeException(sprintf('Controller "%s::%s()" requires that you provide a value for the "$%s" argument (because there is no default value or because there is a non optional argument after this one).', get_class($controller), $method, $param->getName()));
throw new \RuntimeException(sprintf('Controller "%s" requires that you provide a value for the "$%s" argument (because there is no default value or because there is a non optional argument after this one).', $repr, $param->getName()));
}
}

Expand Down

0 comments on commit 1990fc5

Please sign in to comment.