From 6d3bf7c7a5ef4fb06fc0dd5a02d42f43b7a59f2d Mon Sep 17 00:00:00 2001 From: Jonathon Hill Date: Thu, 16 Jan 2025 10:23:40 -0500 Subject: [PATCH] Reinstate opis/closure v4 This partially reverts commit 38775ed32d49ff1ce98d88adaa06a8d66b923436. --- composer.json | 2 +- src/Cache/Router.php | 16 ++++------------ src/Route.php | 9 --------- 3 files changed, 5 insertions(+), 22 deletions(-) diff --git a/composer.json b/composer.json index a4d36ec..299442c 100644 --- a/composer.json +++ b/composer.json @@ -22,13 +22,13 @@ ], "require": { "php": "^8.1", - "laravel/serializable-closure": "^2.0.0", "nikic/fast-route": "^1.3", "psr/container": "^2.0", "psr/http-factory": "^1.0", "psr/http-message": "^2.0", "psr/http-server-handler": "^1.0.1", "psr/http-server-middleware": "^1.0.1", + "opis/closure": "^4.0", "psr/simple-cache": "^3.0" }, "require-dev": { diff --git a/src/Cache/Router.php b/src/Cache/Router.php index 6a01c0e..40289f5 100644 --- a/src/Cache/Router.php +++ b/src/Cache/Router.php @@ -11,11 +11,12 @@ namespace League\Route\Cache; use InvalidArgumentException; -use Laravel\SerializableClosure\SerializableClosure; use League\Route\Router as MainRouter; use Psr\Http\Message\{ResponseInterface, ServerRequestInterface}; use Psr\SimpleCache\CacheInterface; +use function Opis\Closure\{serialize as s, unserialize as u}; + class Router { /** @@ -31,10 +32,6 @@ public function __construct( protected bool $cacheEnabled = true, protected string $cacheKey = 'league/route/cache' ) { - if (true === $this->cacheEnabled && $builder instanceof \Closure) { - $builder = new SerializableClosure($builder); - } - $this->builder = $builder; } @@ -53,7 +50,7 @@ public function dispatch(ServerRequestInterface $request): ResponseInterface protected function buildRouter(ServerRequestInterface $request): MainRouter { if (true === $this->cacheEnabled && $cache = $this->cache->get($this->cacheKey)) { - $router = unserialize($cache, ['allowed_classes' => true]); + $router = u($cache, ['allowed_classes' => true]); if ($router instanceof MainRouter) { return $router; @@ -61,11 +58,6 @@ protected function buildRouter(ServerRequestInterface $request): MainRouter } $builder = $this->builder; - - if ($builder instanceof SerializableClosure) { - $builder = $builder->getClosure(); - } - $router = $builder(new MainRouter()); if (false === $this->cacheEnabled) { @@ -74,7 +66,7 @@ protected function buildRouter(ServerRequestInterface $request): MainRouter if ($router instanceof MainRouter) { $router->prepareRoutes($request); - $this->cache->set($this->cacheKey, serialize($router)); + $this->cache->set($this->cacheKey, s($router)); return $router; } diff --git a/src/Route.php b/src/Route.php index f4ed922..a613dad 100644 --- a/src/Route.php +++ b/src/Route.php @@ -4,7 +4,6 @@ namespace League\Route; -use Laravel\SerializableClosure\SerializableClosure; use League\Route\Middleware\{MiddlewareAwareInterface, MiddlewareAwareTrait}; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; @@ -37,10 +36,6 @@ public function __construct( protected ?RouteGroup $group = null, protected array $vars = [] ) { - if ($handler instanceof \Closure) { - $handler = new SerializableClosure($handler); - } - $this->handler = ($handler instanceof RequestHandlerInterface) ? [$handler, 'handle'] : $handler; } @@ -48,10 +43,6 @@ public function getCallable(?ContainerInterface $container = null): callable { $callable = $this->handler; - if ($callable instanceof SerializableClosure) { - $callable = $callable->getClosure(); - } - if (is_string($callable) && str_contains($callable, '::')) { $callable = explode('::', $callable); }