From 38775ed32d49ff1ce98d88adaa06a8d66b923436 Mon Sep 17 00:00:00 2001 From: Phil Bennett Date: Mon, 25 Nov 2024 08:10:15 +0000 Subject: [PATCH] Replaced opis/closure due to it now seeming inactive and causing warning with PHP 8.4 --- CHANGELOG.md | 5 +++++ composer.json | 2 +- docs/_data/releases.yml | 2 +- src/Cache/Router.php | 27 +++++++++++++++++++-------- src/Route.php | 9 +++++++++ 5 files changed, 35 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 660169b..0b88e47 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [6.2.0] 2024-11 + +### Changed +- Replaced opis/closure with laravel/serializable-closure and implemented throughout the handler process rather than a blanket serialisation of the router. + ## [6.1.1] 2024-11 ### Fixed diff --git a/composer.json b/composer.json index 273a54a..a4d36ec 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": "^3.6.3", "psr/simple-cache": "^3.0" }, "require-dev": { diff --git a/docs/_data/releases.yml b/docs/_data/releases.yml index 38879bb..c77442e 100644 --- a/docs/_data/releases.yml +++ b/docs/_data/releases.yml @@ -22,7 +22,7 @@ name: League\Route 6.x type: Current requires: PHP >= 8.1.0 - release: 6.1.1 - 2024-11 + release: 6.2.0 - 2024-11 support: Ongoing url: /6.x/ menu: diff --git a/src/Cache/Router.php b/src/Cache/Router.php index f1c0df5..6a01c0e 100644 --- a/src/Cache/Router.php +++ b/src/Cache/Router.php @@ -11,12 +11,11 @@ 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 { /** @@ -24,10 +23,7 @@ class Router */ protected $builder; - /** - * @var integer - */ - protected $ttl; + protected int $ttl; public function __construct( callable $builder, @@ -35,19 +31,29 @@ 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; } + /** + * @throws \Psr\SimpleCache\InvalidArgumentException + */ public function dispatch(ServerRequestInterface $request): ResponseInterface { $router = $this->buildRouter($request); return $router->dispatch($request); } + /** + * @throws \Psr\SimpleCache\InvalidArgumentException + */ protected function buildRouter(ServerRequestInterface $request): MainRouter { if (true === $this->cacheEnabled && $cache = $this->cache->get($this->cacheKey)) { - $router = u($cache, ['allowed_classes' => true]); + $router = unserialize($cache, ['allowed_classes' => true]); if ($router instanceof MainRouter) { return $router; @@ -55,6 +61,11 @@ protected function buildRouter(ServerRequestInterface $request): MainRouter } $builder = $this->builder; + + if ($builder instanceof SerializableClosure) { + $builder = $builder->getClosure(); + } + $router = $builder(new MainRouter()); if (false === $this->cacheEnabled) { @@ -63,7 +74,7 @@ protected function buildRouter(ServerRequestInterface $request): MainRouter if ($router instanceof MainRouter) { $router->prepareRoutes($request); - $this->cache->set($this->cacheKey, s($router)); + $this->cache->set($this->cacheKey, serialize($router)); return $router; } diff --git a/src/Route.php b/src/Route.php index a613dad..f4ed922 100644 --- a/src/Route.php +++ b/src/Route.php @@ -4,6 +4,7 @@ namespace League\Route; +use Laravel\SerializableClosure\SerializableClosure; use League\Route\Middleware\{MiddlewareAwareInterface, MiddlewareAwareTrait}; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; @@ -36,6 +37,10 @@ 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; } @@ -43,6 +48,10 @@ 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); }