mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-17 16:57:53 +00:00
Compare commits
19 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c1c25e19ff | |||
| dc7faf52c0 | |||
| 539e905b45 | |||
| 5977efc942 | |||
| bf069c2d42 | |||
| 523d49359b | |||
| 7cc2652bcd | |||
| e26d55a810 | |||
| 8f3ce68a5e | |||
| d25351f4f9 | |||
| e8f19fbeae | |||
| 5c89ae2aaf | |||
| b694a7c0c9 | |||
| 7847b71bbc | |||
| d9b97ccf42 | |||
| 74351e0330 | |||
| f5b03e106c | |||
| 2c5221051e | |||
| aad11ac581 |
@@ -51,15 +51,13 @@ class Input
|
||||
}
|
||||
|
||||
/* Parse get requests */
|
||||
$this->parseFiles();
|
||||
if (count($_FILES) > 0) {
|
||||
$this->file = $this->parseFiles();
|
||||
}
|
||||
}
|
||||
|
||||
public function parseFiles()
|
||||
{
|
||||
if (count($_FILES) === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$list = [];
|
||||
|
||||
foreach ($_FILES as $key => $value) {
|
||||
@@ -73,7 +71,13 @@ class Input
|
||||
|
||||
$keys = [];
|
||||
|
||||
$list = array_merge_recursive($list, [$key => $this->rearrangeFiles($value['name'], $keys, $value)]);
|
||||
$files = $this->rearrangeFiles($value['name'], $keys, $value);
|
||||
|
||||
if (isset($list[$key])) {
|
||||
$list[$key][] = $files;
|
||||
} else {
|
||||
$list[$key] = $files;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -109,8 +113,6 @@ class Input
|
||||
'filename' => $getItem($key, 'name'),
|
||||
]);
|
||||
|
||||
$output = array_merge_recursive($output, [$key => $file]);
|
||||
|
||||
if (isset($output[$key])) {
|
||||
$output[$key][] = $file;
|
||||
} else {
|
||||
@@ -121,7 +123,14 @@ class Input
|
||||
}
|
||||
|
||||
$index[] = $key;
|
||||
$output = array_merge_recursive($output, [$key => $this->rearrangeFiles($value, $index, $original)]);
|
||||
|
||||
$files = $this->rearrangeFiles($value, $index, $original);
|
||||
|
||||
if (isset($output[$key])) {
|
||||
$output[$key][] = $files;
|
||||
} else {
|
||||
$output[$key] = $files;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -258,29 +267,21 @@ class Input
|
||||
*/
|
||||
public function all(array $filter = null)
|
||||
{
|
||||
$output = $_POST;
|
||||
$output = $_GET;
|
||||
|
||||
if ($this->request->getMethod() === 'post') {
|
||||
|
||||
$contents = file_get_contents('php://input');
|
||||
|
||||
if (strpos(trim($contents), '{') === 0) {
|
||||
$output = json_decode($contents, true);
|
||||
if ($output === false) {
|
||||
$output = [];
|
||||
$post = json_decode($contents, true);
|
||||
if ($post !== false) {
|
||||
$output = array_merge($output, $post);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$output = array_merge($_GET, $output);
|
||||
|
||||
if ($filter !== null) {
|
||||
$output = array_filter($output, function ($key) use ($filter) {
|
||||
return (in_array($key, $filter) === true);
|
||||
}, ARRAY_FILTER_USE_KEY);
|
||||
}
|
||||
|
||||
return $output;
|
||||
return ($filter !== null) ? array_intersect_key($output, array_flip($filter)) : $output;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -128,7 +127,7 @@ class InputFile implements IInputItem
|
||||
*/
|
||||
public function getExtension()
|
||||
{
|
||||
return pathinfo($this->getName(), PATHINFO_EXTENSION);
|
||||
return pathinfo($this->getFilename(), PATHINFO_EXTENSION);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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');
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ class Request
|
||||
$this->host = $this->getHeader('http-host');
|
||||
$this->uri = $this->getHeader('request-uri');
|
||||
$this->input = new Input($this);
|
||||
$this->method = strtolower($this->input->get('_method', $this->getHeader('request-method'), 'post'));
|
||||
$this->method = strtolower($this->input->get('_method', $this->getHeader('request-method')));
|
||||
}
|
||||
|
||||
protected function parseHeaders()
|
||||
@@ -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)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
namespace Pecee\Http;
|
||||
|
||||
class Response
|
||||
@@ -82,14 +83,20 @@ class Response
|
||||
}
|
||||
|
||||
/**
|
||||
* Json encode array
|
||||
* @param array $value
|
||||
* Json encode
|
||||
* @param array|\JsonSerializable $value
|
||||
* @param int $options JSON options Bitmask consisting of JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT, JSON_PRESERVE_ZERO_FRACTION, JSON_UNESCAPED_UNICODE, JSON_PARTIAL_OUTPUT_ON_ERROR.
|
||||
* @param int $dept JSON debt.
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function json(array $value)
|
||||
public function json($value, $options = null, $dept = 512)
|
||||
{
|
||||
$this->header('Content-Type: application/json');
|
||||
echo json_encode($value);
|
||||
die();
|
||||
if (($value instanceof \JsonSerializable) === false && is_array($value) === false) {
|
||||
throw new \InvalidArgumentException('Invalid type for parameter "value". Must be of type array or object implementing the \JsonSerializable interface.');
|
||||
}
|
||||
$this->header('Content-Type: application/json; charset=utf-8');
|
||||
echo json_encode($value, $options, $dept);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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);
|
||||
|
||||
}
|
||||
@@ -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.
|
||||
*
|
||||
|
||||
@@ -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;
|
||||
@@ -91,39 +107,38 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
|
||||
*/
|
||||
public function findUrl($method = null, $parameters = null, $name = null)
|
||||
{
|
||||
$url = '';
|
||||
|
||||
$parameters = (array)$parameters;
|
||||
$url = $this->getUrl();
|
||||
|
||||
if ($this->getGroup() !== null && count($this->getGroup()->getDomains()) > 0) {
|
||||
$url .= '//' . $this->getGroup()->getDomains()[0];
|
||||
$url = '//' . $this->getGroup()->getDomains()[0] . $url;
|
||||
}
|
||||
|
||||
$url .= $this->getUrl();
|
||||
|
||||
$params = array_merge($this->getParameters(), $parameters);
|
||||
|
||||
/* Url that contains parameters that aren't recognized */
|
||||
/* Contains parameters that aren't recognized and will be appended at the end of the url */
|
||||
$unknownParams = [];
|
||||
|
||||
/* Create the param string - {} */
|
||||
/* Create the param string - {parameter} */
|
||||
$param1 = $this->paramModifiers[0] . '%s' . $this->paramModifiers[1];
|
||||
|
||||
/* Create the param string with the optional symbol - {?} */
|
||||
/* Create the param string with the optional symbol - {parameter?} */
|
||||
$param2 = $this->paramModifiers[0] . '%s' . $this->paramOptionalSymbol . $this->paramModifiers[1];
|
||||
|
||||
/* Let's parse the values of any {} parameter in the url */
|
||||
/* 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];
|
||||
$value = $params[$param];
|
||||
$value = $value = ($parameters !== null && array_key_exists($param, $parameters)) ? $parameters[$param] : $params[$param];
|
||||
|
||||
$value = isset($parameters[$param]) ? $parameters[$param] : $value;
|
||||
/* If parameter is specifically set to null - use the original-defined value */
|
||||
if ($value === null && isset($this->originalParameters[$param])) {
|
||||
$value = $this->originalParameters[$param];
|
||||
}
|
||||
|
||||
if (stripos($url, $param1) !== false || stripos($url, $param) !== false) {
|
||||
/* Add parameter to the correct position */
|
||||
$url = str_ireplace([sprintf($param1, $param), sprintf($param2, $param)], $value, $url);
|
||||
} else {
|
||||
$unknownParams[$param] = $value;
|
||||
@@ -132,6 +147,8 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
|
||||
|
||||
$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().
|
||||
|
||||
@@ -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,68 @@ 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];
|
||||
$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] = (isset($matches[$name]) && $matches[$name] !== '') ? $matches[$name] : null;
|
||||
}
|
||||
|
||||
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 +308,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 +329,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;
|
||||
}
|
||||
@@ -466,7 +402,7 @@ abstract class Route implements IRoute
|
||||
*/
|
||||
public function where(array $options)
|
||||
{
|
||||
return $this->where($options);
|
||||
return $this->setWhere($options);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -476,7 +412,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,7 +430,15 @@ abstract class Route implements IRoute
|
||||
*/
|
||||
public function setParameters(array $parameters)
|
||||
{
|
||||
$this->parameters = $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 = array_merge($this->parameters, $parameters);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
|
||||
@@ -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']);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
+20
-10
@@ -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();
|
||||
|
||||
@@ -99,4 +99,16 @@ class RouterUrlTest extends PHPUnit_Framework_TestCase
|
||||
|
||||
}
|
||||
|
||||
public function testRegEx()
|
||||
{
|
||||
SimpleRouter::router()->reset();
|
||||
SimpleRouter::request()->setMethod('get');
|
||||
SimpleRouter::request()->setUri('/my/custom-path');
|
||||
|
||||
SimpleRouter::get('/my/{path}', 'DummyController@start')->where(['path' => '[a-zA-Z\-]+']);
|
||||
|
||||
SimpleRouter::start();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user