Skip to content

Commit

Permalink
Added expcet and only options for Controller method of Router.
Browse files Browse the repository at this point in the history
  • Loading branch information
izniburak committed Nov 29, 2020
1 parent 70e16b9 commit 10aaa47
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Router
/**
* Router Version
*/
const VERSION = '2.1.0';
const VERSION = '2.2.0';

/**
* @var string $baseFolder Pattern definitions for parameters of Route
Expand Down Expand Up @@ -375,6 +375,8 @@ public function controller(string $route, string $controller, array $options = [
return true;
}

$only = $options['only'] ?? [];
$except = $options['except'] ?? [];
$controller = $this->resolveClassName($controller);
$classMethods = get_class_methods($controller);
if ($classMethods) {
Expand All @@ -390,6 +392,15 @@ public function controller(string $route, string $controller, array $options = [

$methodVar = lcfirst(preg_replace('/' . $method . '/i', '', $methodName, 1));
$methodVar = strtolower(preg_replace('%([a-z]|[0-9])([A-Z])%', '\1-\2', $methodVar));

if (!empty($only) && !in_array($methodVar, $only)) {
continue;
}

if (!empty($except) && in_array($methodVar, $except)) {
continue;
}

$ref = new ReflectionMethod($controller, $methodName);
$endpoints = [];
foreach ($ref->getParameters() as $param) {
Expand Down Expand Up @@ -770,9 +781,10 @@ protected function clearRouteName(string $route = ''): string
*/
protected function getRequestUri(): string
{
$dirname = dirname($this->request()->server->get('SCRIPT_NAME'));
$script = $this->request()->server->get('SCRIPT_NAME');
$dirname = dirname($script);
$dirname = $dirname === '/' ? '' : $dirname;
$basename = basename($_SERVER['SCRIPT_NAME']);
$basename = basename($script);
$uri = str_replace([$dirname, $basename],null, $this->request()->server->get('REQUEST_URI'));
return $this->clearRouteName(explode('?', $uri)[0]);
}
Expand Down

0 comments on commit 10aaa47

Please sign in to comment.