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 1/2] [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 2/2] 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