From e172214c2f6ee8fc47183436dad503d91604a684 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vojt=C4=9Bch=20Havr=C3=A1nek?= Date: Wed, 6 Dec 2023 14:57:58 +0100 Subject: [PATCH] Add possibility to call route methods from SlimRoute --- composer.json | 7 +++++-- src/Bridge/Slim/SlimRoute.php | 16 ++++++++++++++++ src/Bridge/Slim/SlimRouterNetteDiFactory.php | 6 ++---- src/Bridge/Slim/SlimRouterTest.php | 2 +- 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/composer.json b/composer.json index 754b9fc..b4151b0 100644 --- a/composer.json +++ b/composer.json @@ -7,7 +7,7 @@ } }, "require": { - "php": ">=7.2", + "php": ">=7.4", "psr/http-message": "^1.0" }, "suggest": { @@ -30,6 +30,9 @@ }, "config": { "sort-packages": true, - "process-timeout": 600 + "process-timeout": 600, + "allow-plugins": { + "dealerdirect/phpcodesniffer-composer-installer": true + } } } diff --git a/src/Bridge/Slim/SlimRoute.php b/src/Bridge/Slim/SlimRoute.php index 307b22e..9f4600e 100644 --- a/src/Bridge/Slim/SlimRoute.php +++ b/src/Bridge/Slim/SlimRoute.php @@ -5,6 +5,8 @@ use BrandEmbassy\Router\Route; use Slim\Interfaces\RouteInterface; use function assert; +use function call_user_func_array; +use function is_callable; /** * @final @@ -37,4 +39,18 @@ public function getCallable(): callable return $this->route->getCallable(); } + + + /** + * @param mixed $arguments + * + * @return mixed + */ + public function __call(string $method, $arguments) + { + $callable = [$this->route, $method]; + assert(is_callable($callable)); + + return call_user_func_array($callable, $arguments); + } } diff --git a/src/Bridge/Slim/SlimRouterNetteDiFactory.php b/src/Bridge/Slim/SlimRouterNetteDiFactory.php index 78c60d8..661f613 100644 --- a/src/Bridge/Slim/SlimRouterNetteDiFactory.php +++ b/src/Bridge/Slim/SlimRouterNetteDiFactory.php @@ -58,14 +58,12 @@ private static function createSlimRouter(Container $container, array $routes): R throw new LogicException(sprintf('Route with pattern: "%s" must have name.', $pattern)); } - $callbackProvider = function () use ($container, $data) { - return self::getService($container, $data['service']); - }; + $callbackProvider = fn() => self::getService($container, $data['service']); $route = $router->map( explode(self::METHOD_DELIMITER, $method), $pattern, - new RouteCallbackAccessor($callbackProvider) + new RouteCallbackAccessor($callbackProvider), ); $route->setName($data['name']); } diff --git a/src/Bridge/Slim/SlimRouterTest.php b/src/Bridge/Slim/SlimRouterTest.php index 8d8790e..f48dc6e 100644 --- a/src/Bridge/Slim/SlimRouterTest.php +++ b/src/Bridge/Slim/SlimRouterTest.php @@ -18,7 +18,7 @@ public function testPathFor(): void ['GET'], '/foo/{foo}/bar/{bar}', function (): void { - } + }, ); $route->setName('fooRoute'); $router = new SlimRouter($slimRouter);