Skip to content

Commit

Permalink
Refactor callback invocation
Browse files Browse the repository at this point in the history
  • Loading branch information
bramus committed Dec 21, 2017
1 parent 3c87a67 commit 937c233
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions src/Bramus/Router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,26 +358,7 @@ private function handle($routes, $quitAfterRun = false)
}, $matches, array_keys($matches));

// Call the handling function with the URL parameters if the desired input is callable
if (is_callable($route['fn'])) {
call_user_func_array($route['fn'], $params);
} // If not, check the existence of special parameters
elseif (stripos($route['fn'], '@') !== false) {
// Explode segments of given route
list($controller, $method) = explode('@', $route['fn']);
// Adjust controller class if namespace has been set
if ($this->getNamespace() !== '') {
$controller = $this->getNamespace().'\\'.$controller;
}
// Check if class exists, if not just ignore and check if the class exists on the default namespace
if (class_exists($controller)) {
// First check if is a static method, directly trying to invoke it.
// If isn't a valid static method, we will try as a normal method invocation.
if (call_user_func_array(array(new $controller(), $method), $params) === false) {
// Try to call the method as an non-static method. (the if does nothing, only avoids the notice)
if (forward_static_call_array(array($controller, $method), $params) === false);
}
}
}
$this->invoke($route['fn'], $params);

++$numHandled;

Expand All @@ -392,6 +373,29 @@ private function handle($routes, $quitAfterRun = false)
return $numHandled;
}

private function invoke($fn, $params = array()) {
if (is_callable($fn)) {
call_user_func_array($fn, $params);
} // If not, check the existence of special parameters
elseif (stripos($fn, '@') !== false) {
// Explode segments of given route
list($controller, $method) = explode('@', $fn);
// Adjust controller class if namespace has been set
if ($this->getNamespace() !== '') {
$controller = $this->getNamespace().'\\'.$controller;
}
// Check if class exists, if not just ignore and check if the class exists on the default namespace
if (class_exists($controller)) {
// First check if is a static method, directly trying to invoke it.
// If isn't a valid static method, we will try as a normal method invocation.
if (call_user_func_array(array(new $controller(), $method), $params) === false) {
// Try to call the method as an non-static method. (the if does nothing, only avoids the notice)
if (forward_static_call_array(array($controller, $method), $params) === false);
}
}
}
}

/**
* Define the current relative URI.
*
Expand Down

0 comments on commit 937c233

Please sign in to comment.