mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-17 16:57:53 +00:00
Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 448a423d5d | |||
| df9a855579 | |||
| d6bd9bbd72 | |||
| 569b3a8760 | |||
| 79a075ef49 | |||
| c66d7f7df7 | |||
| c6d0ff3c0e | |||
| 718d60c53b | |||
| 2a573f27fe | |||
| ecbb0825e0 | |||
| b94dc4355f | |||
| 52c6c226c0 |
@@ -6,43 +6,43 @@ interface IResourceController
|
||||
{
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
* @return mixed
|
||||
*/
|
||||
public function index(): ?string;
|
||||
public function index();
|
||||
|
||||
/**
|
||||
* @param mixed $id
|
||||
* @return string|null
|
||||
* @return mixed
|
||||
*/
|
||||
public function show($id): ?string;
|
||||
public function show($id);
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
* @return mixed
|
||||
*/
|
||||
public function store(): ?string;
|
||||
public function store();
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
* @return mixed
|
||||
*/
|
||||
public function create(): ?string;
|
||||
public function create();
|
||||
|
||||
/**
|
||||
* View
|
||||
* @param mixed $id
|
||||
* @return string|null
|
||||
* @return mixed
|
||||
*/
|
||||
public function edit($id): ?string;
|
||||
public function edit($id);
|
||||
|
||||
/**
|
||||
* @param mixed $id
|
||||
* @return string|null
|
||||
* @return mixed
|
||||
*/
|
||||
public function update($id): ?string;
|
||||
public function update($id);
|
||||
|
||||
/**
|
||||
* @param mixed $id
|
||||
* @return string|null
|
||||
* @return mixed
|
||||
*/
|
||||
public function destroy($id): ?string;
|
||||
public function destroy($id);
|
||||
|
||||
}
|
||||
@@ -165,7 +165,7 @@ class InputFile implements IInputItem
|
||||
* @param string $name
|
||||
* @return static
|
||||
*/
|
||||
public function setFilename($name): IInputItem
|
||||
public function setFilename(string $name): IInputItem
|
||||
{
|
||||
$this->filename = $name;
|
||||
|
||||
@@ -188,7 +188,7 @@ class InputFile implements IInputItem
|
||||
* @param string $destination
|
||||
* @return bool
|
||||
*/
|
||||
public function move($destination): bool
|
||||
public function move(string $destination): bool
|
||||
{
|
||||
return move_uploaded_file($this->tmpName, $destination);
|
||||
}
|
||||
@@ -216,20 +216,20 @@ class InputFile implements IInputItem
|
||||
/**
|
||||
* Get upload-error code.
|
||||
*
|
||||
* @return int
|
||||
* @return int|null
|
||||
*/
|
||||
public function getError(): int
|
||||
public function getError(): ?int
|
||||
{
|
||||
return (int)$this->errors;
|
||||
return $this->errors;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set error
|
||||
*
|
||||
* @param int $error
|
||||
* @param int|null $error
|
||||
* @return static
|
||||
*/
|
||||
public function setError($error): IInputItem
|
||||
public function setError(?int $error): IInputItem
|
||||
{
|
||||
$this->errors = (int)$error;
|
||||
|
||||
@@ -249,7 +249,7 @@ class InputFile implements IInputItem
|
||||
* @param string $name
|
||||
* @return static
|
||||
*/
|
||||
public function setTmpName($name): IInputItem
|
||||
public function setTmpName(string $name): IInputItem
|
||||
{
|
||||
$this->tmpName = $name;
|
||||
|
||||
@@ -261,7 +261,7 @@ class InputFile implements IInputItem
|
||||
return $this->getTmpName();
|
||||
}
|
||||
|
||||
public function getValue()
|
||||
public function getValue(): string
|
||||
{
|
||||
return $this->getFilename();
|
||||
}
|
||||
|
||||
@@ -147,7 +147,7 @@ class InputHandler
|
||||
* @param array|null $original
|
||||
* @return array
|
||||
*/
|
||||
protected function rearrangeFile(array $values, &$index, $original): array
|
||||
protected function rearrangeFile(array $values, array &$index, ?array $original): array
|
||||
{
|
||||
$originalIndex = $index[0];
|
||||
array_shift($index);
|
||||
|
||||
@@ -50,12 +50,12 @@ class BaseCsrfVerifier implements IMiddleware
|
||||
$url = rtrim($url, '*');
|
||||
$skip = $request->getUrl()->contains($url);
|
||||
} else {
|
||||
$skip = ($url === $request->getUrl()->getOriginalUrl());
|
||||
$skip = ($url === rtrim($request->getUrl()->getRelativeUrl(false), '/'));
|
||||
}
|
||||
|
||||
if ($skip === true) {
|
||||
|
||||
if($this->include !== null && count($this->include) > 0) {
|
||||
if(is_array($this->include) === true && count($this->include) > 0) {
|
||||
foreach($this->include as $includeUrl) {
|
||||
$includeUrl = rtrim($includeUrl, '/');
|
||||
if ($includeUrl[strlen($includeUrl) - 1] === '*') {
|
||||
@@ -64,7 +64,7 @@ class BaseCsrfVerifier implements IMiddleware
|
||||
break;
|
||||
}
|
||||
|
||||
$skip = !($includeUrl === $request->getUrl()->getOriginalUrl());
|
||||
$skip = !($includeUrl === rtrim($request->getUrl()->getRelativeUrl(false), '/'));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -34,6 +34,10 @@ abstract class IpRestrictAccess implements IMiddleware
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @throws HttpException
|
||||
*/
|
||||
public function handle(Request $request): void
|
||||
{
|
||||
if($this->validate((string)$request->getIp()) === false) {
|
||||
|
||||
+14
-8
@@ -248,8 +248,9 @@ class Url implements JsonSerializable
|
||||
public function setQueryString(string $queryString): self
|
||||
{
|
||||
$params = [];
|
||||
parse_str($queryString, $params);
|
||||
|
||||
if(parse_str($queryString, $params) !== false) {
|
||||
if(count($params) > 0) {
|
||||
return $this->setParams($params);
|
||||
}
|
||||
|
||||
@@ -341,7 +342,7 @@ class Url implements JsonSerializable
|
||||
*/
|
||||
public function removeParams(...$names): self
|
||||
{
|
||||
$params = array_diff_key($this->getParams(), array_flip($names));
|
||||
$params = array_diff_key($this->getParams(), array_flip(...$names));
|
||||
$this->setParams($params);
|
||||
|
||||
return $this;
|
||||
@@ -427,14 +428,18 @@ class Url implements JsonSerializable
|
||||
/**
|
||||
* Returns the relative url
|
||||
*
|
||||
* @param bool $includeParams
|
||||
* @return string
|
||||
*/
|
||||
public function getRelativeUrl(): string
|
||||
public function getRelativeUrl($includeParams = true): string
|
||||
{
|
||||
$params = $this->getQueryString();
|
||||
$path = $this->path ?? '/';
|
||||
|
||||
$path = $this->path ?? '';
|
||||
$query = $params !== '' ? '?' . $params : '';
|
||||
if($includeParams === false) {
|
||||
return $path;
|
||||
}
|
||||
|
||||
$query = $this->getQueryString() !== '' ? '?' . $this->getQueryString() : '';
|
||||
$fragment = $this->fragment !== null ? '#' . $this->fragment : '';
|
||||
|
||||
return $path . $query . $fragment;
|
||||
@@ -443,9 +448,10 @@ class Url implements JsonSerializable
|
||||
/**
|
||||
* Returns the absolute url
|
||||
*
|
||||
* @param bool $includeParams
|
||||
* @return string
|
||||
*/
|
||||
public function getAbsoluteUrl(): string
|
||||
public function getAbsoluteUrl($includeParams = true): string
|
||||
{
|
||||
$scheme = $this->scheme !== null ? $this->scheme . '://' : '';
|
||||
$host = $this->host ?? '';
|
||||
@@ -454,7 +460,7 @@ class Url implements JsonSerializable
|
||||
$pass = $this->password !== null ? ':' . $this->password : '';
|
||||
$pass = ($user || $pass) ? $pass . '@' : '';
|
||||
|
||||
return $scheme . $user . $pass . $host . $port . $this->getRelativeUrl();
|
||||
return $scheme . $user . $pass . $host . $port . $this->getRelativeUrl($includeParams);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -55,7 +55,6 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
|
||||
public function matchRegex(Request $request, $url): ?bool
|
||||
{
|
||||
/* Match on custom defined regular expression */
|
||||
|
||||
if ($this->regex === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -76,6 +76,11 @@ abstract class Route implements IRoute
|
||||
if (is_callable($callback) === true) {
|
||||
$router->debug('Executing callback');
|
||||
|
||||
/* Load class from type hinting */
|
||||
if (is_array($callback) === true && isset($callback[0], $callback[1]) === true) {
|
||||
$callback[0] = $router->getClassLoader()->loadClass($callback[0]);
|
||||
}
|
||||
|
||||
/* When the callback is a function */
|
||||
return $router->getClassLoader()->loadClosure($callback, $parameters);
|
||||
}
|
||||
@@ -160,7 +165,7 @@ abstract class Route implements IRoute
|
||||
foreach ((array)$parameters[1] as $name) {
|
||||
|
||||
// Ignore parent parameters
|
||||
if(isset($groupParameters[$name]) === true) {
|
||||
if (isset($groupParameters[$name]) === true) {
|
||||
$lastParams[$name] = $matches[$name];
|
||||
continue;
|
||||
}
|
||||
@@ -244,6 +249,7 @@ abstract class Route implements IRoute
|
||||
$this->group = $group;
|
||||
|
||||
/* Add/merge parent settings with child */
|
||||
|
||||
return $this->setSettings($group->toArray(), true);
|
||||
}
|
||||
|
||||
@@ -331,6 +337,17 @@ abstract class Route implements IRoute
|
||||
*/
|
||||
public function setNamespace(string $namespace): IRoute
|
||||
{
|
||||
$ns = $this->getNamespace();
|
||||
|
||||
if ($ns !== null) {
|
||||
// Don't overwrite namespaces that starts with \
|
||||
if ($ns[0] !== '\\') {
|
||||
$namespace .= '\\' . $ns;
|
||||
} else {
|
||||
$namespace = $ns;
|
||||
}
|
||||
}
|
||||
|
||||
$this->namespace = $namespace;
|
||||
|
||||
return $this;
|
||||
@@ -401,7 +418,7 @@ abstract class Route implements IRoute
|
||||
*/
|
||||
public function setSettings(array $settings, bool $merge = false): IRoute
|
||||
{
|
||||
if ($this->namespace === null && isset($settings['namespace']) === true) {
|
||||
if (isset($settings['namespace']) === true) {
|
||||
$this->setNamespace($settings['namespace']);
|
||||
}
|
||||
|
||||
@@ -576,6 +593,7 @@ abstract class Route implements IRoute
|
||||
public function setFilterEmptyParams(bool $enabled): IRoute
|
||||
{
|
||||
$this->filterEmptyParams = $enabled;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
@@ -183,7 +183,6 @@ class RouteGroup extends Route implements IGroupRoute
|
||||
*/
|
||||
public function setSettings(array $settings, bool $merge = false): IRoute
|
||||
{
|
||||
|
||||
if (isset($settings['prefix']) === true) {
|
||||
$this->setPrefix($settings['prefix'] . $this->prefix);
|
||||
}
|
||||
|
||||
@@ -509,7 +509,7 @@ class Router
|
||||
]);
|
||||
|
||||
/* @var $handler IExceptionHandler */
|
||||
foreach ($this->exceptionHandlers as $key => $handler) {
|
||||
foreach (array_reverse($this->exceptionHandlers) as $key => $handler) {
|
||||
|
||||
if (is_object($handler) === false) {
|
||||
$handler = new $handler();
|
||||
|
||||
@@ -4,8 +4,8 @@
|
||||
* Router helper class
|
||||
* ---------------------------
|
||||
*
|
||||
* This class is added so calls can be made statically like Router::get() making the code look pretty.
|
||||
* It also adds some extra functionality like default-namespace.
|
||||
* This class is added so calls can be made statically like SimpleRouter::get() making the code look pretty.
|
||||
* It also adds some extra functionality like default-namespace etc.
|
||||
*/
|
||||
|
||||
namespace Pecee\SimpleRouter;
|
||||
@@ -348,7 +348,7 @@ class SimpleRouter
|
||||
* @param array|null $settings
|
||||
* @return RouteUrl|IRoute
|
||||
*/
|
||||
public static function match(array $requestMethods, string $url, $callback, array $settings = null)
|
||||
public static function match(array $requestMethods, string $url, $callback, array $settings = null): IRoute
|
||||
{
|
||||
$route = new RouteUrl($url, $callback);
|
||||
$route->setRequestMethods($requestMethods);
|
||||
@@ -368,7 +368,7 @@ class SimpleRouter
|
||||
* @param array|null $settings
|
||||
* @return RouteUrl|IRoute
|
||||
*/
|
||||
public static function all(string $url, $callback, array $settings = null)
|
||||
public static function all(string $url, $callback, array $settings = null): IRoute
|
||||
{
|
||||
$route = new RouteUrl($url, $callback);
|
||||
|
||||
@@ -387,7 +387,7 @@ class SimpleRouter
|
||||
* @param array|null $settings
|
||||
* @return RouteController|IRoute
|
||||
*/
|
||||
public static function controller(string $url, string $controller, array $settings = null)
|
||||
public static function controller(string $url, string $controller, array $settings = null): IRoute
|
||||
{
|
||||
$route = new RouteController($url, $controller);
|
||||
|
||||
@@ -406,7 +406,7 @@ class SimpleRouter
|
||||
* @param array|null $settings
|
||||
* @return RouteResource|IRoute
|
||||
*/
|
||||
public static function resource(string $url, string $controller, array $settings = null)
|
||||
public static function resource(string $url, string $controller, array $settings = null): IRoute
|
||||
{
|
||||
$route = new RouteResource($url, $controller);
|
||||
|
||||
@@ -512,20 +512,7 @@ class SimpleRouter
|
||||
public static function addDefaultNamespace(IRoute $route): IRoute
|
||||
{
|
||||
if (static::$defaultNamespace !== null) {
|
||||
|
||||
$ns = static::$defaultNamespace;
|
||||
$namespace = $route->getNamespace();
|
||||
|
||||
if ($namespace !== null) {
|
||||
// Don't overwrite namespaces that starts with \
|
||||
if ($namespace[0] !== '\\') {
|
||||
$ns .= '\\' . $namespace;
|
||||
} else {
|
||||
$ns = $namespace;
|
||||
}
|
||||
}
|
||||
|
||||
$route->setNamespace($ns);
|
||||
$route->setNamespace(static::$defaultNamespace);
|
||||
}
|
||||
|
||||
return $route;
|
||||
|
||||
+39
-3
@@ -3,20 +3,20 @@
|
||||
require_once 'Dummy/DummyMiddleware.php';
|
||||
require_once 'Dummy/DummyController.php';
|
||||
|
||||
class GroupTest extends \PHPUnit\Framework\TestCase
|
||||
class RouterGroupTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
|
||||
public function testGroupLoad()
|
||||
{
|
||||
$result = false;
|
||||
|
||||
TestRouter::group(['prefix' => '/group'], function () use(&$result) {
|
||||
TestRouter::group(['prefix' => '/group'], function () use (&$result) {
|
||||
$result = true;
|
||||
});
|
||||
|
||||
try {
|
||||
TestRouter::debug('/', 'get');
|
||||
} catch(\Exception $e) {
|
||||
} catch (\Exception $e) {
|
||||
|
||||
}
|
||||
$this->assertTrue($result);
|
||||
@@ -81,4 +81,40 @@ class GroupTest extends \PHPUnit\Framework\TestCase
|
||||
|
||||
}
|
||||
|
||||
public function testNamespaceExtend()
|
||||
{
|
||||
TestRouter::group(['namespace' => '\My\Namespace'], function () use (&$result) {
|
||||
|
||||
TestRouter::group(['namespace' => 'Service'], function () use (&$result) {
|
||||
|
||||
TestRouter::get('/test', function () use (&$result) {
|
||||
return \Pecee\SimpleRouter\SimpleRouter::router()->getRequest()->getLoadedRoute()->getNamespace();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
$namespace = TestRouter::debugOutput('/test');
|
||||
$this->assertEquals('\My\Namespace\Service', $namespace);
|
||||
}
|
||||
|
||||
public function testNamespaceOverwrite()
|
||||
{
|
||||
TestRouter::group(['namespace' => '\My\Namespace'], function () use (&$result) {
|
||||
|
||||
TestRouter::group(['namespace' => '\Service'], function () use (&$result) {
|
||||
|
||||
TestRouter::get('/test', function () use (&$result) {
|
||||
return \Pecee\SimpleRouter\SimpleRouter::router()->getRequest()->getLoadedRoute()->getNamespace();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
$namespace = TestRouter::debugOutput('/test');
|
||||
$this->assertEquals('\Service', $namespace);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -49,9 +49,9 @@ class RouterRewriteTest extends \PHPUnit\Framework\TestCase
|
||||
}
|
||||
|
||||
$expectedStack = [
|
||||
ExceptionHandlerFirst::class,
|
||||
ExceptionHandlerSecond::class,
|
||||
ExceptionHandlerThird::class,
|
||||
ExceptionHandlerSecond::class,
|
||||
ExceptionHandlerFirst::class,
|
||||
];
|
||||
|
||||
$this->assertEquals($expectedStack, $stack);
|
||||
|
||||
Reference in New Issue
Block a user