mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-07-01 15:29:58 +00:00
Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 448a423d5d | |||
| df9a855579 | |||
| d6bd9bbd72 | |||
| 569b3a8760 | |||
| 79a075ef49 | |||
| c66d7f7df7 | |||
| c6d0ff3c0e | |||
| 718d60c53b |
@@ -6,43 +6,43 @@ interface IResourceController
|
|||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string|null
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function index(): ?string;
|
public function index();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $id
|
* @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
|
* View
|
||||||
* @param mixed $id
|
* @param mixed $id
|
||||||
* @return string|null
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function edit($id): ?string;
|
public function edit($id);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $id
|
* @param mixed $id
|
||||||
* @return string|null
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function update($id): ?string;
|
public function update($id);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $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
|
* @param string $name
|
||||||
* @return static
|
* @return static
|
||||||
*/
|
*/
|
||||||
public function setFilename($name): IInputItem
|
public function setFilename(string $name): IInputItem
|
||||||
{
|
{
|
||||||
$this->filename = $name;
|
$this->filename = $name;
|
||||||
|
|
||||||
@@ -188,7 +188,7 @@ class InputFile implements IInputItem
|
|||||||
* @param string $destination
|
* @param string $destination
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function move($destination): bool
|
public function move(string $destination): bool
|
||||||
{
|
{
|
||||||
return move_uploaded_file($this->tmpName, $destination);
|
return move_uploaded_file($this->tmpName, $destination);
|
||||||
}
|
}
|
||||||
@@ -216,20 +216,20 @@ class InputFile implements IInputItem
|
|||||||
/**
|
/**
|
||||||
* Get upload-error code.
|
* 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
|
* Set error
|
||||||
*
|
*
|
||||||
* @param int $error
|
* @param int|null $error
|
||||||
* @return static
|
* @return static
|
||||||
*/
|
*/
|
||||||
public function setError($error): IInputItem
|
public function setError(?int $error): IInputItem
|
||||||
{
|
{
|
||||||
$this->errors = (int)$error;
|
$this->errors = (int)$error;
|
||||||
|
|
||||||
@@ -249,7 +249,7 @@ class InputFile implements IInputItem
|
|||||||
* @param string $name
|
* @param string $name
|
||||||
* @return static
|
* @return static
|
||||||
*/
|
*/
|
||||||
public function setTmpName($name): IInputItem
|
public function setTmpName(string $name): IInputItem
|
||||||
{
|
{
|
||||||
$this->tmpName = $name;
|
$this->tmpName = $name;
|
||||||
|
|
||||||
@@ -261,7 +261,7 @@ class InputFile implements IInputItem
|
|||||||
return $this->getTmpName();
|
return $this->getTmpName();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getValue()
|
public function getValue(): string
|
||||||
{
|
{
|
||||||
return $this->getFilename();
|
return $this->getFilename();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -147,7 +147,7 @@ class InputHandler
|
|||||||
* @param array|null $original
|
* @param array|null $original
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function rearrangeFile(array $values, &$index, $original): array
|
protected function rearrangeFile(array $values, array &$index, ?array $original): array
|
||||||
{
|
{
|
||||||
$originalIndex = $index[0];
|
$originalIndex = $index[0];
|
||||||
array_shift($index);
|
array_shift($index);
|
||||||
|
|||||||
@@ -248,8 +248,9 @@ class Url implements JsonSerializable
|
|||||||
public function setQueryString(string $queryString): self
|
public function setQueryString(string $queryString): self
|
||||||
{
|
{
|
||||||
$params = [];
|
$params = [];
|
||||||
|
parse_str($queryString, $params);
|
||||||
|
|
||||||
if(parse_str($queryString, $params) !== false) {
|
if(count($params) > 0) {
|
||||||
return $this->setParams($params);
|
return $this->setParams($params);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -341,7 +342,7 @@ class Url implements JsonSerializable
|
|||||||
*/
|
*/
|
||||||
public function removeParams(...$names): self
|
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);
|
$this->setParams($params);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
|
|||||||
@@ -55,7 +55,6 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
|
|||||||
public function matchRegex(Request $request, $url): ?bool
|
public function matchRegex(Request $request, $url): ?bool
|
||||||
{
|
{
|
||||||
/* Match on custom defined regular expression */
|
/* Match on custom defined regular expression */
|
||||||
|
|
||||||
if ($this->regex === null) {
|
if ($this->regex === null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -76,6 +76,11 @@ abstract class Route implements IRoute
|
|||||||
if (is_callable($callback) === true) {
|
if (is_callable($callback) === true) {
|
||||||
$router->debug('Executing callback');
|
$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 */
|
/* When the callback is a function */
|
||||||
return $router->getClassLoader()->loadClosure($callback, $parameters);
|
return $router->getClassLoader()->loadClosure($callback, $parameters);
|
||||||
}
|
}
|
||||||
@@ -160,7 +165,7 @@ abstract class Route implements IRoute
|
|||||||
foreach ((array)$parameters[1] as $name) {
|
foreach ((array)$parameters[1] as $name) {
|
||||||
|
|
||||||
// Ignore parent parameters
|
// Ignore parent parameters
|
||||||
if(isset($groupParameters[$name]) === true) {
|
if (isset($groupParameters[$name]) === true) {
|
||||||
$lastParams[$name] = $matches[$name];
|
$lastParams[$name] = $matches[$name];
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -244,6 +249,7 @@ abstract class Route implements IRoute
|
|||||||
$this->group = $group;
|
$this->group = $group;
|
||||||
|
|
||||||
/* Add/merge parent settings with child */
|
/* Add/merge parent settings with child */
|
||||||
|
|
||||||
return $this->setSettings($group->toArray(), true);
|
return $this->setSettings($group->toArray(), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -331,6 +337,17 @@ abstract class Route implements IRoute
|
|||||||
*/
|
*/
|
||||||
public function setNamespace(string $namespace): 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;
|
$this->namespace = $namespace;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
@@ -401,7 +418,7 @@ abstract class Route implements IRoute
|
|||||||
*/
|
*/
|
||||||
public function setSettings(array $settings, bool $merge = false): 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']);
|
$this->setNamespace($settings['namespace']);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -576,6 +593,7 @@ abstract class Route implements IRoute
|
|||||||
public function setFilterEmptyParams(bool $enabled): IRoute
|
public function setFilterEmptyParams(bool $enabled): IRoute
|
||||||
{
|
{
|
||||||
$this->filterEmptyParams = $enabled;
|
$this->filterEmptyParams = $enabled;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -509,7 +509,7 @@ class Router
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
/* @var $handler IExceptionHandler */
|
/* @var $handler IExceptionHandler */
|
||||||
foreach ($this->exceptionHandlers as $key => $handler) {
|
foreach (array_reverse($this->exceptionHandlers) as $key => $handler) {
|
||||||
|
|
||||||
if (is_object($handler) === false) {
|
if (is_object($handler) === false) {
|
||||||
$handler = new $handler();
|
$handler = new $handler();
|
||||||
|
|||||||
@@ -348,7 +348,7 @@ class SimpleRouter
|
|||||||
* @param array|null $settings
|
* @param array|null $settings
|
||||||
* @return RouteUrl|IRoute
|
* @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 = new RouteUrl($url, $callback);
|
||||||
$route->setRequestMethods($requestMethods);
|
$route->setRequestMethods($requestMethods);
|
||||||
@@ -368,7 +368,7 @@ class SimpleRouter
|
|||||||
* @param array|null $settings
|
* @param array|null $settings
|
||||||
* @return RouteUrl|IRoute
|
* @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);
|
$route = new RouteUrl($url, $callback);
|
||||||
|
|
||||||
@@ -387,7 +387,7 @@ class SimpleRouter
|
|||||||
* @param array|null $settings
|
* @param array|null $settings
|
||||||
* @return RouteController|IRoute
|
* @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);
|
$route = new RouteController($url, $controller);
|
||||||
|
|
||||||
@@ -406,7 +406,7 @@ class SimpleRouter
|
|||||||
* @param array|null $settings
|
* @param array|null $settings
|
||||||
* @return RouteResource|IRoute
|
* @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);
|
$route = new RouteResource($url, $controller);
|
||||||
|
|
||||||
@@ -512,20 +512,7 @@ class SimpleRouter
|
|||||||
public static function addDefaultNamespace(IRoute $route): IRoute
|
public static function addDefaultNamespace(IRoute $route): IRoute
|
||||||
{
|
{
|
||||||
if (static::$defaultNamespace !== null) {
|
if (static::$defaultNamespace !== null) {
|
||||||
|
$route->setNamespace(static::$defaultNamespace);
|
||||||
$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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $route;
|
return $route;
|
||||||
|
|||||||
+39
-3
@@ -3,20 +3,20 @@
|
|||||||
require_once 'Dummy/DummyMiddleware.php';
|
require_once 'Dummy/DummyMiddleware.php';
|
||||||
require_once 'Dummy/DummyController.php';
|
require_once 'Dummy/DummyController.php';
|
||||||
|
|
||||||
class GroupTest extends \PHPUnit\Framework\TestCase
|
class RouterGroupTest extends \PHPUnit\Framework\TestCase
|
||||||
{
|
{
|
||||||
|
|
||||||
public function testGroupLoad()
|
public function testGroupLoad()
|
||||||
{
|
{
|
||||||
$result = false;
|
$result = false;
|
||||||
|
|
||||||
TestRouter::group(['prefix' => '/group'], function () use(&$result) {
|
TestRouter::group(['prefix' => '/group'], function () use (&$result) {
|
||||||
$result = true;
|
$result = true;
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
TestRouter::debug('/', 'get');
|
TestRouter::debug('/', 'get');
|
||||||
} catch(\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
|
|
||||||
}
|
}
|
||||||
$this->assertTrue($result);
|
$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 = [
|
$expectedStack = [
|
||||||
ExceptionHandlerFirst::class,
|
|
||||||
ExceptionHandlerSecond::class,
|
|
||||||
ExceptionHandlerThird::class,
|
ExceptionHandlerThird::class,
|
||||||
|
ExceptionHandlerSecond::class,
|
||||||
|
ExceptionHandlerFirst::class,
|
||||||
];
|
];
|
||||||
|
|
||||||
$this->assertEquals($expectedStack, $stack);
|
$this->assertEquals($expectedStack, $stack);
|
||||||
|
|||||||
Reference in New Issue
Block a user