protected function createControllerClosure($name)
{
list($controllerName, $actionName) = explode(':', $name);
// Create a callable that will find or create the controller instance
// and then execute the action
$app = $this;
$callable = function () use($app, $controllerName, $actionName) {
// Try to fetch the controller instance from Slim's container
if ($app->container->has($controllerName)) {
$controller = $app->container->get($controllerName);
} else {
// not in container, assume it can be directly instantiated
$controller = new $controllerName($app);
}
// Set the app, request and response into the controller if we can
if (method_exists($controller, 'setApp')) {
$controller->setApp($app);
}
if (method_exists($controller, 'setRequest')) {
$controller->setRequest($app->request);
}
if (method_exists($controller, 'setResponse')) {
$controller->setResponse($app->response);
}
// Call init in case the controller wants to do something now that
// it has an app, request and response.
if (method_exists($controller, 'init')) {
$controller->init();
}
return call_user_func_array(array($controller, $actionName), func_get_args());
};
return $callable;
}