Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reinstate opis/closure v4 #351

Open
wants to merge 1 commit into
base: 6.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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": "^4.0",
"psr/simple-cache": "^3.0"
},
"require-dev": {
Expand Down
16 changes: 4 additions & 12 deletions src/Cache/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
/**
Expand All @@ -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;
}

Expand All @@ -53,19 +50,14 @@ 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;
}
}

$builder = $this->builder;

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

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

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

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