Skip to content

Commit

Permalink
remove catching reflection e
Browse files Browse the repository at this point in the history
  • Loading branch information
miladrahimi committed Dec 24, 2019
1 parent 0d3e1d7 commit 2409ae2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ composer require miladrahimi/phprouter "4.*"
First of all, you need to configure your webserver to handle all the HTTP requests with a single PHP file like `index.php`. Here you can see sample configurations for Apache HTTP Server and NGINX.

* Apache `.htaccess` sample:
```
```apacheconfig
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
Expand Down
41 changes: 20 additions & 21 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -371,36 +371,33 @@ private function runControllerThroughMiddleware(
* @param ServerRequestInterface $request
* @return ResponseInterface|mixed|null
* @throws InvalidControllerException
* @throws ReflectionException
*/
private function runController($controller, array $parameters, ServerRequestInterface $request)
{
try {
if (is_string($controller) && strpos($controller, '@')) {
list($className, $methodName) = explode('@', $controller);

if (class_exists($className) == false) {
throw new InvalidControllerException("Controller class `$controller` not found.");
}
if (is_string($controller) && strpos($controller, '@')) {
list($className, $methodName) = explode('@', $controller);

$classObject = new $className();

if (method_exists($classObject, $methodName) == false) {
throw new InvalidControllerException("Controller method `$methodName` not found.");
}
if (class_exists($className) == false) {
throw new InvalidControllerException("Controller class `$controller` not found.");
}

$parameters = $this->arrangeMethodParameters($className, $methodName, $parameters, $request);
$classObject = new $className();

$controller = [$classObject, $methodName];
} elseif (is_callable($controller)) {
$parameters = $this->arrangeFunctionParameters($controller, $parameters, $request);
} else {
throw new InvalidControllerException('Invalid controller: ' . $controller);
if (method_exists($classObject, $methodName) == false) {
throw new InvalidControllerException("Controller method `$methodName` not found.");
}

return call_user_func_array($controller, $parameters);
} catch (ReflectionException $e) {
throw new InvalidControllerException('Reflection error', 0, $e);
$parameters = $this->arrangeMethodParameters($className, $methodName, $parameters, $request);

$controller = [$classObject, $methodName];
} elseif (is_callable($controller)) {
$parameters = $this->arrangeFunctionParameters($controller, $parameters, $request);
} else {
throw new InvalidControllerException('Invalid controller: ' . $controller);
}

return call_user_func_array($controller, $parameters);
}

/**
Expand Down Expand Up @@ -457,6 +454,7 @@ function (ReflectionParameter $parameter) use ($parameters, $request) {
return $parameters[$parameter->getName()];
}

/** @noinspection PhpPossiblePolymorphicInvocationInspection */
if (
($parameter->getType() && $parameter->getType()->getName() == ServerRequestInterface::class) ||
($parameter->getType() && $parameter->getType()->getName() == ServerRequest::class) ||
Expand All @@ -465,6 +463,7 @@ function (ReflectionParameter $parameter) use ($parameters, $request) {
return $request;
}

/** @noinspection PhpPossiblePolymorphicInvocationInspection */
if (
($parameter->getType() && $parameter->getType()->getName() == Router::class) ||
($parameter->getName() == 'router')
Expand Down

0 comments on commit 2409ae2

Please sign in to comment.