Development

- Added new Redirect method to SimpleRouter class.
- Changed method-names in InputHandler for better description.
- Fixed return-types for InputHandler for collections.
- Added unit-tests for InputHandler (get, post).
- Optimisations.
This commit is contained in:
Simon Sessingø
2018-04-06 17:20:00 +02:00
parent 30a2ddeed9
commit d38f81836d
15 changed files with 706 additions and 528 deletions
+5 -11
View File
@@ -120,9 +120,6 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
$url = '//' . $group->getDomains()[0] . $url;
}
/* Contains parameters that aren't recognized and will be appended at the end of the url */
$unknownParams = [];
/* Create the param string - {parameter} */
$param1 = $this->paramModifiers[0] . '%s' . $this->paramModifiers[1];
@@ -142,7 +139,7 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
$value = array_key_exists($param, $p) ? $p[$param] : $params[$param];
/* If parameter is specifically set to null - use the original-defined value */
if ($value === null && isset($this->originalParameters[$param])) {
if ($value === null && isset($this->originalParameters[$param]) === true) {
$value = $this->originalParameters[$param];
}
}
@@ -151,13 +148,12 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
/* Add parameter to the correct position */
$url = str_ireplace([sprintf($param1, $param), sprintf($param2, $param)], $value, $url);
} else {
$unknownParams[$param] = $value;
/* Parameter aren't recognized and will be appended at the end of the url */
$url .= $value . '/';
}
}
$url = '/' . ltrim($url, '/') . implode('/', $unknownParams);
return rtrim($url, '/') . '/';
return rtrim('/' . ltrim($url, '/'), '/') . '/';
}
/**
@@ -254,9 +250,7 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
$this->prependUrl($values['prefix']);
}
parent::setSettings($values, $merge);
return $this;
return parent::setSettings($values, $merge);
}
}
+12 -11
View File
@@ -76,7 +76,9 @@ abstract class Route implements IRoute
}
$router->debug('Parsing parameters');
$parameters = $this->getParameters();
$router->debug('Finished parsing parameters');
/* Filter parameters with null-value */
@@ -118,14 +120,17 @@ abstract class Route implements IRoute
protected function parseParameters($route, $url, $parameterRegex = null)
{
$regex = sprintf(static::PARAMETERS_REGEX_FORMAT, $this->paramModifiers[0], $this->paramOptionalSymbol, $this->paramModifiers[1]);
$parameters = [];
$regex = null;
if (strpos($route, $this->paramModifiers[0]) !== false) {
$regex = 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;
$parameters = [];
if ((bool)preg_match_all('/' . $regex . '/u', $route, $parameters) === false) {
if ($regex === null || (bool)preg_match_all('/' . $regex . '/u', $route, $parameters) === false) {
$urlRegex = preg_quote($route, '/');
} else {
@@ -155,14 +160,11 @@ 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;
$urlRegex .= preg_quote($t, '/') . $regex;
}
$urlRegex = implode('', $urlParts);
}
if ((bool)preg_match(sprintf($this->urlRegex, $urlRegex), $url, $matches) === false) {
if ($urlRegex === null || (bool)preg_match(sprintf($this->urlRegex, $urlRegex), $url, $matches) === false) {
return null;
}
@@ -247,9 +249,8 @@ abstract class Route implements IRoute
$this->group = $group;
/* Add/merge parent settings with child */
$this->setSettings($group->toArray(), true);
return $this;
return $this->setSettings($group->toArray(), true);
}
/**
@@ -95,7 +95,7 @@ class RouteController extends LoadableRoute implements IControllerRoute
/* Match global regular-expression for route */
$regexMatch = $this->matchRegex($request, $url);
if ($regexMatch === false || (stripos($url, $this->url) !== 0 && strtolower($url) !== strtolower($this->url))) {
if ($regexMatch === false || (stripos($url, $this->url) !== 0 && strtoupper($url) !== strtoupper($this->url))) {
return false;
}
@@ -177,9 +177,7 @@ class RouteController extends LoadableRoute implements IControllerRoute
$this->names = $values['names'];
}
parent::setSettings($values, $merge);
return $this;
return parent::setSettings($values, $merge);
}
}
+1 -3
View File
@@ -173,9 +173,7 @@ class RouteGroup extends Route implements IGroupRoute
$this->name = $name;
}
parent::setSettings($values, $merge);
return $this;
return parent::setSettings($values, $merge);
}
/**
@@ -92,7 +92,7 @@ class RouteResource extends LoadableRoute implements IControllerRoute
/* Match global regular-expression for route */
$regexMatch = $this->matchRegex($request, $url);
if ($regexMatch === false || (stripos($url, $this->url) !== 0 && strtolower($url) !== strtolower($this->url))) {
if ($regexMatch === false || (stripos($url, $this->url) !== 0 && strtoupper($url) !== strtoupper($this->url))) {
return false;
}
@@ -224,9 +224,7 @@ class RouteResource extends LoadableRoute implements IControllerRoute
$this->methodNames = $values['methods'];
}
parent::setSettings($values, $merge);
return $this;
return parent::setSettings($values, $merge);
}
}
+4 -14
View File
@@ -506,7 +506,6 @@ class Router
}
try {
$this->debug('Start rendering exception handler');
$handler->handleError($this->request, $e);
$this->debug('Finished rendering exception-handler');
@@ -565,7 +564,7 @@ class Router
}
/* Direct match to controller */
if ($route instanceof IControllerRoute && strtolower($route->getController()) === strtolower($name)) {
if ($route instanceof IControllerRoute && strtoupper($route->getController()) === strtoupper($name)) {
$this->debug('Found route "%s" by controller "%s"', $route->getUrl(), $name);
return $route;
@@ -805,26 +804,20 @@ class Router
* Set csrf verifier class
*
* @param BaseCsrfVerifier $csrfVerifier
* @return static
*/
public function setCsrfVerifier(BaseCsrfVerifier $csrfVerifier): self
public function setCsrfVerifier(BaseCsrfVerifier $csrfVerifier): void
{
$this->csrfVerifier = $csrfVerifier;
return $this;
}
/**
* Set class loader
*
* @param IClassLoader $loader
* @return static
*/
public function setClassLoader(IClassLoader $loader)
public function setClassLoader(IClassLoader $loader): void
{
$this->classLoader = $loader;
return $this;
}
/**
@@ -841,13 +834,10 @@ class Router
* Register event handler
*
* @param IEventHandler $handler
* @return static
*/
public function addEventHandler(IEventHandler $handler): self
public function addEventHandler(IEventHandler $handler): void
{
$this->eventHandlers[] = $handler;
return $this;
}
/**
+25 -17
View File
@@ -24,6 +24,7 @@ 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;
@@ -160,6 +161,21 @@ class SimpleRouter
static::router()->addBootManager($bootManager);
}
/**
* Redirect to when route matches.
*
* @param string $where
* @param string $to
* @param int $httpCode
* @return IRoute
*/
public static function redirect($where, $to, $httpCode = 301): IRoute
{
return static::get($where, function () use ($to, $httpCode) {
static::response()->redirect($to, $httpCode);
});
}
/**
* Route the given url to your callback on GET request method.
*
@@ -171,7 +187,7 @@ class SimpleRouter
*/
public static function get(string $url, $callback, array $settings = null): IRoute
{
return static::match(['get'], $url, $callback, $settings);
return static::match([Route::REQUEST_TYPE_GET], $url, $callback, $settings);
}
/**
@@ -184,7 +200,7 @@ class SimpleRouter
*/
public static function post(string $url, $callback, array $settings = null): IRoute
{
return static::match(['post'], $url, $callback, $settings);
return static::match([Route::REQUEST_TYPE_POST], $url, $callback, $settings);
}
/**
@@ -197,7 +213,7 @@ class SimpleRouter
*/
public static function put(string $url, $callback, array $settings = null): IRoute
{
return static::match(['put'], $url, $callback, $settings);
return static::match([Route::REQUEST_TYPE_PUT], $url, $callback, $settings);
}
/**
@@ -210,7 +226,7 @@ class SimpleRouter
*/
public static function patch(string $url, $callback, array $settings = null): IRoute
{
return static::match(['patch'], $url, $callback, $settings);
return static::match([Route::REQUEST_TYPE_PATCH], $url, $callback, $settings);
}
/**
@@ -223,7 +239,7 @@ class SimpleRouter
*/
public static function options(string $url, $callback, array $settings = null): IRoute
{
return static::match(['options'], $url, $callback, $settings);
return static::match([Route::REQUEST_TYPE_OPTIONS], $url, $callback, $settings);
}
/**
@@ -337,9 +353,7 @@ class SimpleRouter
$route->setSettings($settings);
}
static::router()->addRoute($route);
return $route;
return static::router()->addRoute($route);
}
/**
@@ -359,9 +373,7 @@ class SimpleRouter
$route->setSettings($settings);
}
static::router()->addRoute($route);
return $route;
return static::router()->addRoute($route);
}
/**
@@ -381,9 +393,7 @@ class SimpleRouter
$route->setSettings($settings);
}
static::router()->addRoute($route);
return $route;
return static::router()->addRoute($route);
}
/**
@@ -403,9 +413,7 @@ class SimpleRouter
$route->setSettings($settings);
}
static::router()->addRoute($route);
return $route;
return static::router()->addRoute($route);
}
/**