diff --git a/src/Pecee/Http/Input/Input.php b/src/Pecee/Http/Input/Input.php index 4c8fd71..ae86bca 100644 --- a/src/Pecee/Http/Input/Input.php +++ b/src/Pecee/Http/Input/Input.php @@ -51,15 +51,13 @@ class Input } /* Parse get requests */ - $this->file = $this->parseFiles(); + if (count($_FILES) > 0) { + $this->file = $this->parseFiles(); + } } public function parseFiles() { - if (count($_FILES) === 0) { - return []; - } - $list = []; foreach ($_FILES as $key => $value) { diff --git a/src/Pecee/Http/Input/InputFile.php b/src/Pecee/Http/Input/InputFile.php index 6fdec72..6ec141b 100644 --- a/src/Pecee/Http/Input/InputFile.php +++ b/src/Pecee/Http/Input/InputFile.php @@ -42,14 +42,13 @@ class InputFile implements IInputItem 'error' => null, ], $values); - $input = new static($values['index']); - $input->setError($values['error']) + return (new static($values['index'])) + ->setError($values['error']) ->setSize($values['size']) ->setType($values['type']) ->setTmpName($values['tmp_name']) ->setFilename($values['name']); - return $input; } /** diff --git a/src/Pecee/Http/Middleware/BaseCsrfVerifier.php b/src/Pecee/Http/Middleware/BaseCsrfVerifier.php index 046251b..7a96ba9 100644 --- a/src/Pecee/Http/Middleware/BaseCsrfVerifier.php +++ b/src/Pecee/Http/Middleware/BaseCsrfVerifier.php @@ -58,7 +58,7 @@ class BaseCsrfVerifier implements IMiddleware public function handle(Request $request, ILoadableRoute &$route = null) { - if ($this->skip($request) === false && in_array($request->getMethod(), ['post', 'put', 'delete']) === true) { + if ($this->skip($request) === false && in_array($request->getMethod(), ['post', 'put', 'delete'], false) === true) { $token = $request->getInput()->get(static::POST_KEY, null, 'post'); diff --git a/src/Pecee/Http/Request.php b/src/Pecee/Http/Request.php index 8215a27..eb2fe26 100644 --- a/src/Pecee/Http/Request.php +++ b/src/Pecee/Http/Request.php @@ -215,7 +215,7 @@ class Request public function __isset($name) { - return $this->data[$name] ?? null; + return array_key_exists($name, $this->data); } public function __set($name, $value = null) diff --git a/src/Pecee/SimpleRouter/Route/ILoadableRoute.php b/src/Pecee/SimpleRouter/Route/ILoadableRoute.php index 92ef82a..4036337 100644 --- a/src/Pecee/SimpleRouter/Route/ILoadableRoute.php +++ b/src/Pecee/SimpleRouter/Route/ILoadableRoute.php @@ -51,4 +51,19 @@ interface ILoadableRoute extends IRoute */ public function setName($name); + /** + * Get regular expression match used for matching route (if defined). + * + * @return string + */ + public function getMatch(); + + /** + * Add regular expression match for the entire route. + * + * @param string $regex + * @return static + */ + public function setMatch($regex); + } \ No newline at end of file diff --git a/src/Pecee/SimpleRouter/Route/IRoute.php b/src/Pecee/SimpleRouter/Route/IRoute.php index 14bcbe8..175cd68 100644 --- a/src/Pecee/SimpleRouter/Route/IRoute.php +++ b/src/Pecee/SimpleRouter/Route/IRoute.php @@ -18,7 +18,8 @@ interface IRoute * Returns class to be rendered. * * @param Request $request - * @return \Closure|string + * @throws \Pecee\SimpleRouter\Exceptions\NotFoundHttpException + * @return void */ public function renderRoute(Request $request); @@ -112,21 +113,6 @@ interface IRoute public function getDefaultNamespace(); - /** - * Get regular expression match used for matching route (if defined). - * - * @return string - */ - public function getMatch(); - - /** - * Add regular expression match for the entire route. - * - * @param string $regex - * @return static - */ - public function setMatch($regex); - /** * Get parameter names. * diff --git a/src/Pecee/SimpleRouter/Route/LoadableRoute.php b/src/Pecee/SimpleRouter/Route/LoadableRoute.php index 81b0297..39e9d0a 100644 --- a/src/Pecee/SimpleRouter/Route/LoadableRoute.php +++ b/src/Pecee/SimpleRouter/Route/LoadableRoute.php @@ -7,10 +7,8 @@ use Pecee\SimpleRouter\Exceptions\HttpException; abstract class LoadableRoute extends Route implements ILoadableRoute { - const PARAMETERS_REGEX_MATCH = '%s([\w\-\_]*?)\%s{0,1}%s'; - /** - * @var + * @var string */ protected $url; @@ -19,6 +17,8 @@ abstract class LoadableRoute extends Route implements ILoadableRoute */ protected $name; + protected $regex; + /** * Loads and renders middlewares-classes * @@ -37,6 +37,7 @@ abstract class LoadableRoute extends Route implements ILoadableRoute $middleware = $this->getMiddlewares()[$i]; $middleware = $this->loadClass($middleware); + if (!($middleware instanceof IMiddleware)) { throw new HttpException($middleware . ' must be instance of Middleware'); } @@ -46,6 +47,28 @@ abstract class LoadableRoute extends Route implements ILoadableRoute } } + public function matchRegex(Request $request, $url) + { + + /* Match on custom defined regular expression */ + + if ($this->regex === null) { + return null; + } + + $parameters = []; + + if (preg_match($this->regex, $request->getHost() . $url, $parameters) > 0) { + + /* Remove global match */ + $this->parameters = array_slice($parameters, 1); + + return true; + } + + return false; + } + /** * Set url * @@ -61,15 +84,8 @@ abstract class LoadableRoute extends Route implements ILoadableRoute $regex = sprintf(static::PARAMETERS_REGEX_MATCH, $this->paramModifiers[0], $this->paramOptionalSymbol, $this->paramModifiers[1]); if (preg_match_all('/' . $regex . '/is', $this->url, $matches)) { - - $max = count($matches[1]); - - for ($i = 0; $i < $max; $i++) { - $this->parameters[$matches[1][$i]] = null; - } - + $this->parameters = array_fill_keys($matches[1], null); } - } return $this; @@ -130,6 +146,7 @@ abstract class LoadableRoute extends Route implements ILoadableRoute } } + /** @noinspection AliasFunctionsUsageInspection */ $url .= join('/', $unknownParams); return rtrim($url, '/') . '/'; @@ -156,6 +173,29 @@ abstract class LoadableRoute extends Route implements ILoadableRoute return (strtolower($this->name) === strtolower($name)); } + /** + * Add regular expression match for the entire route. + * + * @param string $regex + * @return static + */ + public function setMatch($regex) + { + $this->regex = $regex; + + return $this; + } + + /** + * Get regular expression match used for matching route (if defined). + * + * @return string + */ + public function getMatch() + { + return $this->regex; + } + /** * Sets the router name, which makes it easier to obtain the url or router at a later point. * Alias for LoadableRoute::setName(). diff --git a/src/Pecee/SimpleRouter/Route/Route.php b/src/Pecee/SimpleRouter/Route/Route.php index cb2a8d3..59987e9 100644 --- a/src/Pecee/SimpleRouter/Route/Route.php +++ b/src/Pecee/SimpleRouter/Route/Route.php @@ -2,11 +2,12 @@ namespace Pecee\SimpleRouter\Route; use Pecee\Http\Request; -use Pecee\SimpleRouter\Exceptions\HttpException; use Pecee\SimpleRouter\Exceptions\NotFoundHttpException; abstract class Route implements IRoute { + const PARAMETERS_REGEX_MATCH = '%s([\w]+)(\%s?)%s'; + const REQUEST_TYPE_GET = 'get'; const REQUEST_TYPE_POST = 'post'; const REQUEST_TYPE_PUT = 'put'; @@ -23,6 +24,13 @@ abstract class Route implements IRoute self::REQUEST_TYPE_DELETE, ]; + /** + * If enabled parameters containing null-value + * will not be passed along to the callback. + * + * @var bool + */ + protected $filterEmptyParams = false; protected $paramModifiers = '{}'; protected $paramOptionalSymbol = '?'; protected $group; @@ -32,12 +40,21 @@ abstract class Route implements IRoute /* Default options */ protected $namespace; - protected $regex; protected $requestMethods = []; protected $where = []; protected $parameters = []; + protected $originalParameters = []; protected $middlewares = []; + protected function loadClass($name) + { + if (class_exists($name) === false) { + throw new NotFoundHttpException(sprintf('Class %s does not exist', $name), 404); + } + + return new $name(); + } + public function renderRoute(Request $request) { if ($this->getCallback() !== null && is_callable($this->getCallback())) { @@ -54,116 +71,72 @@ abstract class Route implements IRoute $class = $this->loadClass($className); $method = $controller[1]; - if (!method_exists($class, $method)) { + if (method_exists($class, $method) === false) { throw new NotFoundHttpException(sprintf('Method %s does not exist in class %s', $method, $className), 404); } - $parameters = array_filter($this->getParameters(), function ($var) { - return ($var !== null); - }); + $parameters = $this->getParameters(); + + /* Filter parameters with null-value */ + + if ($this->filterEmptyParams === true) { + $parameters = array_filter($parameters, function ($var) { + return ($var !== null); + }); + } call_user_func_array([$class, $method], $parameters); - - return $class; } - - return null; } protected function parseParameters($route, $url, $parameterRegex = '[\w]+') { - $parameterNames = []; - $regex = ''; - $lastCharacter = ''; - $isParameter = false; - $parameter = ''; - $routeLength = strlen($route) - 1; + $regex = sprintf(static::PARAMETERS_REGEX_MATCH, $this->paramModifiers[0], $this->paramOptionalSymbol, $this->paramModifiers[1]); - for ($i = $routeLength; $i >= 0; $i--) { + if (preg_match_all('/' . $regex . '/is', $route, $parameters)) { - $character = strrev($route)[$i]; + $parameterNamesRegex = []; + $parameterNames = $parameters[1]; + $parameterRequired = []; + + $urlParts = preg_split('/\{[^}]+\}/is', rtrim($route, '/')); + + foreach ($urlParts as $key => $t) { + + $regex = ''; + + if ($key < (count($parameters[1]))) { + + $name = $parameters[1][$key]; + $regex = isset($this->where[$name]) ? $this->where[$name] : $parameterRegex; + $regex = sprintf('(?P<%s>%s)', $name, $regex) . $parameters[2][$key]; - if ($character === '{') { - /* Remove "/" and "\" from regex */ - if (substr($regex, strlen($regex) - 1) === '/') { - $regex = substr($regex, 0, -2); } - $isParameter = true; - } elseif ($isParameter && $character === '}') { - $required = true; - - /* Check for optional parameter and use custom parameter regex if it exists */ - if (is_array($this->where) === true && isset($this->where[$parameter])) { - $parameterRegex = $this->where[$parameter]; - } - - if ($lastCharacter === '?') { - $parameter = substr($parameter, 0, -1); - $regex .= '(?:\/?(?P<' . $parameter . '>' . $parameterRegex . ')[^\/]?)?'; - $required = false; - } else { - $regex .= '\/?(?P<' . $parameter . '>' . $parameterRegex . ')[^\/]?'; - } - - $parameterNames[] = [ - 'name' => $parameter, - 'required' => $required, - ]; - - $parameter = ''; - $isParameter = false; - } elseif ($isParameter) { - $parameter .= $character; - } elseif ($character === '/') { - $regex .= '\\' . $character; - } else { - $regex .= str_replace('.', '\\.', $character); + $urlParts[$key] = preg_quote($t, '/') . $regex; } - $lastCharacter = $character; + $urlRegex = join('', $urlParts); + + } else { + $urlRegex = preg_quote($route, '/'); } - $parameterValues = []; + if (preg_match('/^' . $urlRegex . '(\/?)$/is', $url, $matches) > 0) { - if (preg_match('/^' . $regex . '\/?$/is', $url, $parameterValues)) { + $values = []; - $parameters = []; - - $max = count($parameterNames) - 1; - - for ($i = $max; $i >= 0; $i--) { - - $name = $parameterNames[$i]; - - $parameterValue = isset($parameterValues[$name['name']]) ? $parameterValues[$name['name']] : null; - - if ($parameterValue === null && $name['required']) { - throw new HttpException('Missing required parameter ' . $name['name'], 404); - } - - if ($parameterValue === null && $name['required'] === false) { - continue; - } - - $parameters[$name['name']] = $parameterValue; + /* Only take matched parameters with name */ + foreach ($parameters[1] as $name) { + $values[$name] = $matches[$name]; } - return $parameters; + return $values; } return null; } - protected function loadClass($name) - { - if (!class_exists($name)) { - throw new NotFoundHttpException(sprintf('Class %s does not exist', $name), 404); - } - - return new $name(); - } - /** * Returns callback name/identifier for the current route based on the callback. * Useful if you need to get a unique identifier for the loaded route, for instance @@ -339,29 +312,6 @@ abstract class Route implements IRoute return ($this->namespace === null) ? $this->defaultNamespace : $this->namespace; } - /** - * Add regular expression match for the entire route. - * - * @param string $regex - * @return static - */ - public function setMatch($regex) - { - $this->regex = $regex; - - return $this; - } - - /** - * Get regular expression match used for matching route (if defined). - * - * @return string - */ - public function getMatch() - { - return $this->regex; - } - /** * Export route settings to array so they can be merged with another route. * @@ -383,16 +333,6 @@ abstract class Route implements IRoute $values['where'] = $this->where; } - if (count($this->parameters) > 0) { - - /* Ensure the right order + values */ - $parameters = (isset($values['parameters']) ? $values['parameters'] : []) + $this->parameters; - $parameters = array_merge($parameters, $this->parameters); - - $this->setParameters($parameters); - $values['parameters'] = $parameters; - } - if (count($this->middlewares) > 0) { $values['middleware'] = $this->middlewares; } @@ -476,7 +416,14 @@ abstract class Route implements IRoute */ public function getParameters() { - return $this->parameters; + /* Sort the parameters after the user-defined param order, if any */ + $parameters = []; + + if (count($this->originalParameters) > 0) { + $parameters = $this->originalParameters; + } + + return array_merge($parameters, $this->parameters); } /** @@ -487,6 +434,14 @@ abstract class Route implements IRoute */ public function setParameters(array $parameters) { + /* + * If this is the first time setting parameters we store them so we + * later can organize the array, in case somebody tried to sort the array. + */ + if (count($parameters) > 0 && count($this->originalParameters) === 0) { + $this->originalParameters = $parameters; + } + $this->parameters = $parameters; return $this; diff --git a/src/Pecee/SimpleRouter/Route/RouteController.php b/src/Pecee/SimpleRouter/Route/RouteController.php index 486d92a..7f12d09 100644 --- a/src/Pecee/SimpleRouter/Route/RouteController.php +++ b/src/Pecee/SimpleRouter/Route/RouteController.php @@ -2,7 +2,6 @@ namespace Pecee\SimpleRouter\Route; use Pecee\Http\Request; -use Pecee\SimpleRouter\Exceptions\NotFoundHttpException; class RouteController extends LoadableRoute implements IControllerRoute { @@ -35,7 +34,7 @@ class RouteController extends LoadableRoute implements IControllerRoute $method = substr($name, strrpos($name, '.') + 1); $newName = substr($name, 0, strrpos($name, '.')); - if (in_array($method, $this->names) === true && strtolower($this->name) === strtolower($newName)) { + if (in_array($method, $this->names, false) === true && strtolower($this->name) === strtolower($newName)) { return true; } } @@ -52,7 +51,7 @@ class RouteController extends LoadableRoute implements IControllerRoute public function findUrl($method = null, $parameters = null, $name = null) { if (strpos($name, '.') !== false) { - $found = array_search(substr($name, strrpos($name, '.') + 1), $this->names); + $found = array_search(substr($name, strrpos($name, '.') + 1), $this->names, false); if ($found !== false) { $method = $found; } @@ -61,14 +60,10 @@ class RouteController extends LoadableRoute implements IControllerRoute $url = ''; $parameters = (array)$parameters; - /* Remove requestType from method-name, if it exists */ if ($method !== null) { - $max = count(static::$requestTypes); - - for ($i = 0; $i < $max; $i++) { - - $requestType = static::$requestTypes[$i]; + /* Remove requestType from method-name, if it exists */ + foreach (static::$requestTypes as $requestType) { if (stripos($method, $requestType) === 0) { $method = substr($method, strlen($requestType)); @@ -88,37 +83,16 @@ class RouteController extends LoadableRoute implements IControllerRoute return '/' . trim($url, '/') . '/'; } - public function renderRoute(Request $request) - { - if ($this->getCallback() !== null && is_callable($this->getCallback())) { - - // When the callback is a function - call_user_func_array($this->getCallback(), $this->getParameters()); - } else { - // When the callback is a method - $controller = explode('@', $this->getCallback()); - $className = $this->getNamespace() . '\\' . $controller[0]; - - $class = $this->loadClass($className); - $method = $request->getMethod() . ucfirst($controller[1]); - - if (!method_exists($class, $method)) { - throw new NotFoundHttpException(sprintf('Method %s does not exist in class %s', $method, $className), 404); - } - - call_user_func_array([$class, $method], $this->getParameters()); - - return $class; - } - - return null; - } - public function matchRoute(Request $request) { $url = parse_url(urldecode($request->getUri()), PHP_URL_PATH); $url = rtrim($url, '/') . '/'; + /* Match global regular-expression for route */ + if ($this->matchRegex($request, $url) === true) { + return true; + } + if (stripos($url, $this->url) === 0 && strtolower($url) === strtolower($this->url)) { $strippedUrl = trim(str_ireplace($this->url, '/', $url), '/'); @@ -130,9 +104,7 @@ class RouteController extends LoadableRoute implements IControllerRoute $method = (!isset($path[0]) || trim($path[0]) === '') ? $this->defaultMethod : $path[0]; $this->method = $method; - array_shift($path); - - $this->parameters = $path; + $this->parameters = array_slice($path, 1); // Set callback $this->setCallback($this->controller . '@' . $this->method); @@ -141,7 +113,7 @@ class RouteController extends LoadableRoute implements IControllerRoute } } - return null; + return false; } /** diff --git a/src/Pecee/SimpleRouter/Route/RouteGroup.php b/src/Pecee/SimpleRouter/Route/RouteGroup.php index f55567d..ca5aa04 100644 --- a/src/Pecee/SimpleRouter/Route/RouteGroup.php +++ b/src/Pecee/SimpleRouter/Route/RouteGroup.php @@ -18,26 +18,23 @@ class RouteGroup extends Route implements IGroupRoute */ public function matchDomain(Request $request) { - if (count($this->domains) > 0) { - - $max = count($this->domains) - 1; - - for ($i = $max; $i >= 0; $i--) { - - $domain = $this->domains[$i]; - $parameters = $this->parseParameters($domain, $request->getHost(), '.*'); - - if ($parameters !== null && count($parameters) > 0) { - $this->parameters = array_merge($this->parameters, $parameters); - - return true; - } - } - - return false; + if (count($this->domains) === 0) { + return true; } - return true; + foreach ($this->domains as $domain) { + + $parameters = $this->parseParameters($domain, $request->getHost(), '.*'); + + if ($parameters !== null && count($parameters) > 0) { + + $this->parameters = $parameters; + + return true; + } + } + + return false; } /** @@ -48,7 +45,7 @@ class RouteGroup extends Route implements IGroupRoute */ public function matchRoute(Request $request) { - // Skip if prefix doesn't match + /* Skip if prefix doesn't match */ if ($this->prefix !== null && stripos($request->getUri(), $this->prefix) === false) { return false; } @@ -175,6 +172,10 @@ class RouteGroup extends Route implements IGroupRoute $values['as'] = $this->name; } + if (count($this->parameters) > 0) { + $values['parameters'] = $this->parameters; + } + return array_merge($values, parent::toArray()); } diff --git a/src/Pecee/SimpleRouter/Route/RouteResource.php b/src/Pecee/SimpleRouter/Route/RouteResource.php index a4ea2b3..05ba6c0 100644 --- a/src/Pecee/SimpleRouter/Route/RouteResource.php +++ b/src/Pecee/SimpleRouter/Route/RouteResource.php @@ -2,7 +2,6 @@ namespace Pecee\SimpleRouter\Route; use Pecee\Http\Request; -use Pecee\SimpleRouter\Exceptions\NotFoundHttpException; class RouteResource extends LoadableRoute implements IControllerRoute { @@ -70,34 +69,9 @@ class RouteResource extends LoadableRoute implements IControllerRoute return $this->url; } - public function renderRoute(Request $request) - { - if ($this->getCallback() !== null && is_callable($this->getCallback())) { - // When the callback is a function - call_user_func_array($this->getCallback(), $this->getParameters()); - } else { - // When the callback is a method - $controller = explode('@', $this->getCallback()); - $className = $this->getNamespace() . '\\' . $controller[0]; - $class = $this->loadClass($className); - $method = strtolower($controller[1]); - - if (!method_exists($class, $method)) { - throw new NotFoundHttpException(sprintf('Method %s does not exist in class %s', $method, $className), 404); - } - - call_user_func_array([$class, $method], $this->getParameters()); - - return $class; - } - - return null; - } - - protected function call($method, $parameters) + protected function call($method) { $this->setCallback($this->controller . '@' . $method); - $this->parameters = $parameters; return true; } @@ -107,54 +81,58 @@ class RouteResource extends LoadableRoute implements IControllerRoute $url = parse_url(urldecode($request->getUri()), PHP_URL_PATH); $url = rtrim($url, '/') . '/'; + /* Match global regular-expression for route */ + $domainMatch = $this->matchRegex($request, $url); + if ($domainMatch !== null) { + return $domainMatch; + } + $route = rtrim($this->url, '/') . '/{id?}/{action?}'; $parameters = $this->parseParameters($route, $url); - - if ($parameters !== null) { - - $parameters = array_merge($this->parameters, (array)$parameters); - - $action = isset($parameters['action']) ? $parameters['action'] : null; - unset($parameters['action']); - - $method = $request->getMethod(); - - // Delete - if ($method === static::REQUEST_TYPE_DELETE && isset($parameters['id'])) { - return $this->call($this->methodNames['destroy'], $parameters); - } - - // Update - if (isset($parameters['id']) && in_array($method, [static::REQUEST_TYPE_PATCH, static::REQUEST_TYPE_PUT])) { - return $this->call($this->methodNames['update'], $parameters); - } - - // Edit - if ($method === static::REQUEST_TYPE_GET && isset($parameters['id']) && strtolower($action) === 'edit') { - return $this->call($this->methodNames['edit'], $parameters); - } - - // Create - if ($method === static::REQUEST_TYPE_GET && strtolower($action) === 'create') { - return $this->call($this->methodNames['create'], $parameters); - } - - // Save - if ($method === static::REQUEST_TYPE_POST) { - return $this->call($this->methodNames['store'], $parameters); - } - - // Show - if ($method === static::REQUEST_TYPE_GET && isset($parameters['id'])) { - return $this->call($this->methodNames['show'], $parameters); - } - - // Index - return $this->call($this->methodNames['index'], $parameters); + if ($parameters === null) { + return false; } - return null; + $this->parameters = (array)$parameters; + + $action = isset($this->parameters['action']) ? $this->parameters['action'] : null; + unset($this->parameters['action']); + + $method = $request->getMethod(); + + // Delete + if ($method === static::REQUEST_TYPE_DELETE && isset($this->parameters['id'])) { + return $this->call($this->methodNames['destroy']); + } + + // Update + if (isset($this->parameters['id']) && in_array($method, [static::REQUEST_TYPE_PATCH, static::REQUEST_TYPE_PUT], false)) { + return $this->call($this->methodNames['update']); + } + + // Edit + if ($method === static::REQUEST_TYPE_GET && isset($this->parameters['id']) && strtolower($action) === 'edit') { + return $this->call($this->methodNames['edit']); + } + + // Create + if ($method === static::REQUEST_TYPE_GET && strtolower($action) === 'create') { + return $this->call($this->methodNames['create']); + } + + // Save + if ($method === static::REQUEST_TYPE_POST) { + return $this->call($this->methodNames['store']); + } + + // Show + if ($method === static::REQUEST_TYPE_GET && isset($this->parameters['id'])) { + return $this->call($this->methodNames['show']); + } + + // Index + return $this->call($this->methodNames['index']); } /** diff --git a/src/Pecee/SimpleRouter/Route/RouteUrl.php b/src/Pecee/SimpleRouter/Route/RouteUrl.php index baa2329..3b2b985 100644 --- a/src/Pecee/SimpleRouter/Route/RouteUrl.php +++ b/src/Pecee/SimpleRouter/Route/RouteUrl.php @@ -16,34 +16,22 @@ class RouteUrl extends LoadableRoute $url = parse_url(urldecode($request->getUri()), PHP_URL_PATH); $url = rtrim($url, '/') . '/'; - // Match on custom defined regular expression - if ($this->regex !== null) { - $parameters = []; - if (preg_match($this->regex, $request->getHost() . $url, $parameters)) { - /* Remove global match */ - if (count($parameters) > 1) { - array_shift($parameters); - $this->parameters = $parameters; - } - - return true; - } - - return null; + /* Match global regular-expression for route */ + $domainMatch = $this->matchRegex($request, $url); + if ($domainMatch !== null) { + return $domainMatch; } - // Make regular expression based on route - $route = rtrim($this->url, '/') . '/'; - - $parameters = $this->parseParameters($route, $url); - - if ($parameters !== null) { - $this->parameters = array_merge($this->parameters, $parameters); - - return true; + /* Make regular expression based on route */ + $parameters = $this->parseParameters($this->url, $url); + if ($parameters === null) { + return false; } - return null; + $this->setParameters($parameters); + + return true; + } } \ No newline at end of file diff --git a/src/Pecee/SimpleRouter/Router.php b/src/Pecee/SimpleRouter/Router.php index e031431..7b8a9f2 100644 --- a/src/Pecee/SimpleRouter/Router.php +++ b/src/Pecee/SimpleRouter/Router.php @@ -167,7 +167,7 @@ class Router $route->renderRoute($this->request); $this->processingRoute = false; - if ($route->matchRoute($this->request)) { + if ($route->matchRoute($this->request) === true) { /* Add exception handlers */ if (count($route->getExceptionHandlers()) > 0) { @@ -220,7 +220,6 @@ class Router $routeNotAllowed = false; try { - /* Initialize boot-managers */ if (count($this->bootManagers) > 0) { @@ -246,7 +245,7 @@ class Router if ($this->csrfVerifier !== null) { - // Verify csrf token for request + /* Verify csrf token for request */ $this->csrfVerifier->handle($this->request); } @@ -261,10 +260,10 @@ class Router $route = $this->processedRoutes[$i]; /* If the route matches */ - if ($route->matchRoute($this->request)) { + if ($route->matchRoute($this->request) === true) { /* Check if request method matches */ - if (count($route->getRequestMethods()) > 0 && in_array($this->request->getMethod(), $route->getRequestMethods()) === false) { + if (count($route->getRequestMethods()) > 0 && in_array($this->request->getMethod(), $route->getRequestMethods(), false) === false) { $routeNotAllowed = true; continue; } diff --git a/test/RouterRouteTest.php b/test/RouterRouteTest.php index 23bdaff..5637f46 100644 --- a/test/RouterRouteTest.php +++ b/test/RouterRouteTest.php @@ -14,6 +14,26 @@ class RouterRouteTest extends PHPUnit_Framework_TestCase { protected $result = false; + public function testMultiParam() + { + SimpleRouter::router()->reset(); + SimpleRouter::request()->setMethod('get'); + SimpleRouter::request()->setUri('/test-param1-param2'); + + SimpleRouter::get('/test-{param1}-{param2}', function($param1, $param2) { + + if($param1 === 'param1' && $param2 === 'param2') { + $this->result = true; + } + + }); + + SimpleRouter::start(); + + $this->assertTrue($this->result); + + } + /** * Redirects to another route through 3 exception handlers. * @@ -118,16 +138,6 @@ class RouterRouteTest extends PHPUnit_Framework_TestCase SimpleRouter::start(); } - public function testMultiParam() - { - SimpleRouter::router()->reset(); - SimpleRouter::request()->setMethod('get'); - SimpleRouter::request()->setUri('/test-param1-param2'); - - SimpleRouter::get('/test-{param1}-{param2}', 'DummyController@param'); - SimpleRouter::start(); - } - public function testPathParamRegex() { SimpleRouter::router()->reset();