Skip to content

Commit

Permalink
Added some new pattern and made some optimization.
Browse files Browse the repository at this point in the history
  • Loading branch information
izniburak committed May 8, 2021
1 parent 674fe55 commit a0d9036
Showing 1 changed file with 16 additions and 22 deletions.
38 changes: 16 additions & 22 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@

namespace Buki\Router;

use Buki\Router\RouterCommand;
use Buki\Router\RouterException;
use Buki\Router\RouterRequest;
use Closure;
use Exception;
use ReflectionMethod;
Expand Down Expand Up @@ -43,7 +40,7 @@ class Router
/**
* Router Version
*/
const VERSION = '2.2.1';
const VERSION = '2.3.0';

/**
* @var string $baseFolder Pattern definitions for parameters of Route
Expand All @@ -64,11 +61,14 @@ class Router
* @var array $patterns Pattern definitions for parameters of Route
*/
protected $patterns = [
':id' => '(\d+)',
':number' => '(\d+)',
':any' => '([^/]+)',
':all' => '(.*)',
':string' => '(\w+)',
':any' => '([^/]+)',
':id' => '(\d+)',
':int' => '(\d+)',
':number' => '([+-]?([0-9]*[.])?[0-9]+)',
':float' => '([+-]?([0-9]*[.])?[0-9]+)',
':bool' => '(true|false|1|0)',
':string' => '([\w\-_]+)',
':slug' => '([\w\-_]+)',
':uuid' => '([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})',
':date' => '([0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1]))',
Expand Down Expand Up @@ -390,30 +390,24 @@ public function controller(string $route, string $controller, array $options = [
}
}

$methodVar = lcfirst(preg_replace('/' . $method . '/i', '', $methodName, 1));
$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)) {
if ((!empty($only) && !in_array($methodVar, $only))
|| (!empty($except) && in_array($methodVar, $except))) {
continue;
}

$ref = new ReflectionMethod($controller, $methodName);
$endpoints = [];
foreach ($ref->getParameters() as $param) {
$typeHint = $param->hasType() ? $param->getType()->getName() : null;
if (in_array($typeHint, ['int'])) {
$pattern = ':id';
} elseif (in_array($typeHint, ['string', 'float', 'bool'])) {
$pattern = ':slug';
} elseif ($typeHint === null) {
$pattern = ':any';
} else {
if (!in_array($typeHint, ['int', 'float', 'string', 'bool']) && $typeHint !== null) {
continue;
}
$pattern = $this->patterns[":{$typeHint}"] ? ":{$typeHint}" : ":any";
$endpoints[] = $param->isOptional() ? "{$pattern}?" : $pattern;
}

Expand Down Expand Up @@ -785,7 +779,7 @@ protected function getRequestUri(): string
$dirname = dirname($script);
$dirname = $dirname === '/' ? '' : $dirname;
$basename = basename($script);
$uri = str_replace([$dirname, $basename],null, $this->request()->server->get('REQUEST_URI'));
$uri = str_replace([$dirname, $basename], null, $this->request()->server->get('REQUEST_URI'));
return $this->clearRouteName(explode('?', $uri)[0]);
}
}

0 comments on commit a0d9036

Please sign in to comment.