Development

- Fixed issue with subdomains on groups.
- Fixed issue with loadble routes sometimes mistakenly inheriting wrong group.
- Routes will now check if group matches before matching any custom regex.
- Added setValue method to IInputItem class to streamline the InputFile and InputItem classes.
- Other minor optimisations.
- Documentation: fixed broken links and cleaned it up.
This commit is contained in:
Simon Sessingo
2018-03-03 23:37:14 +01:00
parent be39010be3
commit d279d5598d
12 changed files with 125 additions and 94 deletions
+3
View File
@@ -1,4 +1,5 @@
<?php
namespace Pecee\Http\Input;
interface IInputItem
@@ -14,6 +15,8 @@ interface IInputItem
public function getValue();
public function setValue($value);
public function __toString();
}
+11
View File
@@ -266,6 +266,17 @@ class InputFile implements IInputItem
return $this->getFilename();
}
/**
* @param string $value
* @return static
*/
public function setValue($value)
{
$this->filename = $value;
return $this;
}
public function toArray()
{
return [
+1
View File
@@ -1,4 +1,5 @@
<?php
namespace Pecee\Http\Input;
class InputItem implements IInputItem
@@ -50,7 +50,7 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
return null;
}
return (preg_match($this->regex, $request->getHost() . $url) > 0);
return ((bool)preg_match($this->regex, $request->getHost() . $url) !== false);
}
/**
@@ -67,7 +67,7 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
$regex = sprintf(static::PARAMETERS_REGEX_FORMAT, $this->paramModifiers[0], $this->paramOptionalSymbol, $this->paramModifiers[1]);
if (preg_match_all('/' . $regex . '/u', $this->url, $matches) > 0) {
if ((bool)preg_match_all('/' . $regex . '/u', $this->url, $matches) !== false) {
$this->parameters = array_fill_keys($matches[1], null);
}
}
@@ -111,11 +111,8 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
/* Replace any {parameter} in the url with the correct value */
$params = $this->getParameters();
$max = count($params) - 1;
$keys = array_keys($params);
for ($i = $max; $i >= 0; $i--) {
$param = $keys[$i];
foreach (array_keys($params) as $param) {
if ($parameters === '' || (is_array($parameters) === true && count($parameters) === 0)) {
$value = '';
@@ -137,8 +134,7 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
}
}
$url = '/' . ltrim($url, '/');
$url .= implode('/', $unknownParams);
$url = '/' . ltrim($url, '/') . implode('/', $unknownParams);
return rtrim($url, '/') . '/';
}
+7 -5
View File
@@ -128,7 +128,9 @@ abstract class Route implements IRoute
// Ensures that hostnames/domains will work with parameters
$url = '/' . ltrim($url, '/');
if (preg_match_all('/' . $regex . '/u', $route, $parameters) !== 0) {
if ((bool)preg_match_all('/' . $regex . '/u', $route, $parameters) === false) {
$urlRegex = preg_quote($route, '/');
} else {
$urlParts = preg_split('/((\-?\/?)\{[^}]+\})/', $route);
@@ -154,7 +156,6 @@ abstract class Route implements IRoute
}
$regex = sprintf('(?:\/|\-)%1$s(?P<%2$s>%3$s)%1$s', $parameters[2][$key], $name, $regex);
}
$urlParts[$key] = preg_quote($t, '/') . $regex;
@@ -162,11 +163,9 @@ abstract class Route implements IRoute
$urlRegex = implode('', $urlParts);
} else {
$urlRegex = preg_quote($route, '/');
}
if (preg_match(sprintf($this->urlRegex, $urlRegex), $url, $matches) === 0) {
if ((bool)preg_match(sprintf($this->urlRegex, $urlRegex), $url, $matches) === false) {
return null;
}
@@ -250,6 +249,9 @@ abstract class Route implements IRoute
{
$this->group = $group;
/* Add/merge parent settings with child */
$this->setSettings($group->toArray(), true);
return $this;
}
@@ -88,6 +88,10 @@ class RouteController extends LoadableRoute implements IControllerRoute
public function matchRoute($url, Request $request)
{
if($this->getGroup() !== null && $this->getGroup()->matchRoute($url, $request) === false) {
return false;
}
/* Match global regular-expression for route */
$regexMatch = $this->matchRegex($request, $url);
@@ -48,6 +48,10 @@ class RouteGroup extends Route implements IGroupRoute
*/
public function matchRoute($url, Request $request)
{
if($this->getGroup() !== null && $this->getGroup()->matchRoute($url, $request) === false) {
return false;
}
/* Skip if prefix doesn't match */
if ($this->prefix !== null && stripos($url, $this->prefix) === false) {
return false;
@@ -17,6 +17,10 @@ class RoutePartialGroup extends RouteGroup implements IPartialGroupRoute
*/
public function matchRoute($url, Request $request)
{
if($this->getGroup() !== null && $this->getGroup()->matchRoute($url, $request) === false) {
return false;
}
if ($this->prefix !== null) {
/* Parse parameters from current route */
$parameters = $this->parseParameters($this->prefix, $url);
@@ -79,6 +79,10 @@ class RouteResource extends LoadableRoute implements IControllerRoute
public function matchRoute($url, Request $request)
{
if($this->getGroup() !== null && $this->getGroup()->matchRoute($url, $request) === false) {
return false;
}
/* Match global regular-expression for route */
$regexMatch = $this->matchRegex($request, $url);
@@ -14,6 +14,10 @@ class RouteUrl extends LoadableRoute
public function matchRoute($url, Request $request)
{
if($this->getGroup() !== null && $this->getGroup()->matchRoute($url, $request) === false) {
return false;
}
/* Match global regular-expression for route */
$regexMatch = $this->matchRegex($request, $url);
+37 -41
View File
@@ -97,31 +97,51 @@ class Router
public function addRoute(IRoute $route)
{
/*
* If a route is currently being processed, that means that the
* route being added are rendered from the parent routes callback,
* so we add them to the stack instead.
* If a route is currently being processed, that means that the route being added are rendered from the parent
* routes callback, so we add them to the stack instead.
*/
if ($this->processingRoute === true) {
$this->routeStack[] = $route;
} else {
$this->routes[] = $route;
return $route;
}
$this->routes[] = $route;
return $route;
}
/**
* Render and process any new routes added.
*
* @param IRoute $route
* @throws NotFoundHttpException
*/
protected function renderAndProcess(IRoute $route) {
$this->processingRoute = true;
$route->renderRoute($this->request);
$this->processingRoute = false;
if (count($this->routeStack) !== 0) {
/* Pop and grab the routes added when executing group callback earlier */
$stack = $this->routeStack;
$this->routeStack = [];
/* Route any routes added to the stack */
$this->processRoutes($stack, $route);
}
}
/**
* Process added routes.
*
* @param array $routes
* @param IGroupRoute|null $group
* @param IRoute|null $parent
* @throws NotFoundHttpException
*/
protected function processRoutes(array $routes, IGroupRoute $group = null, IRoute $parent = null)
protected function processRoutes(array $routes, IGroupRoute $group = null)
{
// Loop through each route-request
$exceptionHandlers = [];
// Stop processing routes if no valid route is found.
@@ -131,20 +151,10 @@ class Router
$url = ($this->request->getRewriteUrl() !== null) ? $this->request->getRewriteUrl() : $this->request->getUrl()->getPath();
/* @var $route IRoute */
foreach ($routes as $route) {
if ($parent !== null) {
/* Add the parent route */
$route->setParent($parent);
/* Add/merge parent settings with child */
$route->setSettings($parent->toArray(), true);
}
if ($group !== null) {
/* Add the parent group */
$route->setGroup($group);
}
@@ -152,8 +162,6 @@ class Router
/* @var $route IGroupRoute */
if ($route instanceof IGroupRoute) {
$group = $route;
if ($route->matchRoute($url, $this->request) === true) {
/* Add exception handlers */
@@ -163,36 +171,24 @@ class Router
}
/* Only render partial group if it matches */
if ($route instanceof IPartialGroupRoute) {
$this->processingRoute = true;
$route->renderRoute($this->request);
$this->processingRoute = false;
if ($route instanceof IPartialGroupRoute === true) {
$this->renderAndProcess($route);
}
}
if (($route instanceof IPartialGroupRoute) === false) {
$this->processingRoute = true;
$route->renderRoute($this->request);
$this->processingRoute = false;
if ($route instanceof IPartialGroupRoute === false) {
$this->renderAndProcess($route);
}
continue;
}
if ($route instanceof ILoadableRoute) {
if ($route instanceof ILoadableRoute === true) {
/* Add the route to the map, so we can find the active one when all routes has been loaded */
$this->processedRoutes[] = $route;
}
if (count($this->routeStack) !== 0) {
/* Pop and grab the routes added when executing group callback earlier */
$stack = $this->routeStack;
$this->routeStack = [];
/* Route any routes added to the stack */
$this->processRoutes($stack, $route, $group);
}
}
$this->exceptionHandlers = array_merge($exceptionHandlers, $this->exceptionHandlers);
@@ -606,4 +602,4 @@ class Router
return $this;
}
}
}