Skip to content

Commit

Permalink
Remove oscarotero/middleland - it triggers deprecations on PHP 8.4
Browse files Browse the repository at this point in the history
  • Loading branch information
fisharebest committed Dec 17, 2024
1 parent 08b85f5 commit 04f1956
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 215 deletions.
81 changes: 81 additions & 0 deletions app/Http/Dispatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

/**
* webtrees: online genealogy
* Copyright (C) 2023 webtrees development team
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

declare(strict_types=1);

namespace Fisharebest\Webtrees\Http;

use Fisharebest\Webtrees\Http\RequestHandlers\NotFound;
use Fisharebest\Webtrees\Registry;
use LogicException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

use function array_reduce;
use function array_reverse;
use function is_string;

readonly class Dispatcher
{
/**
* @param list<class-string|MiddlewareInterface> $middleware
*/
public static function dispatch(array $middleware, ServerRequestInterface $request): ResponseInterface
{
$pipeline = array_reduce(
array: array_reverse(array: $middleware),
callback: self::reduceMiddleware(...),
initial: new NotFound(),
);

return $pipeline->handle(request: $request);
}

/**
* @param class-string|MiddlewareInterface $item
*/
private static function reduceMiddleware(RequestHandlerInterface $carry, string|MiddlewareInterface $item): RequestHandlerInterface
{
return new readonly class (carry: $carry, item: $item) implements RequestHandlerInterface {
/**
* @param class-string|MiddlewareInterface $item
*/
public function __construct(
private RequestHandlerInterface $carry,
private string|MiddlewareInterface $item,
) {
}

public function handle(ServerRequestInterface $request): ResponseInterface
{
$item = $this->item;

if (is_string($item)) {
$item = Registry::container()->get(id: $item);
}

if ($item instanceof MiddlewareInterface) {
return $item->process(request: $request, handler: $this->carry);
}

throw new LogicException(message: 'Invalid or undefined middleware');
}
};
}
}
56 changes: 12 additions & 44 deletions app/Http/Middleware/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@
use Aura\Router\Rule\Accepts;
use Aura\Router\Rule\Allows;
use Fig\Http\Message\StatusCodeInterface;
use Fisharebest\Webtrees\Http\Dispatcher;
use Fisharebest\Webtrees\Registry;
use Fisharebest\Webtrees\Services\ModuleService;
use Fisharebest\Webtrees\Services\TreeService;
use Fisharebest\Webtrees\Tree;
use Fisharebest\Webtrees\Validator;
use Fisharebest\Webtrees\Webtrees;
use Middleland\Dispatcher;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
Expand All @@ -43,35 +42,15 @@
/**
* Simple class to help migrate to a third-party routing library.
*/
class Router implements MiddlewareInterface
readonly class Router implements MiddlewareInterface
{
private ModuleService $module_service;

private RouterContainer $router_container;

private TreeService $tree_service;

/**
* @param ModuleService $module_service
* @param RouterContainer $router_container
* @param TreeService $tree_service
*/
public function __construct(
ModuleService $module_service,
RouterContainer $router_container,
TreeService $tree_service
private ModuleService $module_service,
private RouterContainer $router_container,
private TreeService $tree_service
) {
$this->module_service = $module_service;
$this->router_container = $router_container;
$this->tree_service = $tree_service;
}

/**
* @param ServerRequestInterface $request
* @param RequestHandlerInterface $handler
*
* @return ResponseInterface
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
// Ugly URLs store the path in a query parameter.
Expand Down Expand Up @@ -116,31 +95,22 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
}
}

// Not found
return $handler->handle($request);
}

// Add the route as attribute of the request
$request = $request->withAttribute('route', $route);

// This middleware cannot run until after the routing, as it needs to know the route.
$post_routing_middleware = [CheckCsrf::class];

// Firstly, apply the route middleware
$route_middleware = $route->extras['middleware'] ?? [];

// Secondly, apply any module middleware
$module_middleware = $this->module_service->findByInterface(MiddlewareInterface::class)->all();

// Finally, run the handler using middleware
$handler_middleware = [RequestHandler::class];

$middleware = array_merge(
$post_routing_middleware,
$route_middleware,
$module_middleware,
$handler_middleware
);
$middleware = [
...$route_middleware,
CheckCsrf::class,
...$module_middleware,
RequestHandler::class,
];

// Add the matched attributes to the request.
foreach ($route->attributes as $key => $value) {
Expand All @@ -163,8 +133,6 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
// Bind the updated request into the container
Registry::container()->set(ServerRequestInterface::class, $request);

$dispatcher = new Dispatcher($middleware, Registry::container());

return $dispatcher->dispatch($request);
return Dispatcher::dispatch(middleware: $middleware, request: $request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,29 @@

declare(strict_types=1);

namespace Fisharebest\Webtrees\Http\Middleware;
namespace Fisharebest\Webtrees\Http\RequestHandlers;

use Fig\Http\Message\RequestMethodInterface;
use Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException;
use Fisharebest\Webtrees\Http\RequestHandlers\HomePage;
use Fisharebest\Webtrees\Http\ViewResponseTrait;
use Fisharebest\Webtrees\Registry;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

use function redirect;
use function route;

/**
* Middleware to generate a response when no route was matched.
*/
class NoRouteFound implements MiddlewareInterface
class NotFound implements RequestHandlerInterface
{
use ViewResponseTrait;

/**
* @param ServerRequestInterface $request
* @param RequestHandlerInterface $handler
*
* @return ResponseInterface
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
public function handle(ServerRequestInterface $request): ResponseInterface
{
// Bind the request into the container. We'll need it to generate an error page.
// Need the request to generate a route/error page.
Registry::container()->set(ServerRequestInterface::class, $request);

if ($request->getMethod() !== RequestMethodInterface::METHOD_GET) {
throw new HttpNotFoundException();
}

return redirect(route(HomePage::class));
return redirect(url: route(route_name: HomePage::class));
}
}
8 changes: 2 additions & 6 deletions app/Webtrees.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
use Fisharebest\Webtrees\Factories\TimestampFactory;
use Fisharebest\Webtrees\Factories\XrefFactory;
use Fisharebest\Webtrees\GedcomFilters\GedcomEncodingFilter;
use Fisharebest\Webtrees\Http\Dispatcher;
use Fisharebest\Webtrees\Http\Middleware\BadBotBlocker;
use Fisharebest\Webtrees\Http\Middleware\BaseUrl;
use Fisharebest\Webtrees\Http\Middleware\BootModules;
Expand All @@ -61,7 +62,6 @@
use Fisharebest\Webtrees\Http\Middleware\ErrorHandler;
use Fisharebest\Webtrees\Http\Middleware\HandleExceptions;
use Fisharebest\Webtrees\Http\Middleware\LoadRoutes;
use Fisharebest\Webtrees\Http\Middleware\NoRouteFound;
use Fisharebest\Webtrees\Http\Middleware\PublicFiles;
use Fisharebest\Webtrees\Http\Middleware\ReadConfigIni;
use Fisharebest\Webtrees\Http\Middleware\RegisterGedcomTags;
Expand All @@ -73,7 +73,6 @@
use Fisharebest\Webtrees\Http\Middleware\UseSession;
use Fisharebest\Webtrees\Http\Middleware\UseTheme;
use Fisharebest\Webtrees\Http\Middleware\UseTransaction;
use Middleland\Dispatcher;
use Nyholm\Psr7\Factory\Psr17Factory;
use Nyholm\Psr7Server\ServerRequestCreator;
use Psr\Http\Message\ResponseFactoryInterface;
Expand Down Expand Up @@ -174,7 +173,6 @@ class Webtrees
RegisterGedcomTags::class,
BootModules::class,
Router::class,
NoRouteFound::class,
];

public static function new(): self
Expand Down Expand Up @@ -275,8 +273,6 @@ public function httpRequest(): ResponseInterface

$request = $server_request_creator->fromGlobals();

$dispatcher = new Dispatcher(self::MIDDLEWARE, Registry::container());

return $dispatcher->dispatch($request);
return Dispatcher::dispatch(middleware: self::MIDDLEWARE, request: $request);
}
}
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
"nesbot/carbon": "3.8.2",
"nyholm/psr7": "1.8.2",
"nyholm/psr7-server": "1.1.0",
"oscarotero/middleland": "1.0.1",
"psr/cache": "3.0.0",
"psr/http-message": "1.1",
"psr/http-server-handler": "1.0.2",
Expand Down
Loading

0 comments on commit 04f1956

Please sign in to comment.