Skip to content

Commit

Permalink
Replaced opis/closure due to it now seeming inactive and causing warn…
Browse files Browse the repository at this point in the history
…ing with PHP 8.4
  • Loading branch information
philipobenito committed Nov 25, 2024
1 parent fe97317 commit 38775ed
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 10 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion docs/_data/releases.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
27 changes: 19 additions & 8 deletions src/Cache/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,50 +11,61 @@
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
{
/**
* @var callable
*/
protected $builder;

/**
* @var integer
*/
protected $ttl;
protected int $ttl;

public function __construct(
callable $builder,
protected CacheInterface $cache,
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;
}
}

$builder = $this->builder;

if ($builder instanceof SerializableClosure) {
$builder = $builder->getClosure();
}

$router = $builder(new MainRouter());

if (false === $this->cacheEnabled) {
Expand All @@ -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;
}

Expand Down
9 changes: 9 additions & 0 deletions src/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -36,13 +37,21 @@ 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;
}

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);
}
Expand Down

0 comments on commit 38775ed

Please sign in to comment.