Skip to content

Commit 38775ed

Browse files
committed
Replaced opis/closure due to it now seeming inactive and causing warning with PHP 8.4
1 parent fe97317 commit 38775ed

File tree

5 files changed

+35
-10
lines changed

5 files changed

+35
-10
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).
66

7+
## [6.2.0] 2024-11
8+
9+
### Changed
10+
- Replaced opis/closure with laravel/serializable-closure and implemented throughout the handler process rather than a blanket serialisation of the router.
11+
712
## [6.1.1] 2024-11
813

914
### Fixed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@
2222
],
2323
"require": {
2424
"php": "^8.1",
25+
"laravel/serializable-closure": "^2.0.0",
2526
"nikic/fast-route": "^1.3",
2627
"psr/container": "^2.0",
2728
"psr/http-factory": "^1.0",
2829
"psr/http-message": "^2.0",
2930
"psr/http-server-handler": "^1.0.1",
3031
"psr/http-server-middleware": "^1.0.1",
31-
"opis/closure": "^3.6.3",
3232
"psr/simple-cache": "^3.0"
3333
},
3434
"require-dev": {

docs/_data/releases.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
name: League\Route 6.x
2323
type: Current
2424
requires: PHP >= 8.1.0
25-
release: 6.1.1 - 2024-11
25+
release: 6.2.0 - 2024-11
2626
support: Ongoing
2727
url: /6.x/
2828
menu:

src/Cache/Router.php

+19-8
Original file line numberDiff line numberDiff line change
@@ -11,50 +11,61 @@
1111
namespace League\Route\Cache;
1212

1313
use InvalidArgumentException;
14+
use Laravel\SerializableClosure\SerializableClosure;
1415
use League\Route\Router as MainRouter;
1516
use Psr\Http\Message\{ResponseInterface, ServerRequestInterface};
1617
use Psr\SimpleCache\CacheInterface;
1718

18-
use function Opis\Closure\{serialize as s, unserialize as u};
19-
2019
class Router
2120
{
2221
/**
2322
* @var callable
2423
*/
2524
protected $builder;
2625

27-
/**
28-
* @var integer
29-
*/
30-
protected $ttl;
26+
protected int $ttl;
3127

3228
public function __construct(
3329
callable $builder,
3430
protected CacheInterface $cache,
3531
protected bool $cacheEnabled = true,
3632
protected string $cacheKey = 'league/route/cache'
3733
) {
34+
if (true === $this->cacheEnabled && $builder instanceof \Closure) {
35+
$builder = new SerializableClosure($builder);
36+
}
37+
3838
$this->builder = $builder;
3939
}
4040

41+
/**
42+
* @throws \Psr\SimpleCache\InvalidArgumentException
43+
*/
4144
public function dispatch(ServerRequestInterface $request): ResponseInterface
4245
{
4346
$router = $this->buildRouter($request);
4447
return $router->dispatch($request);
4548
}
4649

50+
/**
51+
* @throws \Psr\SimpleCache\InvalidArgumentException
52+
*/
4753
protected function buildRouter(ServerRequestInterface $request): MainRouter
4854
{
4955
if (true === $this->cacheEnabled && $cache = $this->cache->get($this->cacheKey)) {
50-
$router = u($cache, ['allowed_classes' => true]);
56+
$router = unserialize($cache, ['allowed_classes' => true]);
5157

5258
if ($router instanceof MainRouter) {
5359
return $router;
5460
}
5561
}
5662

5763
$builder = $this->builder;
64+
65+
if ($builder instanceof SerializableClosure) {
66+
$builder = $builder->getClosure();
67+
}
68+
5869
$router = $builder(new MainRouter());
5970

6071
if (false === $this->cacheEnabled) {
@@ -63,7 +74,7 @@ protected function buildRouter(ServerRequestInterface $request): MainRouter
6374

6475
if ($router instanceof MainRouter) {
6576
$router->prepareRoutes($request);
66-
$this->cache->set($this->cacheKey, s($router));
77+
$this->cache->set($this->cacheKey, serialize($router));
6778
return $router;
6879
}
6980

src/Route.php

+9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace League\Route;
66

7+
use Laravel\SerializableClosure\SerializableClosure;
78
use League\Route\Middleware\{MiddlewareAwareInterface, MiddlewareAwareTrait};
89
use Psr\Container\ContainerExceptionInterface;
910
use Psr\Container\NotFoundExceptionInterface;
@@ -36,13 +37,21 @@ public function __construct(
3637
protected ?RouteGroup $group = null,
3738
protected array $vars = []
3839
) {
40+
if ($handler instanceof \Closure) {
41+
$handler = new SerializableClosure($handler);
42+
}
43+
3944
$this->handler = ($handler instanceof RequestHandlerInterface) ? [$handler, 'handle'] : $handler;
4045
}
4146

4247
public function getCallable(?ContainerInterface $container = null): callable
4348
{
4449
$callable = $this->handler;
4550

51+
if ($callable instanceof SerializableClosure) {
52+
$callable = $callable->getClosure();
53+
}
54+
4655
if (is_string($callable) && str_contains($callable, '::')) {
4756
$callable = explode('::', $callable);
4857
}

0 commit comments

Comments
 (0)