From fd585e8b9d8418aae057fc2ba27262902660b15c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Mon, 22 Mar 2021 16:41:20 +0100 Subject: [PATCH 01/22] [FEATURE] Changed behavior for default-namespace after issue #446 - Router::setDefaultNamespace() no longer has to be set in the beginning of routes.php. - Default namespace are now set once the router is started (Router::start()). - [WIP] Added unit-tests for custom-namespaces. --- src/Pecee/Http/Input/InputHandler.php | 2 +- src/Pecee/SimpleRouter/SimpleRouter.php | 38 ++++++++++------------ tests/Pecee/SimpleRouter/RouterUrlTest.php | 22 +++++++++++++ 3 files changed, 41 insertions(+), 21 deletions(-) diff --git a/src/Pecee/Http/Input/InputHandler.php b/src/Pecee/Http/Input/InputHandler.php index e485505..c6a000d 100644 --- a/src/Pecee/Http/Input/InputHandler.php +++ b/src/Pecee/Http/Input/InputHandler.php @@ -319,7 +319,7 @@ class InputHandler public function all(array $filter = []): array { $output = $this->originalParams + $this->originalPost + $this->originalFile; - $output = (\count($filter) > 0) ? array_intersect_key($output, array_flip($filter)) : $output; + $output = (\count($filter) > 0) ? \array_intersect_key($output, \array_flip($filter)) : $output; foreach ($filter as $filterKey) { if (array_key_exists($filterKey, $output) === false) { diff --git a/src/Pecee/SimpleRouter/SimpleRouter.php b/src/Pecee/SimpleRouter/SimpleRouter.php index 284078b..5d87c4c 100644 --- a/src/Pecee/SimpleRouter/SimpleRouter.php +++ b/src/Pecee/SimpleRouter/SimpleRouter.php @@ -59,6 +59,11 @@ class SimpleRouter */ public static function start(): void { + // Set default namespaces + foreach (static::router()->getRoutes() as $route) { + static::addDefaultNamespace($route); + } + echo static::router()->start(); } @@ -307,8 +312,8 @@ class SimpleRouter * @param string $url * @param string|array|\Closure $callback * @param array|null $settings - * @see SimpleRouter::form * @return RouteUrl + * @see SimpleRouter::form */ public static function basic(string $url, $callback, array $settings = null): IRoute { @@ -322,14 +327,14 @@ class SimpleRouter * @param string $url * @param string|array|\Closure $callback * @param array|null $settings - * @see SimpleRouter::form * @return RouteUrl + * @see SimpleRouter::form */ public static function form(string $url, $callback, array $settings = null): IRoute { return static::match([ Request::REQUEST_TYPE_GET, - Request::REQUEST_TYPE_POST + Request::REQUEST_TYPE_POST, ], $url, $callback, $settings); } @@ -346,7 +351,6 @@ class SimpleRouter { $route = new RouteUrl($url, $callback); $route->setRequestMethods($requestMethods); - $route = static::addDefaultNamespace($route); if ($settings !== null) { $route->setSettings($settings); @@ -366,7 +370,6 @@ class SimpleRouter public static function all(string $url, $callback, array $settings = null) { $route = new RouteUrl($url, $callback); - $route = static::addDefaultNamespace($route); if ($settings !== null) { $route->setSettings($settings); @@ -386,7 +389,6 @@ class SimpleRouter public static function controller(string $url, string $controller, array $settings = null) { $route = new RouteController($url, $controller); - $route = static::addDefaultNamespace($route); if ($settings !== null) { $route->setSettings($settings); @@ -406,7 +408,6 @@ class SimpleRouter public static function resource(string $url, string $controller, array $settings = null) { $route = new RouteResource($url, $controller); - $route = static::addDefaultNamespace($route); if ($settings !== null) { $route->setSettings($settings); @@ -511,22 +512,19 @@ class SimpleRouter { if (static::$defaultNamespace !== null) { - $callback = $route->getCallback(); + $ns = static::$defaultNamespace; + $namespace = $route->getNamespace(); - /* Only add default namespace on relative callbacks */ - if ($callback === null || (\is_string($callback) === true && $callback[0] !== '\\')) { - - $namespace = static::$defaultNamespace; - - $currentNamespace = $route->getNamespace(); - - if ($currentNamespace !== null) { - $namespace .= '\\' . $currentNamespace; + if ($namespace !== null) { + // Don't overwrite namespaces that starts with \ + if ($namespace[0] !== '\\') { + $ns .= '\\' . $namespace; + } else { + $ns = $namespace; } - - $route->setDefaultNamespace($namespace); - } + + $route->setNamespace($ns); } return $route; diff --git a/tests/Pecee/SimpleRouter/RouterUrlTest.php b/tests/Pecee/SimpleRouter/RouterUrlTest.php index 2aa4706..2d67e49 100644 --- a/tests/Pecee/SimpleRouter/RouterUrlTest.php +++ b/tests/Pecee/SimpleRouter/RouterUrlTest.php @@ -183,4 +183,26 @@ class RouterUrlTest extends \PHPUnit\Framework\TestCase $this->assertEquals('match', $output); } + public function testDefaultNamespace() + { + TestRouter::setDefaultNamespace('\\TestRoutersss'); + + TestRouter::get('/', 'DummyController@method1', ['as' => 'home']); + TestRouter::get('/about', 'DummyController@about'); + + TestRouter::group([ + 'prefix' => '/horses', + ], function() { + + TestRouter::get('/about', 'DummyController@about'); + + }); + + + TestRouter::debugNoReset('/horses/about'); + + + $routes = TestRouter::router()->getRoutes(); + } + } \ No newline at end of file From fa83d2f74bbb11db202abfe0fc1d90803164405a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Mon, 22 Mar 2021 17:03:22 +0100 Subject: [PATCH 02/22] Default-namespace changes. - Added new ClassNotFoundHttpException thrown when class is not found. - ClassNotFoundHttpException is now thrown when class/method is not found (backwards compatible). - Added unit-tests for default-namespace tests (rewrite + append cases). --- .../SimpleRouter/ClassLoader/ClassLoader.php | 3 +- .../Exceptions/ClassNotFoundHttpException.php | 38 +++++++++++++++ src/Pecee/SimpleRouter/Route/Route.php | 3 +- tests/Pecee/SimpleRouter/RouterUrlTest.php | 46 +++++++++++++++---- 4 files changed, 79 insertions(+), 11 deletions(-) create mode 100644 src/Pecee/SimpleRouter/Exceptions/ClassNotFoundHttpException.php diff --git a/src/Pecee/SimpleRouter/ClassLoader/ClassLoader.php b/src/Pecee/SimpleRouter/ClassLoader/ClassLoader.php index c928339..40cb3b6 100644 --- a/src/Pecee/SimpleRouter/ClassLoader/ClassLoader.php +++ b/src/Pecee/SimpleRouter/ClassLoader/ClassLoader.php @@ -3,6 +3,7 @@ namespace Pecee\SimpleRouter\ClassLoader; use DI\Container; +use Pecee\SimpleRouter\Exceptions\ClassNotFoundHttpException; use Pecee\SimpleRouter\Exceptions\NotFoundHttpException; class ClassLoader implements IClassLoader @@ -28,7 +29,7 @@ class ClassLoader implements IClassLoader public function loadClass(string $class) { if (class_exists($class) === false) { - throw new NotFoundHttpException(sprintf('Class "%s" does not exist', $class), 404); + throw new ClassNotFoundHttpException(sprintf('Class "%s" does not exist', $class), 404, null, $class); } if ($this->useDependencyInjection === true) { diff --git a/src/Pecee/SimpleRouter/Exceptions/ClassNotFoundHttpException.php b/src/Pecee/SimpleRouter/Exceptions/ClassNotFoundHttpException.php new file mode 100644 index 0000000..d283874 --- /dev/null +++ b/src/Pecee/SimpleRouter/Exceptions/ClassNotFoundHttpException.php @@ -0,0 +1,38 @@ +class = $class; + $this->method = $method; + } + + /** + * Get class name + * @return string + */ + public function getClass(): string + { + return $this->class; + } + + /** + * Get method + * @return string|null + */ + public function getMethod(): ?string + { + return $this->method; + } + +} \ No newline at end of file diff --git a/src/Pecee/SimpleRouter/Route/Route.php b/src/Pecee/SimpleRouter/Route/Route.php index 3e9370c..574c412 100644 --- a/src/Pecee/SimpleRouter/Route/Route.php +++ b/src/Pecee/SimpleRouter/Route/Route.php @@ -4,6 +4,7 @@ namespace Pecee\SimpleRouter\Route; use Pecee\Http\Middleware\IMiddleware; use Pecee\Http\Request; +use Pecee\SimpleRouter\Exceptions\ClassNotFoundHttpException; use Pecee\SimpleRouter\Exceptions\NotFoundHttpException; use Pecee\SimpleRouter\Router; @@ -95,7 +96,7 @@ abstract class Route implements IRoute } if (method_exists($class, $method) === false) { - throw new NotFoundHttpException(sprintf('Method "%s" does not exist in class "%s"', $method, $className), 404); + throw new ClassNotFoundHttpException(sprintf('Method "%s" does not exist in class "%s"', $method, $className), 404, null, $className, $method); } $router->debug('Executing callback'); diff --git a/tests/Pecee/SimpleRouter/RouterUrlTest.php b/tests/Pecee/SimpleRouter/RouterUrlTest.php index 2d67e49..b360973 100644 --- a/tests/Pecee/SimpleRouter/RouterUrlTest.php +++ b/tests/Pecee/SimpleRouter/RouterUrlTest.php @@ -11,7 +11,7 @@ class RouterUrlTest extends \PHPUnit\Framework\TestCase { TestRouter::get('/', 'DummyController@method1'); TestRouter::get('/page/{id?}', 'DummyController@method1'); - TestRouter::get('/test-output', function() { + TestRouter::get('/test-output', function () { return 'return value'; }); @@ -175,7 +175,7 @@ class RouterUrlTest extends \PHPUnit\Framework\TestCase { TestRouter::request()->setHost('google.com'); - TestRouter::get('/admin/', function() { + TestRouter::get('/admin/', function () { return 'match'; })->setMatch('/^\/admin\/?(.*)/i'); @@ -185,24 +185,52 @@ class RouterUrlTest extends \PHPUnit\Framework\TestCase public function testDefaultNamespace() { - TestRouter::setDefaultNamespace('\\TestRoutersss'); + TestRouter::setDefaultNamespace('\\Default\\Namespace'); TestRouter::get('/', 'DummyController@method1', ['as' => 'home']); - TestRouter::get('/about', 'DummyController@about'); TestRouter::group([ - 'prefix' => '/horses', - ], function() { + 'namespace' => 'Appended\Namespace', + 'prefix' => '/horses', + ], function () { - TestRouter::get('/about', 'DummyController@about'); + TestRouter::get('/', 'DummyController@method1'); + TestRouter::group([ + 'namespace' => '\\New\\Namespace', + 'prefix' => '/race', + ], function () { + + TestRouter::get('/', 'DummyController@method1'); + + }); }); + // Test appended namespace - TestRouter::debugNoReset('/horses/about'); + $class = null; + try { + TestRouter::debugNoReset('/horses/'); + } catch (\Pecee\SimpleRouter\Exceptions\ClassNotFoundHttpException $e) { + $class = $e->getClass(); + } - $routes = TestRouter::router()->getRoutes(); + $this->assertEquals('\\Default\\Namespace\\Appended\Namespace\\DummyController', $class); + + // Test overwritten namespace + + $class = null; + + try { + TestRouter::debugNoReset('/horses/race'); + } catch (\Pecee\SimpleRouter\Exceptions\ClassNotFoundHttpException $e) { + $class = $e->getClass(); + } + + $this->assertEquals('\\New\\Namespace\\DummyController', $class); + + TestRouter::router()->reset(); } } \ No newline at end of file From 801f1e68ccfbaa8cb704579a108dd5fd606c7b59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Mon, 22 Mar 2021 18:05:27 +0100 Subject: [PATCH 03/22] [BUGFIX] BootManager findRoute not working. - Fixed findRoute not working in BootManager as reported by issue: #448 - Added more comprehensive php-unit tests for bootmanagers including findUrl. --- README.md | 2 +- src/Pecee/SimpleRouter/Router.php | 14 +++--- tests/Pecee/SimpleRouter/BootManagerTest.php | 49 +++++++++++++++++++ .../Dummy/Managers/FindUrlBootManager.php | 26 ++++++++++ .../Dummy/Managers/TestBootManager.php | 12 ++--- tests/Pecee/SimpleRouter/EventHandlerTest.php | 5 +- 6 files changed, 90 insertions(+), 18 deletions(-) create mode 100644 tests/Pecee/SimpleRouter/BootManagerTest.php create mode 100644 tests/Pecee/SimpleRouter/Dummy/Managers/FindUrlBootManager.php diff --git a/README.md b/README.md index f528de4..c250220 100644 --- a/README.md +++ b/README.md @@ -1495,7 +1495,7 @@ class CustomRouterRules implement IRouterBootManager // If the current url matches the rewrite url, we use our custom route - if($request->getUrl()->getPath() === $url) { + if($request->getUrl()->contains($url)) { $request->setRewriteUrl($rule); } } diff --git a/src/Pecee/SimpleRouter/Router.php b/src/Pecee/SimpleRouter/Router.php index 758204f..1b0f231 100644 --- a/src/Pecee/SimpleRouter/Router.php +++ b/src/Pecee/SimpleRouter/Router.php @@ -269,6 +269,13 @@ class Router { $this->debug('Loading routes'); + $this->fireEvents(EventHandler::EVENT_LOAD_ROUTES, [ + 'routes' => $this->routes, + ]); + + /* Loop through each route-request */ + $this->processRoutes($this->routes); + $this->fireEvents(EventHandler::EVENT_BOOT, [ 'bootmanagers' => $this->bootManagers, ]); @@ -291,13 +298,6 @@ class Router $this->debug('Finished rendering bootmanager "%s"', $className); } - $this->fireEvents(EventHandler::EVENT_LOAD_ROUTES, [ - 'routes' => $this->routes, - ]); - - /* Loop through each route-request */ - $this->processRoutes($this->routes); - $this->debug('Finished loading routes'); } diff --git a/tests/Pecee/SimpleRouter/BootManagerTest.php b/tests/Pecee/SimpleRouter/BootManagerTest.php new file mode 100644 index 0000000..925ea97 --- /dev/null +++ b/tests/Pecee/SimpleRouter/BootManagerTest.php @@ -0,0 +1,49 @@ + '/about', + '/contact' => '/', + ])); + + TestRouter::debug('/contact'); + + $this->assertTrue($result); + } + + public function testFindUrlFromBootManager() + { + TestRouter::get('/', 'DummyController@method1'); + TestRouter::get('/about', 'DummyController@method2')->name('about'); + TestRouter::get('/contact', 'DummyController@method3')->name('contact'); + + $result = false; + + // Add boot-manager + TestRouter::addBootManager(new FindUrlBootManager($result)); + + TestRouter::debug('/'); + + $this->assertTrue($result); + } + +} \ No newline at end of file diff --git a/tests/Pecee/SimpleRouter/Dummy/Managers/FindUrlBootManager.php b/tests/Pecee/SimpleRouter/Dummy/Managers/FindUrlBootManager.php new file mode 100644 index 0000000..f8bdcf3 --- /dev/null +++ b/tests/Pecee/SimpleRouter/Dummy/Managers/FindUrlBootManager.php @@ -0,0 +1,26 @@ +result = &$result; + } + + /** + * Called when router loads it's routes + * + * @param \Pecee\SimpleRouter\Router $router + * @param \Pecee\Http\Request $request + */ + public function boot(\Pecee\SimpleRouter\Router $router, \Pecee\Http\Request $request): void + { + $contact = $router->findRoute('contact'); + + if($contact !== null) { + $this->result = true; + } + } +} \ No newline at end of file diff --git a/tests/Pecee/SimpleRouter/Dummy/Managers/TestBootManager.php b/tests/Pecee/SimpleRouter/Dummy/Managers/TestBootManager.php index 8516aae..1617b46 100644 --- a/tests/Pecee/SimpleRouter/Dummy/Managers/TestBootManager.php +++ b/tests/Pecee/SimpleRouter/Dummy/Managers/TestBootManager.php @@ -3,13 +3,11 @@ class TestBootManager implements \Pecee\SimpleRouter\IRouterBootManager { - protected $routes; - protected $aliasUrl; + protected $rewrite; - public function __construct(array $routes, string $aliasUrl) + public function __construct(array $rewrite) { - $this->routes = $routes; - $this->aliasUrl = $aliasUrl; + $this->rewrite = $rewrite; } /** @@ -20,11 +18,11 @@ class TestBootManager implements \Pecee\SimpleRouter\IRouterBootManager */ public function boot(\Pecee\SimpleRouter\Router $router, \Pecee\Http\Request $request): void { - foreach ($this->routes as $url) { + foreach ($this->rewrite as $url => $rewrite) { // If the current url matches the rewrite url, we use our custom route if ($request->getUrl()->contains($url) === true) { - $request->setRewriteUrl($this->aliasUrl); + $request->setRewriteUrl($rewrite); } } diff --git a/tests/Pecee/SimpleRouter/EventHandlerTest.php b/tests/Pecee/SimpleRouter/EventHandlerTest.php index 8fa873d..faaa5d9 100644 --- a/tests/Pecee/SimpleRouter/EventHandlerTest.php +++ b/tests/Pecee/SimpleRouter/EventHandlerTest.php @@ -50,8 +50,8 @@ class EventHandlerTest extends \PHPUnit\Framework\TestCase // Add boot-manager TestRouter::addBootManager(new TestBootManager([ - '/test', - ], '/')); + '/test' => '/', + ])); // Start router TestRouter::debug('/non-existing'); @@ -61,7 +61,6 @@ class EventHandlerTest extends \PHPUnit\Framework\TestCase public function testAllEvent() { - $status = false; $eventHandler = new EventHandler(); From 2fb59854be44575bad7a5012b0dda27e110e5249 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Mon, 22 Mar 2021 18:33:16 +0100 Subject: [PATCH 04/22] [BUGFIX] Issue #439: Fixed multiple request-type on same routes. --- src/Pecee/SimpleRouter/Router.php | 15 ++++++++++----- tests/Pecee/SimpleRouter/RouterRouteTest.php | 12 ++++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/Pecee/SimpleRouter/Router.php b/src/Pecee/SimpleRouter/Router.php index 758204f..d9e642b 100644 --- a/src/Pecee/SimpleRouter/Router.php +++ b/src/Pecee/SimpleRouter/Router.php @@ -262,8 +262,8 @@ class Router /** * Load routes - * @throws NotFoundHttpException * @return void + * @throws NotFoundHttpException */ public function loadRoutes(): void { @@ -350,7 +350,7 @@ class Router { $this->debug('Routing request'); - $methodNotAllowed = false; + $methodNotAllowed = null; try { $url = $this->request->getRewriteUrl() ?? $this->request->getUrl()->getPath(); @@ -370,7 +370,12 @@ class Router /* Check if request method matches */ if (\count($route->getRequestMethods()) !== 0 && \in_array($this->request->getMethod(), $route->getRequestMethods(), true) === false) { $this->debug('Method "%s" not allowed', $this->request->getMethod()); - $methodNotAllowed = true; + + // Only set method not allowed is not already set + if ($methodNotAllowed === null) { + $methodNotAllowed = true; + } + continue; } @@ -475,9 +480,9 @@ class Router /** * @param \Exception $e - * @throws HttpException - * @throws \Exception * @return string|null + * @throws \Exception + * @throws HttpException */ protected function handleException(\Exception $e): ?string { diff --git a/tests/Pecee/SimpleRouter/RouterRouteTest.php b/tests/Pecee/SimpleRouter/RouterRouteTest.php index e92bd37..c416ecc 100644 --- a/tests/Pecee/SimpleRouter/RouterRouteTest.php +++ b/tests/Pecee/SimpleRouter/RouterRouteTest.php @@ -210,4 +210,16 @@ class RouterRouteTest extends \PHPUnit\Framework\TestCase $this->assertTrue(true); } + public function testSameRoutes() + { + + TestRouter::get('/recipe', 'DummyController@method1')->name('add'); + TestRouter::post('/recipe', 'DummyController@method2')->name('edit'); + + TestRouter::debugNoReset('/recipe', 'post'); + TestRouter::debug('/recipe', 'get'); + + $this->assertTrue(true); + } + } \ No newline at end of file From f74252e8cc4ba567f07ff045ee64153ea22a04e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Mon, 22 Mar 2021 18:34:14 +0100 Subject: [PATCH 05/22] Removed newline --- tests/Pecee/SimpleRouter/RouterRouteTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Pecee/SimpleRouter/RouterRouteTest.php b/tests/Pecee/SimpleRouter/RouterRouteTest.php index c416ecc..28a7b4e 100644 --- a/tests/Pecee/SimpleRouter/RouterRouteTest.php +++ b/tests/Pecee/SimpleRouter/RouterRouteTest.php @@ -212,7 +212,6 @@ class RouterRouteTest extends \PHPUnit\Framework\TestCase public function testSameRoutes() { - TestRouter::get('/recipe', 'DummyController@method1')->name('add'); TestRouter::post('/recipe', 'DummyController@method2')->name('edit'); From 1d2e5f47d99cd0e04ff406309bb8a949de7ec2f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Mon, 22 Mar 2021 19:34:55 +0100 Subject: [PATCH 06/22] [FEATURE] Option to disable multi-route rendering - Added option to disable multi-route rendering by calling `Router::setRenderMultipleRoutes($bool)`. - Added alias for easier access `SimpleRouter::enableMultiRouteRendering($bool)`. - Added php-unit tests for multi-routing enabled and disabled. --- src/Pecee/SimpleRouter/Router.php | 43 ++++++++++++++++++---- src/Pecee/SimpleRouter/SimpleRouter.php | 12 ++++++ tests/Pecee/SimpleRouter/RouterUrlTest.php | 42 ++++++++++++++++++++- 3 files changed, 88 insertions(+), 9 deletions(-) diff --git a/src/Pecee/SimpleRouter/Router.php b/src/Pecee/SimpleRouter/Router.php index d9e642b..6f8781d 100644 --- a/src/Pecee/SimpleRouter/Router.php +++ b/src/Pecee/SimpleRouter/Router.php @@ -110,6 +110,13 @@ class Router */ protected $classLoader; + /** + * When enabled the router will render all routes that matches. + * When disabled the router will stop execution when first route is found. + * @var bool + */ + protected $renderMultipleRoutes = true; + /** * Router constructor. */ @@ -399,14 +406,21 @@ class Router 'route' => $route, ]); - $output = $route->renderRoute($this->request, $this); - if ($output !== null) { - return $output; - } + $routeOutput = $route->renderRoute($this->request, $this); - $output = $this->handleRouteRewrite($key, $url); - if ($output !== null) { - return $output; + if ($this->renderMultipleRoutes === true) { + if ($routeOutput !== null) { + return $routeOutput; + } + + $output = $this->handleRouteRewrite($key, $url); + if ($output !== null) { + return $output; + } + } else { + $output = $this->handleRouteRewrite($key, $url); + + return $output ?? $routeOutput; } } } @@ -912,4 +926,19 @@ class Router return $this->debugList; } + /** + * Changes the rendering behavior of the router. + * When enabled the router will render all routes that matches. + * When disabled the router will stop rendering at the first route that matches. + * + * @param bool $bool + * @return $this + */ + public function setRenderMultipleRoutes(bool $bool): self + { + $this->renderMultipleRoutes = $bool; + + return $this; + } + } \ No newline at end of file diff --git a/src/Pecee/SimpleRouter/SimpleRouter.php b/src/Pecee/SimpleRouter/SimpleRouter.php index 284078b..2e79055 100644 --- a/src/Pecee/SimpleRouter/SimpleRouter.php +++ b/src/Pecee/SimpleRouter/SimpleRouter.php @@ -532,6 +532,18 @@ class SimpleRouter return $route; } + /** + * Changes the rendering behavior of the router. + * When enabled the router will render all routes that matches. + * When disabled the router will stop rendering at the first route that matches. + * + * @param bool $bool + */ + public static function enableMultiRouteRendering(bool $bool): void + { + static::router()->setRenderMultipleRoutes($bool); + } + /** * Enable or disable dependency injection * diff --git a/tests/Pecee/SimpleRouter/RouterUrlTest.php b/tests/Pecee/SimpleRouter/RouterUrlTest.php index 2aa4706..8a1d1fa 100644 --- a/tests/Pecee/SimpleRouter/RouterUrlTest.php +++ b/tests/Pecee/SimpleRouter/RouterUrlTest.php @@ -11,7 +11,7 @@ class RouterUrlTest extends \PHPUnit\Framework\TestCase { TestRouter::get('/', 'DummyController@method1'); TestRouter::get('/page/{id?}', 'DummyController@method1'); - TestRouter::get('/test-output', function() { + TestRouter::get('/test-output', function () { return 'return value'; }); @@ -175,7 +175,7 @@ class RouterUrlTest extends \PHPUnit\Framework\TestCase { TestRouter::request()->setHost('google.com'); - TestRouter::get('/admin/', function() { + TestRouter::get('/admin/', function () { return 'match'; })->setMatch('/^\/admin\/?(.*)/i'); @@ -183,4 +183,42 @@ class RouterUrlTest extends \PHPUnit\Framework\TestCase $this->assertEquals('match', $output); } + public function testRenderMultipleRoutesDisabled() + { + TestRouter::router()->setRenderMultipleRoutes(false); + + $result = false; + + TestRouter::get('/', function () use (&$result) { + $result = true; + }); + + TestRouter::get('/', function () use (&$result) { + $result = false; + }); + + TestRouter::debug('/'); + + $this->assertTrue($result); + } + + public function testRenderMultipleRoutesEnabled() + { + TestRouter::router()->setRenderMultipleRoutes(true); + + $result = []; + + TestRouter::get('/', function () use (&$result) { + $result[] = 'route1'; + }); + + TestRouter::get('/', function () use (&$result) { + $result[] = 'route2'; + }); + + TestRouter::debug('/'); + + $this->assertCount(2, $result); + } + } \ No newline at end of file From 5c8ff17aeca4b36c5d78d156f7546915c5b49ed4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Mon, 22 Mar 2021 19:43:13 +0100 Subject: [PATCH 07/22] Updated documentation for information about multi-route-rendering. --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index f528de4..21dfc5a 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,7 @@ You can donate any amount of your choice by [clicking here](https://www.paypal.c - [Registering new event](#registering-new-event) - [Custom EventHandlers](#custom-eventhandlers) - [Advanced](#advanced) + - [Disable multiple route rendering](#disable-multiple-route-rendering) - [Url rewriting](#url-rewriting) - [Changing current route](#changing-current-route) - [Bootmanager: loading routes dynamically](#bootmanager-loading-routes-dynamically) @@ -1444,6 +1445,12 @@ class DatabaseDebugHandler implements IEventHandler # Advanced +## Disable multiple route rendering + +By default the router will try to execute all routes that matches a given url. To stop the router from executing any further routes any method can return a value. + +This behavior can be easily disabled by setting `SimpleRouter::enableMultiRouteRendering(false)` in your `routes.php` file. This is the same behavior as version 3 and below. + ## Url rewriting ### Changing current route From a44a93d70522800785cfad01c62f2680b63a3a52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Tue, 23 Mar 2021 00:46:17 +0100 Subject: [PATCH 08/22] [!!!][FEATURE] Removed php-di as suggested by #477 NOTE: Custom class-loader should be used to create custom integrations with frameworks like php-di. See documentation for more information. - Removed all references to php-cli from composer + code. - Added ClassLoader php-unit tests. --- composer.json | 3 +- .../SimpleRouter/ClassLoader/ClassLoader.php | 82 +------------------ .../SimpleRouter/Handlers/EventHandler.php | 2 +- src/Pecee/SimpleRouter/Route/Route.php | 3 +- src/Pecee/SimpleRouter/Router.php | 2 +- src/Pecee/SimpleRouter/SimpleRouter.php | 14 +--- tests/Pecee/SimpleRouter/ClassLoaderTest.php | 30 +++++++ .../SimpleRouter/DependencyInjectionTest.php | 53 ------------ .../Dummy/ClassLoader/CustomClassLoader.php | 14 ++++ .../SimpleRouter/Dummy/DummyController.php | 5 ++ 10 files changed, 58 insertions(+), 150 deletions(-) create mode 100644 tests/Pecee/SimpleRouter/ClassLoaderTest.php delete mode 100644 tests/Pecee/SimpleRouter/DependencyInjectionTest.php create mode 100644 tests/Pecee/SimpleRouter/Dummy/ClassLoader/CustomClassLoader.php diff --git a/composer.json b/composer.json index 5d285cd..8514aee 100644 --- a/composer.json +++ b/composer.json @@ -28,8 +28,7 @@ ], "require": { "php": ">=7.1", - "ext-json": "*", - "php-di/php-di": "^6.0" + "ext-json": "*" }, "require-dev": { "phpunit/phpunit": "^6.0", diff --git a/src/Pecee/SimpleRouter/ClassLoader/ClassLoader.php b/src/Pecee/SimpleRouter/ClassLoader/ClassLoader.php index c928339..87d2e70 100644 --- a/src/Pecee/SimpleRouter/ClassLoader/ClassLoader.php +++ b/src/Pecee/SimpleRouter/ClassLoader/ClassLoader.php @@ -2,22 +2,10 @@ namespace Pecee\SimpleRouter\ClassLoader; -use DI\Container; use Pecee\SimpleRouter\Exceptions\NotFoundHttpException; class ClassLoader implements IClassLoader { - /** - * Dependency injection enabled - * @var bool - */ - protected $useDependencyInjection = false; - - /** - * @var Container|null - */ - protected $container; - /** * Load class * @@ -27,21 +15,10 @@ class ClassLoader implements IClassLoader */ public function loadClass(string $class) { - if (class_exists($class) === false) { + if (\class_exists($class) === false) { throw new NotFoundHttpException(sprintf('Class "%s" does not exist', $class), 404); } - if ($this->useDependencyInjection === true) { - $container = $this->getContainer(); - if ($container !== null) { - try { - return $container->get($class); - } catch (\Exception $e) { - throw new NotFoundHttpException($e->getMessage(), (int)$e->getCode(), $e->getPrevious()); - } - } - } - return new $class(); } @@ -55,64 +32,7 @@ class ClassLoader implements IClassLoader */ public function loadClosure(Callable $closure, array $parameters) { - if ($this->useDependencyInjection === true) { - $container = $this->getContainer(); - if ($container !== null) { - try { - return $container->call($closure, $parameters); - } catch (\Exception $e) { - throw new NotFoundHttpException($e->getMessage(), (int)$e->getCode(), $e->getPrevious()); - } - } - } - return \call_user_func_array($closure, $parameters); } - /** - * Get dependency injector container. - * - * @return Container|null - */ - public function getContainer(): ?Container - { - return $this->container; - } - - /** - * Set the dependency-injector container. - * - * @param Container $container - * @return ClassLoader - */ - public function setContainer(Container $container): self - { - $this->container = $container; - - return $this; - } - - /** - * Enable or disable dependency injection. - * - * @param bool $enabled - * @return static - */ - public function useDependencyInjection(bool $enabled): self - { - $this->useDependencyInjection = $enabled; - - return $this; - } - - /** - * Return true if dependency injection is enabled. - * - * @return bool - */ - public function isDependencyInjectionEnabled(): bool - { - return $this->useDependencyInjection; - } - } diff --git a/src/Pecee/SimpleRouter/Handlers/EventHandler.php b/src/Pecee/SimpleRouter/Handlers/EventHandler.php index 9ac42ec..70b37b3 100644 --- a/src/Pecee/SimpleRouter/Handlers/EventHandler.php +++ b/src/Pecee/SimpleRouter/Handlers/EventHandler.php @@ -143,7 +143,7 @@ class EventHandler implements IEventHandler * Get events. * * @param string|null $name Filter events by name. - * @param array ...$names Add multiple names... + * @param array|string ...$names Add multiple names... * @return array */ public function getEvents(?string $name, ...$names): array diff --git a/src/Pecee/SimpleRouter/Route/Route.php b/src/Pecee/SimpleRouter/Route/Route.php index 3e9370c..8dc45bf 100644 --- a/src/Pecee/SimpleRouter/Route/Route.php +++ b/src/Pecee/SimpleRouter/Route/Route.php @@ -77,7 +77,6 @@ abstract class Route implements IRoute $router->debug('Executing callback'); /* When the callback is a function */ - return $router->getClassLoader()->loadClosure($callback, $parameters); } @@ -250,7 +249,7 @@ abstract class Route implements IRoute /** * Set callback * - * @param string|array\Closure $callback + * @param string|array|\Closure $callback * @return static */ public function setCallback($callback): IRoute diff --git a/src/Pecee/SimpleRouter/Router.php b/src/Pecee/SimpleRouter/Router.php index 758204f..af37c50 100644 --- a/src/Pecee/SimpleRouter/Router.php +++ b/src/Pecee/SimpleRouter/Router.php @@ -306,7 +306,7 @@ class Router * * @return string|null * @throws NotFoundHttpException - * @throws TokenMismatchException + * @throws \Pecee\Http\Middleware\Exceptions\TokenMismatchException * @throws HttpException * @throws \Exception */ diff --git a/src/Pecee/SimpleRouter/SimpleRouter.php b/src/Pecee/SimpleRouter/SimpleRouter.php index 284078b..ffea26a 100644 --- a/src/Pecee/SimpleRouter/SimpleRouter.php +++ b/src/Pecee/SimpleRouter/SimpleRouter.php @@ -10,7 +10,6 @@ namespace Pecee\SimpleRouter; -use DI\Container; use Pecee\Exceptions\InvalidArgumentException; use Pecee\Http\Middleware\BaseCsrfVerifier; use Pecee\Http\Request; @@ -533,17 +532,12 @@ class SimpleRouter } /** - * Enable or disable dependency injection - * - * @param Container $container - * @return IClassLoader + * Set custom class-loader class used. + * @param IClassLoader $classLoader */ - public static function enableDependencyInjection(Container $container): IClassLoader + public static function setCustomClassLoader(IClassLoader $classLoader): void { - return static::router() - ->getClassLoader() - ->useDependencyInjection(true) - ->setContainer($container); + static::router()->setClassLoader($classLoader); } /** diff --git a/tests/Pecee/SimpleRouter/ClassLoaderTest.php b/tests/Pecee/SimpleRouter/ClassLoaderTest.php new file mode 100644 index 0000000..79f6095 --- /dev/null +++ b/tests/Pecee/SimpleRouter/ClassLoaderTest.php @@ -0,0 +1,30 @@ +assertEquals('method3', $classLoaderClass); + $this->assertTrue($result); + + TestRouter::router()->reset(); + } + +} \ No newline at end of file diff --git a/tests/Pecee/SimpleRouter/DependencyInjectionTest.php b/tests/Pecee/SimpleRouter/DependencyInjectionTest.php deleted file mode 100644 index 8863f19..0000000 --- a/tests/Pecee/SimpleRouter/DependencyInjectionTest.php +++ /dev/null @@ -1,53 +0,0 @@ -useAutowiring(true) - ->ignorePhpDocErrors(true) - ->build(); - - TestRouter::enableDependencyInjection($container); - - $className = null; - - TestRouter::get('/', function (DummyMiddleware $url) use (&$className) { - $className = \get_class($url); - }); - - TestRouter::debug('/'); - - $this->assertEquals(DummyMiddleware::class, $className); - } - - public function testDependencyInjectionProduction() - { - $cacheDir = dirname(__DIR__, 2) . '/tmp'; - - $builder = new \DI\ContainerBuilder(); - $builder - ->enableCompilation($cacheDir) - ->writeProxiesToFile(true, $cacheDir . '/proxies') - ->ignorePhpDocErrors(true) - ->useAutowiring(true); - - $container = $builder->build(); - - TestRouter::enableDependencyInjection($container); - - $className = null; - - TestRouter::get('/', function (DummyMiddleware $url) use (&$className) { - $className = \get_class($url); - }); - - TestRouter::debug('/'); - - $this->assertEquals(DummyMiddleware::class, $className); - } -} \ No newline at end of file diff --git a/tests/Pecee/SimpleRouter/Dummy/ClassLoader/CustomClassLoader.php b/tests/Pecee/SimpleRouter/Dummy/ClassLoader/CustomClassLoader.php new file mode 100644 index 0000000..17bff54 --- /dev/null +++ b/tests/Pecee/SimpleRouter/Dummy/ClassLoader/CustomClassLoader.php @@ -0,0 +1,14 @@ + true]); + } +} \ No newline at end of file diff --git a/tests/Pecee/SimpleRouter/Dummy/DummyController.php b/tests/Pecee/SimpleRouter/Dummy/DummyController.php index 4150118..3b25e5d 100644 --- a/tests/Pecee/SimpleRouter/Dummy/DummyController.php +++ b/tests/Pecee/SimpleRouter/Dummy/DummyController.php @@ -16,6 +16,11 @@ class DummyController public function method2() { + } + + public function method3() + { + return 'method3'; } public function param($params = null) From 67211e53321b2a145dfa5b776c8553b5d9a64a4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Tue, 23 Mar 2021 01:24:17 +0100 Subject: [PATCH 09/22] Updated readme + gitignore --- .gitignore | 1 - README.md | 206 ++++++++++-------- .../SimpleRouter/ClassLoader/ClassLoader.php | 3 +- .../SimpleRouter/ClassLoader/IClassLoader.php | 12 + 4 files changed, 132 insertions(+), 90 deletions(-) diff --git a/.gitignore b/.gitignore index 8dc5dfc..6e51a97 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ composer.lock vendor/ -tests/tmp/* .idea/ \ No newline at end of file diff --git a/README.md b/README.md index 9f96203..5a4459c 100644 --- a/README.md +++ b/README.md @@ -14,8 +14,7 @@ SimpleRouter::get('/', function() { ### Support the project -If you like simple-router and wish to see the continued development and maintenance of the project, -please consider showing your support by buying me a coffee. Supporters will be listed under the credits section of this documentation. +If you like simple-router and wish to see the continued development and maintenance of the project, please consider showing your support by buying me a coffee. Supporters will be listed under the credits section of this documentation. You can donate any amount of your choice by [clicking here](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=NNX4D2RUSALCN). @@ -52,9 +51,6 @@ You can donate any amount of your choice by [clicking here](https://www.paypal.c - [Partial groups](#partial-groups) - [Form Method Spoofing](#form-method-spoofing) - [Accessing The Current Route](#accessing-the-current-route) - - [Dependency injection](#dependency-injection) - - [Enabling dependency injection](#enabling-dependency-injection) - - [More reading](#more-reading) - [Other examples](#other-examples) - [CSRF-protection](#csrf-protection) - [Adding CSRF-verifier](#adding-csrf-verifier) @@ -90,6 +86,8 @@ You can donate any amount of your choice by [clicking here](https://www.paypal.c - [Changing current route](#changing-current-route) - [Bootmanager: loading routes dynamically](#bootmanager-loading-routes-dynamically) - [Adding routes manually](#adding-routes-manually) + - [Custom class-loader](#custom-class-loader) + - [Integrating with php-di](#Integrating-with-php-di) - [Parameters](#parameters) - [Extending](#extending) - [Help and support](#help-and-support) @@ -251,6 +249,7 @@ To add `favicon.ico` to the IIS ignore-list, add the following line to the ` ``` @@ -258,6 +257,7 @@ You can also make one exception for files with some extensions: If you are using `$_SERVER['ORIG_PATH_INFO']`, you will get `\index.php\` as part of the returned value. **Example:** + ``` /index.php/test/mypage.php ``` @@ -696,88 +696,6 @@ SimpleRouter::request()->getLoadedRoute(); request()->getLoadedRoute(); ``` -## Dependency injection - -simple-router supports dependency injection using the [`php-di`](http://php-di.org/) library. - -Dependency injection allows the framework to automatically "inject" (load) classes added as parameters. This can simplify your code, as you can avoid creating new instances of objects you are using often in your `Controllers` etc. - -Here's a basic example of a controller class using dependency injection: - -```php -namespace Demo\Controllers; - -class DefaultController { - - public function login(User $user): string - { - // ... - } - -} -``` - -The example above will automatically create a new instance of the `User` from the `$user` parameter. This means that the `$user` class contains a new instance of the `User` class and we won't need to create a new instance our self. - -**WARNING:** dependency injection can have some negative impact in performance. If you experience any performance issues, we recommend disabling this functionality. - -### Enabling dependency injection - -Dependency injection is disabled per default to avoid any performance issues. - -Before enabling dependency injection, we recommend that you read the [Container configuration](http://php-di.org/doc/container-configuration.html) section of the php-di documentation. This section covers how to configure php-di to different environments and speed-up the performance. - -#### Enabling for development environment - -The example below should ONLY be used on a development environment. - -```php -// Create our new php-di container -$container = (new \DI\ContainerBuilder()) - ->useAutowiring(true) - ->build(); - -// Add our container to simple-router and enable dependency injection -SimpleRouter::enableDependencyInjection($container); -``` - -Please check the [More reading](#more-reading) section of the documentation for useful php-di links and tutorials. - -#### Enabling for production environment - -The example below compiles the injections, which can help speed up performance. - -**Note:** You should change the `$cacheDir` to a cache-storage within your project. - -```php -// Cache directory -$cacheDir = sys_get_temp_dir('simple-router'); - -// Create our new php-di container -$container = (new \DI\ContainerBuilder()) - ->enableCompilation($cacheDir) - ->writeProxiesToFile(true, $cacheDir . '/proxies') - ->useAutowiring(true) - ->build(); - -// Add our container to simple-router and enable dependency injection -SimpleRouter::enableDependencyInjection($container); -``` - -Please check the [More reading](#more-reading) section of the documentation for useful php-di links and tutorials. - -### More reading - -For more information about dependency injection, configuration and settings - we recommend that you check the php-di documentation or some of the useful links we've gathered below. - -#### Useful links - -- [php-di documentation](http://php-di.org/doc/) -- [Understanding dependency injection](http://php-di.org/doc/understanding-di.html) -- [Best practices guide](http://php-di.org/doc/best-practices.html) -- [Configuring the container](http://php-di.org/doc/container-configuration.html) -- [Definitions](http://php-di.org/doc/definition.html) - ## Other examples You can find many more examples in the `routes.php` example-file below: @@ -1543,6 +1461,120 @@ $route->setPrefix('v1'); $router->addRoute($route); ``` +## Custom class loader + +You can easily extend simple-router to support custom injection frameworks like php-di by taking advantage of the ability to add your custom class-loader. + +Class-loaders must inherit the `IClassLoader` interface. + +**Example:** + +```php +class MyCustomClassLoader implements IClassLoader +{ + /** + * Load class + * + * @param string $class + * @return object + * @throws NotFoundHttpException + */ + public function loadClass(string $class) + { + if (\class_exists($class) === false) { + throw new NotFoundHttpException(sprintf('Class "%s" does not exist', $class), 404); + } + + return new $class(); + } + + /** + * Load closure + * + * @param Callable $closure + * @param array $parameters + * @return mixed + */ + public function loadClosure(Callable $closure, array $parameters) + { + return \call_user_func_array($closure, $parameters); + } + +} +``` + +Next, we need to configure our `routes.php` so the router uses our `MyCustomClassLoader` class for loading classes. This can be done by adding the following line to your `routes.php` file. + +```php +SimpleRouter::setCustomClassLoader(new MyCustomClassLoader()); +``` + +### Integrating with php-di + +php-di support was discontinued by version 4.3, however you can easily add it again by creating your own class-loader like the example below: + +```php +class MyCustomClassLoader implements IClassLoader +{ + + protected $container; + + public function __construct() + { + // Setup php-di + // Create our new php-di container + $container = (new \DI\ContainerBuilder()) + ->useAutowiring(true) + ->build(); + } + + /** + * Load class + * + * @param string $class + * @return object + * @throws NotFoundHttpException + */ + public function loadClass(string $class) + { + if (class_exists($class) === false) { + throw new NotFoundHttpException(sprintf('Class "%s" does not exist', $class), 404); + } + + if ($this->container !== null) { + try { + return $this->container->get($class); + } catch (\Exception $e) { + throw new NotFoundHttpException($e->getMessage(), (int)$e->getCode(), $e->getPrevious()); + } + } + + return new $class(); + } + + /** + * Load closure + * + * @param Callable $closure + * @param array $parameters + * @return mixed + */ + public function loadClosure(Callable $closure, array $parameters) + { + if ($this->container !== null) { + try { + return $this->container->call($closure, $parameters); + } catch (\Exception $e) { + throw new NotFoundHttpException($e->getMessage(), (int)$e->getCode(), $e->getPrevious()); + } + } + + return \call_user_func_array($closure, $parameters); + } + +} +``` + ## Parameters This section contains advanced tips & tricks on extending the usage for parameters. diff --git a/src/Pecee/SimpleRouter/ClassLoader/ClassLoader.php b/src/Pecee/SimpleRouter/ClassLoader/ClassLoader.php index 87d2e70..8c431a2 100644 --- a/src/Pecee/SimpleRouter/ClassLoader/ClassLoader.php +++ b/src/Pecee/SimpleRouter/ClassLoader/ClassLoader.php @@ -10,7 +10,7 @@ class ClassLoader implements IClassLoader * Load class * * @param string $class - * @return mixed + * @return object * @throws NotFoundHttpException */ public function loadClass(string $class) @@ -28,7 +28,6 @@ class ClassLoader implements IClassLoader * @param Callable $closure * @param array $parameters * @return mixed - * @throws NotFoundHttpException */ public function loadClosure(Callable $closure, array $parameters) { diff --git a/src/Pecee/SimpleRouter/ClassLoader/IClassLoader.php b/src/Pecee/SimpleRouter/ClassLoader/IClassLoader.php index f9c00a4..8eb8cd1 100644 --- a/src/Pecee/SimpleRouter/ClassLoader/IClassLoader.php +++ b/src/Pecee/SimpleRouter/ClassLoader/IClassLoader.php @@ -5,8 +5,20 @@ namespace Pecee\SimpleRouter\ClassLoader; interface IClassLoader { + /** + * Called when loading class + * @param string $class + * @return object + */ public function loadClass(string $class); + /** + * Called when loading method + * + * @param callable $closure + * @param array $parameters + * @return mixed + */ public function loadClosure(Callable $closure, array $parameters); } From e721a921561cd54343765421768f502e03f0e470 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Tue, 23 Mar 2021 01:25:57 +0100 Subject: [PATCH 10/22] Updated readme --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 5a4459c..42122f2 100644 --- a/README.md +++ b/README.md @@ -1521,7 +1521,6 @@ class MyCustomClassLoader implements IClassLoader public function __construct() { - // Setup php-di // Create our new php-di container $container = (new \DI\ContainerBuilder()) ->useAutowiring(true) From 90b0747dbdca65004c062e030206f5be319ca494 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Tue, 23 Mar 2021 01:36:23 +0100 Subject: [PATCH 11/22] [BUGFIX] Add support for mixed value types in InputItem as requested by #438 --- src/Pecee/Http/Input/IInputItem.php | 2 +- src/Pecee/Http/Input/InputItem.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Pecee/Http/Input/IInputItem.php b/src/Pecee/Http/Input/IInputItem.php index b4ddef9..0c851c8 100644 --- a/src/Pecee/Http/Input/IInputItem.php +++ b/src/Pecee/Http/Input/IInputItem.php @@ -15,7 +15,7 @@ interface IInputItem public function getValue(); - public function setValue(string $value): self; + public function setValue($value): self; public function __toString(): string; diff --git a/src/Pecee/Http/Input/InputItem.php b/src/Pecee/Http/Input/InputItem.php index 19ed96d..2322bd3 100644 --- a/src/Pecee/Http/Input/InputItem.php +++ b/src/Pecee/Http/Input/InputItem.php @@ -62,10 +62,10 @@ class InputItem implements IInputItem, \IteratorAggregate /** * Set input value - * @param string $value + * @param mixed $value * @return static */ - public function setValue(string $value): IInputItem + public function setValue($value): IInputItem { $this->value = $value; From 6be9d1003c93a267636fa3e577cfc702b5a7cd89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Tue, 23 Mar 2021 14:33:25 +0100 Subject: [PATCH 12/22] Updated travis.xml --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 72c98f1..aa11649 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,4 +10,4 @@ before_script: - php composer.phar install --prefer-source --no-interaction script: - - ./vendor/bin/phpunit + - ./vendor/bin/phpunit --configuration ./phpunit.xml ./tests --teamcity From 8670af356b936abcddd659bed2e944c827800613 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Tue, 23 Mar 2021 14:37:04 +0100 Subject: [PATCH 13/22] Updated travis.yml --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index aa11649..24c3e5d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,11 +3,11 @@ sudo: false language: php php: - - 7.1 + - 7.3 before_script: - curl -sS http://getcomposer.org/installer | php - php composer.phar install --prefer-source --no-interaction script: - - ./vendor/bin/phpunit --configuration ./phpunit.xml ./tests --teamcity + - ./vendor/bin/phpunit --configuration ./phpunit.xml From 7ba864420e1ec5f631a60097e67c855925b32863 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Tue, 23 Mar 2021 14:47:11 +0100 Subject: [PATCH 14/22] Updated travis.yml --- .travis.yml | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 24c3e5d..0595845 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,11 +3,25 @@ sudo: false language: php php: - - 7.3 + - 7.2 before_script: - - curl -sS http://getcomposer.org/installer | php - - php composer.phar install --prefer-source --no-interaction + - mkdir -p _clover + - ls -al script: - - ./vendor/bin/phpunit --configuration ./phpunit.xml + - ./vendor/bin/phpunit --coverage-clover _clover/clover.xml + +install: + # Install composer packages + - travis_retry composer install --no-interaction --no-suggest + # Install coveralls.phar + - wget -c -nc --retry-connrefused --tries=0 https://github.com/php-coveralls/php-coveralls/releases/download/v2.0.0/php-coveralls.phar -O coveralls.phar + - chmod +x coveralls.phar + - php coveralls.phar --version + +after_success: + # Submit coverage report to Coveralls servers, see .coveralls.yml + - travis_retry php coveralls.phar -v + # Submit coverage report to codecov.io + - bash <(curl -s https://codecov.io/bash) \ No newline at end of file From 635b12735745859b2a9188d1a164aaaffbdd06f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Tue, 23 Mar 2021 15:06:21 +0100 Subject: [PATCH 15/22] Fixed correct return type for InputFile. --- src/Pecee/Http/Input/InputFile.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Pecee/Http/Input/InputFile.php b/src/Pecee/Http/Input/InputFile.php index 2919187..c2c345f 100644 --- a/src/Pecee/Http/Input/InputFile.php +++ b/src/Pecee/Http/Input/InputFile.php @@ -261,16 +261,16 @@ class InputFile implements IInputItem return $this->getTmpName(); } - public function getValue(): ?string + public function getValue() { return $this->getFilename(); } /** - * @param string $value + * @param mixed $value * @return static */ - public function setValue(string $value): IInputItem + public function setValue($value): IInputItem { $this->filename = $value; From 7920188956e419a38a4afa1433e0cab85bfdaac4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Tue, 23 Mar 2021 15:11:44 +0100 Subject: [PATCH 16/22] Updated travis.yml --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 0595845..540b3b2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,7 +3,7 @@ sudo: false language: php php: - - 7.2 + - 7.4 before_script: - mkdir -p _clover From df52ec3df753230410426be821f7ba43fccd51e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Tue, 23 Mar 2021 15:15:58 +0100 Subject: [PATCH 17/22] Updated travis.yml --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 540b3b2..2560aa1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,14 +3,14 @@ sudo: false language: php php: - - 7.4 + - 7.3 before_script: - mkdir -p _clover - ls -al script: - - ./vendor/bin/phpunit --coverage-clover _clover/clover.xml + - ./vendor/phpunit/phpunit/phpunit --configuration ./phpunit.xml install: # Install composer packages From db63aff668e6be90434f5dbb32e90b4bc90b49df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Tue, 23 Mar 2021 15:20:05 +0100 Subject: [PATCH 18/22] Updated php-unit to version 9 --- .phpunit.result.cache | 1 + .travis.yml | 2 +- composer.json | 2 +- phpunit.xml | 8 +------- tests/Pecee/SimpleRouter/RouterRewriteTest.php | 2 +- 5 files changed, 5 insertions(+), 10 deletions(-) create mode 100644 .phpunit.result.cache diff --git a/.phpunit.result.cache b/.phpunit.result.cache new file mode 100644 index 0000000..5deaa32 --- /dev/null +++ b/.phpunit.result.cache @@ -0,0 +1 @@ +C:37:"PHPUnit\Runner\DefaultTestResultCache":3429:{a:2:{s:7:"defects";a:0:{}s:5:"times";a:70:{s:38:"BootManagerTest::testBootManagerRoutes";d:0.007;s:43:"BootManagerTest::testFindUrlFromBootManager";d:0;s:38:"ClassLoaderTest::testCustomClassLoader";d:0;s:39:"EventHandlerTest::testAllEventTriggered";d:0.002;s:30:"EventHandlerTest::testAllEvent";d:0;s:33:"EventHandlerTest::testPrefixEvent";d:0;s:24:"GroupTest::testGroupLoad";d:0;s:26:"GroupTest::testNestedGroup";d:0;s:29:"GroupTest::testMultipleRoutes";d:0;s:19:"GroupTest::testUrls";d:0;s:26:"InputHandlerTest::testPost";d:0.001;s:25:"InputHandlerTest::testGet";d:0;s:26:"InputHandlerTest::testFile";d:0;s:27:"InputHandlerTest::testFiles";d:0;s:25:"InputHandlerTest::testAll";d:0;s:35:"MiddlewareTest::testMiddlewareFound";d:0.001;s:44:"MiddlewareTest::testNestedMiddlewareDontLoad";d:0;s:64:"RouterCallbackExceptionHandlerTest::testCallbackExceptionHandler";d:0;s:29:"RouterControllerTest::testGet";d:0.001;s:30:"RouterControllerTest::testPost";d:0;s:29:"RouterControllerTest::testPut";d:0;s:38:"RouterPartialGroupTest::testParameters";d:0;s:49:"RouterPartialGroupTest::testPartialGroupWithGroup";d:0.001;s:37:"RouterResourceTest::testResourceStore";d:0.001;s:38:"RouterResourceTest::testResourceCreate";d:0;s:37:"RouterResourceTest::testResourceIndex";d:0;s:39:"RouterResourceTest::testResourceDestroy";d:0;s:36:"RouterResourceTest::testResourceEdit";d:0;s:38:"RouterResourceTest::testResourceUpdate";d:0;s:35:"RouterResourceTest::testResourceGet";d:0;s:45:"RouteRewriteTest::testExceptionHandlerRewrite";d:0;s:45:"RouteRewriteTest::testRewriteExceptionMessage";d:0;s:41:"RouteRewriteTest::testRewriteUrlFromRoute";d:0;s:46:"RouteRewriteTest::testRewriteCallbackFromRoute";d:0;s:43:"RouteRewriteTest::testRewriteRouteFromRoute";d:0;s:39:"RouteRewriteTest::testMiddlewareRewrite";d:0;s:43:"RouterRouteTest::testOptionalCharacterRoute";d:0;s:31:"RouterRouteTest::testMultiParam";d:0;s:29:"RouterRouteTest::testNotFound";d:0;s:24:"RouterRouteTest::testGet";d:0;s:25:"RouterRouteTest::testPost";d:0;s:24:"RouterRouteTest::testPut";d:0;s:27:"RouterRouteTest::testDelete";d:0;s:37:"RouterRouteTest::testMethodNotAllowed";d:0;s:32:"RouterRouteTest::testSimpleParam";d:0;s:35:"RouterRouteTest::testPathParamRegex";d:0;s:39:"RouterRouteTest::testDomainAllowedRoute";d:0;s:42:"RouterRouteTest::testDomainNotAllowedRoute";d:0;s:26:"RouterRouteTest::testRegEx";d:0;s:41:"RouterRouteTest::testParametersWithDashes";d:0;s:42:"RouterRouteTest::testParameterDefaultValue";d:0;s:42:"RouterRouteTest::testDefaultParameterRegex";d:0;s:47:"RouterRouteTest::testDefaultParameterRegexGroup";d:0;s:30:"RouterRouteTest::testClassHint";d:0;s:31:"RouterRouteTest::testSameRoutes";d:0;s:27:"RouterUrlTest::testIssue253";d:0;s:36:"RouterUrlTest::testUnicodeCharacters";d:0;s:37:"RouterUrlTest::testOptionalParameters";d:0.001;s:30:"RouterUrlTest::testSimilarUrls";d:0;s:23:"RouterUrlTest::testUrls";d:0.001;s:30:"RouterUrlTest::testCustomRegex";d:0;s:47:"RouterUrlTest::testRenderMultipleRoutesDisabled";d:0;s:46:"RouterUrlTest::testRenderMultipleRoutesEnabled";d:0;s:35:"RouterUrlTest::testDefaultNamespace";d:0;s:46:"RouterRewriteTest::testExceptionHandlerRewrite";d:0;s:46:"RouterRewriteTest::testRewriteExceptionMessage";d:0;s:42:"RouterRewriteTest::testRewriteUrlFromRoute";d:0;s:47:"RouterRewriteTest::testRewriteCallbackFromRoute";d:0;s:44:"RouterRewriteTest::testRewriteRouteFromRoute";d:0;s:40:"RouterRewriteTest::testMiddlewareRewrite";d:0;}}} \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index 2560aa1..a5a5064 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,7 +10,7 @@ before_script: - ls -al script: - - ./vendor/phpunit/phpunit/phpunit --configuration ./phpunit.xml + - ./vendor/bin/phpunit --coverage-clover _clover/clover.xml install: # Install composer packages diff --git a/composer.json b/composer.json index 8514aee..a3f0b54 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,7 @@ "ext-json": "*" }, "require-dev": { - "phpunit/phpunit": "^6.0", + "phpunit/phpunit": "^9.0", "mockery/mockery": "^1" }, "autoload": { diff --git a/phpunit.xml b/phpunit.xml index 6e8f0c6..8aa0a67 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -9,16 +9,10 @@ convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" - stopOnFailure="false" - syntaxCheck="false"> + stopOnFailure="false"> tests/Pecee/SimpleRouter/ - - - src - - diff --git a/tests/Pecee/SimpleRouter/RouterRewriteTest.php b/tests/Pecee/SimpleRouter/RouterRewriteTest.php index c0692c1..5764638 100644 --- a/tests/Pecee/SimpleRouter/RouterRewriteTest.php +++ b/tests/Pecee/SimpleRouter/RouterRewriteTest.php @@ -6,7 +6,7 @@ require_once 'Dummy/Handler/ExceptionHandlerSecond.php'; require_once 'Dummy/Handler/ExceptionHandlerThird.php'; require_once 'Dummy/Middleware/RewriteMiddleware.php'; -class RouteRewriteTest extends \PHPUnit\Framework\TestCase +class RouterRewriteTest extends \PHPUnit\Framework\TestCase { /** From c466af556e541d35e70822b5b608022af115d0f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Tue, 23 Mar 2021 15:22:31 +0100 Subject: [PATCH 19/22] Updated travis.yml --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index a5a5064..263fec2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,7 +3,7 @@ sudo: false language: php php: - - 7.3 + - 7.4.2 before_script: - mkdir -p _clover From 57936b785747f317782e8a6a6105dfd392fd5f22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Tue, 23 Mar 2021 15:26:57 +0100 Subject: [PATCH 20/22] Reverted back php-unit version --- .phpunit.result.cache | 1 - composer.json | 2 +- phpunit.xml | 8 +++++++- 3 files changed, 8 insertions(+), 3 deletions(-) delete mode 100644 .phpunit.result.cache diff --git a/.phpunit.result.cache b/.phpunit.result.cache deleted file mode 100644 index 5deaa32..0000000 --- a/.phpunit.result.cache +++ /dev/null @@ -1 +0,0 @@ -C:37:"PHPUnit\Runner\DefaultTestResultCache":3429:{a:2:{s:7:"defects";a:0:{}s:5:"times";a:70:{s:38:"BootManagerTest::testBootManagerRoutes";d:0.007;s:43:"BootManagerTest::testFindUrlFromBootManager";d:0;s:38:"ClassLoaderTest::testCustomClassLoader";d:0;s:39:"EventHandlerTest::testAllEventTriggered";d:0.002;s:30:"EventHandlerTest::testAllEvent";d:0;s:33:"EventHandlerTest::testPrefixEvent";d:0;s:24:"GroupTest::testGroupLoad";d:0;s:26:"GroupTest::testNestedGroup";d:0;s:29:"GroupTest::testMultipleRoutes";d:0;s:19:"GroupTest::testUrls";d:0;s:26:"InputHandlerTest::testPost";d:0.001;s:25:"InputHandlerTest::testGet";d:0;s:26:"InputHandlerTest::testFile";d:0;s:27:"InputHandlerTest::testFiles";d:0;s:25:"InputHandlerTest::testAll";d:0;s:35:"MiddlewareTest::testMiddlewareFound";d:0.001;s:44:"MiddlewareTest::testNestedMiddlewareDontLoad";d:0;s:64:"RouterCallbackExceptionHandlerTest::testCallbackExceptionHandler";d:0;s:29:"RouterControllerTest::testGet";d:0.001;s:30:"RouterControllerTest::testPost";d:0;s:29:"RouterControllerTest::testPut";d:0;s:38:"RouterPartialGroupTest::testParameters";d:0;s:49:"RouterPartialGroupTest::testPartialGroupWithGroup";d:0.001;s:37:"RouterResourceTest::testResourceStore";d:0.001;s:38:"RouterResourceTest::testResourceCreate";d:0;s:37:"RouterResourceTest::testResourceIndex";d:0;s:39:"RouterResourceTest::testResourceDestroy";d:0;s:36:"RouterResourceTest::testResourceEdit";d:0;s:38:"RouterResourceTest::testResourceUpdate";d:0;s:35:"RouterResourceTest::testResourceGet";d:0;s:45:"RouteRewriteTest::testExceptionHandlerRewrite";d:0;s:45:"RouteRewriteTest::testRewriteExceptionMessage";d:0;s:41:"RouteRewriteTest::testRewriteUrlFromRoute";d:0;s:46:"RouteRewriteTest::testRewriteCallbackFromRoute";d:0;s:43:"RouteRewriteTest::testRewriteRouteFromRoute";d:0;s:39:"RouteRewriteTest::testMiddlewareRewrite";d:0;s:43:"RouterRouteTest::testOptionalCharacterRoute";d:0;s:31:"RouterRouteTest::testMultiParam";d:0;s:29:"RouterRouteTest::testNotFound";d:0;s:24:"RouterRouteTest::testGet";d:0;s:25:"RouterRouteTest::testPost";d:0;s:24:"RouterRouteTest::testPut";d:0;s:27:"RouterRouteTest::testDelete";d:0;s:37:"RouterRouteTest::testMethodNotAllowed";d:0;s:32:"RouterRouteTest::testSimpleParam";d:0;s:35:"RouterRouteTest::testPathParamRegex";d:0;s:39:"RouterRouteTest::testDomainAllowedRoute";d:0;s:42:"RouterRouteTest::testDomainNotAllowedRoute";d:0;s:26:"RouterRouteTest::testRegEx";d:0;s:41:"RouterRouteTest::testParametersWithDashes";d:0;s:42:"RouterRouteTest::testParameterDefaultValue";d:0;s:42:"RouterRouteTest::testDefaultParameterRegex";d:0;s:47:"RouterRouteTest::testDefaultParameterRegexGroup";d:0;s:30:"RouterRouteTest::testClassHint";d:0;s:31:"RouterRouteTest::testSameRoutes";d:0;s:27:"RouterUrlTest::testIssue253";d:0;s:36:"RouterUrlTest::testUnicodeCharacters";d:0;s:37:"RouterUrlTest::testOptionalParameters";d:0.001;s:30:"RouterUrlTest::testSimilarUrls";d:0;s:23:"RouterUrlTest::testUrls";d:0.001;s:30:"RouterUrlTest::testCustomRegex";d:0;s:47:"RouterUrlTest::testRenderMultipleRoutesDisabled";d:0;s:46:"RouterUrlTest::testRenderMultipleRoutesEnabled";d:0;s:35:"RouterUrlTest::testDefaultNamespace";d:0;s:46:"RouterRewriteTest::testExceptionHandlerRewrite";d:0;s:46:"RouterRewriteTest::testRewriteExceptionMessage";d:0;s:42:"RouterRewriteTest::testRewriteUrlFromRoute";d:0;s:47:"RouterRewriteTest::testRewriteCallbackFromRoute";d:0;s:44:"RouterRewriteTest::testRewriteRouteFromRoute";d:0;s:40:"RouterRewriteTest::testMiddlewareRewrite";d:0;}}} \ No newline at end of file diff --git a/composer.json b/composer.json index a3f0b54..8514aee 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,7 @@ "ext-json": "*" }, "require-dev": { - "phpunit/phpunit": "^9.0", + "phpunit/phpunit": "^6.0", "mockery/mockery": "^1" }, "autoload": { diff --git a/phpunit.xml b/phpunit.xml index 8aa0a67..6e8f0c6 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -9,10 +9,16 @@ convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" - stopOnFailure="false"> + stopOnFailure="false" + syntaxCheck="false"> tests/Pecee/SimpleRouter/ + + + src + + From dbd8d381e712f3b05aa1feea8e4919d7e198ace0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Tue, 23 Mar 2021 15:31:47 +0100 Subject: [PATCH 21/22] Updated travis.yml --- .travis.yml | 22 +++------------------- 1 file changed, 3 insertions(+), 19 deletions(-) diff --git a/.travis.yml b/.travis.yml index 263fec2..d3645cb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,27 +1,11 @@ -sudo: false - language: php php: - 7.4.2 before_script: - - mkdir -p _clover - - ls -al + - curl -sS http://getcomposer.org/installer | php + - php composer.phar install --prefer-source --no-interaction script: - - ./vendor/bin/phpunit --coverage-clover _clover/clover.xml - -install: - # Install composer packages - - travis_retry composer install --no-interaction --no-suggest - # Install coveralls.phar - - wget -c -nc --retry-connrefused --tries=0 https://github.com/php-coveralls/php-coveralls/releases/download/v2.0.0/php-coveralls.phar -O coveralls.phar - - chmod +x coveralls.phar - - php coveralls.phar --version - -after_success: - # Submit coverage report to Coveralls servers, see .coveralls.yml - - travis_retry php coveralls.phar -v - # Submit coverage report to codecov.io - - bash <(curl -s https://codecov.io/bash) \ No newline at end of file + - ./vendor/bin/phpunit \ No newline at end of file From 42633ec453318ee9b806a89358671cff1923111b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Tue, 23 Mar 2021 15:38:10 +0100 Subject: [PATCH 22/22] php-unit updates --- .travis.yml | 20 +++++++++++++++++--- composer.json | 2 +- phpunit.xml | 14 +++++++------- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/.travis.yml b/.travis.yml index d3645cb..a3d4148 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,8 +4,22 @@ php: - 7.4.2 before_script: - - curl -sS http://getcomposer.org/installer | php - - php composer.phar install --prefer-source --no-interaction + - mkdir -p _clover + - ls -al script: - - ./vendor/bin/phpunit \ No newline at end of file + - ./vendor/phpunit/phpunit/phpunit --configuration ./phpunit.xml ./tests + +install: + # Install composer packages + - travis_retry composer install --no-interaction --no-suggest + # Install coveralls.phar + - wget -c -nc --retry-connrefused --tries=0 https://github.com/php-coveralls/php-coveralls/releases/download/v2.0.0/php-coveralls.phar -O coveralls.phar + - chmod +x coveralls.phar + - php coveralls.phar --version + +after_success: + # Submit coverage report to Coveralls servers, see .coveralls.yml + - travis_retry php coveralls.phar -v + # Submit coverage report to codecov.io + - bash <(curl -s https://codecov.io/bash) \ No newline at end of file diff --git a/composer.json b/composer.json index 8514aee..4f4ee6f 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,7 @@ "ext-json": "*" }, "require-dev": { - "phpunit/phpunit": "^6.0", + "phpunit/phpunit": "^8.0", "mockery/mockery": "^1" }, "autoload": { diff --git a/phpunit.xml b/phpunit.xml index 6e8f0c6..bad4398 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -9,16 +9,16 @@ convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" - stopOnFailure="false" - syntaxCheck="false"> + stopOnFailure="false"> tests/Pecee/SimpleRouter/ - - + + src - - - + + + \ No newline at end of file