Development

- Fixed 403 not allowed exception is now thrown as NotFoundHttpException.
- Added REQUEST_TYPE_HEAD to Route class.
- Minor optimizations.
This commit is contained in:
Simon Sessingø
2018-04-06 19:44:30 +02:00
parent b8cfc4eb0b
commit 8653bfa86f
5 changed files with 208 additions and 213 deletions
+1 -1
View File
@@ -313,7 +313,7 @@ class InputHandler
}
}
return ($filter !== null) ? array_intersect_key($output, array_flip($filter)) : $output;
return (\count($filter) > 0) ? array_intersect_key($output, array_flip($filter)) : $output;
}
/**
+14 -10
View File
@@ -18,6 +18,7 @@ abstract class Route implements IRoute
public const REQUEST_TYPE_PATCH = 'patch';
public const REQUEST_TYPE_OPTIONS = 'options';
public const REQUEST_TYPE_DELETE = 'delete';
public const REQUEST_TYPE_HEAD = 'head';
public static $requestTypes = [
self::REQUEST_TYPE_GET,
@@ -26,6 +27,7 @@ abstract class Route implements IRoute
self::REQUEST_TYPE_PATCH,
self::REQUEST_TYPE_OPTIONS,
self::REQUEST_TYPE_DELETE,
self::REQUEST_TYPE_HEAD,
];
/**
@@ -120,23 +122,25 @@ abstract class Route implements IRoute
protected function parseParameters($route, $url, $parameterRegex = null)
{
$regex = null;
if (strpos($route, $this->paramModifiers[0]) !== false) {
$regex = sprintf(static::PARAMETERS_REGEX_FORMAT, $this->paramModifiers[0], $this->paramOptionalSymbol, $this->paramModifiers[1]);
}
$regex = (strpos($route, $this->paramModifiers[0]) === false) ? null :
sprintf
(
static::PARAMETERS_REGEX_FORMAT,
$this->paramModifiers[0],
$this->paramOptionalSymbol,
$this->paramModifiers[1]
);
// Ensures that host names/domains will work with parameters
$url = '/' . ltrim($url, '/');
$urlRegex = null;
$urlRegex = '';
$parameters = [];
if ($regex === null || (bool)preg_match_all('/' . $regex . '/u', $route, $parameters) === false) {
$urlRegex = preg_quote($route, '/');
} else {
$urlParts = preg_split('/((\-?\/?)\{[^}]+\})/', $route);
foreach ($urlParts as $key => $t) {
foreach (preg_split('/((\-?\/?)\{[^}]+\})/', $route) as $key => $t) {
$regex = '';
@@ -164,7 +168,7 @@ abstract class Route implements IRoute
}
}
if ($urlRegex === null || (bool)preg_match(sprintf($this->urlRegex, $urlRegex), $url, $matches) === false) {
if (trim($urlRegex) === '' || (bool)preg_match(sprintf($this->urlRegex, $urlRegex), $url, $matches) === false) {
return null;
}
@@ -174,7 +178,7 @@ abstract class Route implements IRoute
/* Only take matched parameters with name */
foreach ((array)$parameters[1] as $name) {
$values[$name] = (isset($matches[$name]) && $matches[$name] !== '') ? $matches[$name] : null;
$values[$name] = (isset($matches[$name]) === true && $matches[$name] !== '') ? $matches[$name] : null;
}
}
+4 -6
View File
@@ -412,7 +412,7 @@ class Router
if ($methodNotAllowed === true) {
$message = sprintf('Route "%s" or method "%s" not allowed.', $this->request->getUrl()->getPath(), $this->request->getMethod());
$this->handleException(new HttpException($message, 403));
$this->handleException(new NotFoundHttpException($message, 403));
}
if (\count($this->request->getLoadedRoutes()) === 0) {
@@ -457,7 +457,9 @@ class Router
}
if ($this->request->getRewriteUrl() !== $url) {
unset($this->processedRoutes[$key]);
$this->request->setHasPendingRewrite(false);
$this->fireEvents(EventHandler::EVENT_REWRITE, [
@@ -644,11 +646,7 @@ class Router
}
/* Only merge $_GET when all parameters are null */
if ($name === null && $parameters === null && $getParams === null) {
$getParams = $_GET;
} else {
$getParams = (array)$getParams;
}
$getParams = ($name === null && $parameters === null && $getParams === null) ? $_GET : (array)$getParams;
/* Return current route if no options has been specified */
if ($name === null && $parameters === null) {
+6 -8
View File
@@ -24,7 +24,6 @@ use Pecee\SimpleRouter\Handlers\IEventHandler;
use Pecee\SimpleRouter\Route\IGroupRoute;
use Pecee\SimpleRouter\Route\IPartialGroupRoute;
use Pecee\SimpleRouter\Route\IRoute;
use Pecee\SimpleRouter\Route\Route;
use Pecee\SimpleRouter\Route\RouteController;
use Pecee\SimpleRouter\Route\RouteGroup;
use Pecee\SimpleRouter\Route\RoutePartialGroup;
@@ -75,8 +74,7 @@ class SimpleRouter
try {
ob_start();
static::router()->setDebugEnabled(true);
static::start();
static::router()->setDebugEnabled(true)->start();
$routerOutput = ob_get_contents();
ob_end_clean();
} catch (\Exception $e) {
@@ -187,7 +185,7 @@ class SimpleRouter
*/
public static function get(string $url, $callback, array $settings = null): IRoute
{
return static::match([Route::REQUEST_TYPE_GET], $url, $callback, $settings);
return static::match(['get'], $url, $callback, $settings);
}
/**
@@ -200,7 +198,7 @@ class SimpleRouter
*/
public static function post(string $url, $callback, array $settings = null): IRoute
{
return static::match([Route::REQUEST_TYPE_POST], $url, $callback, $settings);
return static::match(['post'], $url, $callback, $settings);
}
/**
@@ -213,7 +211,7 @@ class SimpleRouter
*/
public static function put(string $url, $callback, array $settings = null): IRoute
{
return static::match([Route::REQUEST_TYPE_PUT], $url, $callback, $settings);
return static::match(['put'], $url, $callback, $settings);
}
/**
@@ -226,7 +224,7 @@ class SimpleRouter
*/
public static function patch(string $url, $callback, array $settings = null): IRoute
{
return static::match([Route::REQUEST_TYPE_PATCH], $url, $callback, $settings);
return static::match(['patch'], $url, $callback, $settings);
}
/**
@@ -239,7 +237,7 @@ class SimpleRouter
*/
public static function options(string $url, $callback, array $settings = null): IRoute
{
return static::match([Route::REQUEST_TYPE_OPTIONS], $url, $callback, $settings);
return static::match(['options'], $url, $callback, $settings);
}
/**