From cca5725fe452ca1425ebf9708c2c114dc14f0b78 Mon Sep 17 00:00:00 2001 From: Sami Mazouz Date: Fri, 11 Aug 2023 14:20:26 +0100 Subject: [PATCH] chore: adapt extenders --- framework/core/src/Extend/ApiController.php | 10 ++-- .../src/Extend/Concerns/ExtendsRoutes.php | 51 +++++++++++++++++ framework/core/src/Extend/Formatter.php | 6 +- framework/core/src/Extend/Frontend.php | 48 ++++------------ framework/core/src/Extend/Routes.php | 57 ++++++------------- framework/core/src/Extend/ThrottleApi.php | 6 +- 6 files changed, 89 insertions(+), 89 deletions(-) create mode 100644 framework/core/src/Extend/Concerns/ExtendsRoutes.php diff --git a/framework/core/src/Extend/ApiController.php b/framework/core/src/Extend/ApiController.php index 8ee6f58c9..ee31ef16d 100644 --- a/framework/core/src/Extend/ApiController.php +++ b/framework/core/src/Extend/ApiController.php @@ -16,7 +16,7 @@ use Flarum\Foundation\ContainerUtil; use Illuminate\Contracts\Container\Container; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Relations\Relation; -use Psr\Http\Message\ServerRequestInterface; +use Illuminate\Http\Request; use Tobscure\JsonApi\Document; class ApiController implements ExtenderInterface @@ -63,12 +63,12 @@ class ApiController implements ExtenderInterface /** * @template S of AbstractSerializeController - * @param (callable(S $controller, mixed $data, ServerRequestInterface $request, Document $document): array)|class-string $callback + * @param (callable(S $controller, mixed $data, Request $request, Document $document): array)|class-string $callback * * The callback can be a closure or an invokable class, and should accept: * - $controller: An instance of this controller. * - $data: Mixed, can be an array of data or an object (like an instance of Collection or AbstractModel). - * - $request: An instance of \Psr\Http\Message\ServerRequestInterface. + * - $request: An instance of \Illuminate\Http\Request. * - $document: An instance of \Tobscure\JsonApi\Document. * * The callable should return: @@ -330,11 +330,11 @@ class ApiController implements ExtenderInterface * * @param string $relation: Relationship name, see load method description. * @template R of Relation - * @param (callable(Builder|R, \Psr\Http\Message\ServerRequestInterface|null, array): void) $callback + * @param (callable(Builder|R, \Illuminate\Http\Request|null, array): void) $callback * * The callback to modify the query, should accept: * - \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Relations\Relation $query: A query object. - * - \Psr\Http\Message\ServerRequestInterface|null $request: An instance of the request. + * - \Illuminate\Http\Request|null $request: An instance of the request. * - array $relations: An array of relations that are to be loaded. * * @return self diff --git a/framework/core/src/Extend/Concerns/ExtendsRoutes.php b/framework/core/src/Extend/Concerns/ExtendsRoutes.php new file mode 100644 index 000000000..e3371cc04 --- /dev/null +++ b/framework/core/src/Extend/Concerns/ExtendsRoutes.php @@ -0,0 +1,51 @@ +routes) && empty($this->removedRoutes)) { + return; + } + + $container->make(Application::class)->booted( + function (Container $container) { + /** @var RouteHandlerFactory $factory */ + $factory = $container->make(RouteHandlerFactory::class); + /** @var Router $router */ + $router = $container->make(Router::class); + /** @var Config $config */ + + foreach ($this->removedRoutes as $routeName) { + $router->forgetRoute($routeName); + } + + foreach ($this->routes as $route) { + if ($router->has($route['name'])) { + throw new \RuntimeException("Route name '{$route['name']}' is already in use."); + } + + $action = isset($route['handler']) + ? $factory->toController($route['handler']) + : $factory->toFrontend($this->frontend, $route['content']); + + $router + ->addRoute($route['method'], $route['path'], $action) + ->prefix($config->path($this->frontend)) + ->name($route['name']); + } + } + ); + } +} diff --git a/framework/core/src/Extend/Formatter.php b/framework/core/src/Extend/Formatter.php index 4e3f24964..48db7217b 100644 --- a/framework/core/src/Extend/Formatter.php +++ b/framework/core/src/Extend/Formatter.php @@ -13,7 +13,7 @@ use Flarum\Extension\Extension; use Flarum\Formatter\Formatter as ActualFormatter; use Flarum\Foundation\ContainerUtil; use Illuminate\Contracts\Container\Container; -use Psr\Http\Message\ServerRequestInterface; +use Illuminate\Http\Request; use s9e\TextFormatter\Configurator; use s9e\TextFormatter\Parser; use s9e\TextFormatter\Renderer; @@ -96,13 +96,13 @@ class Formatter implements ExtenderInterface, LifecycleInterface * Prepare the system for rendering. This can be used to modify the xml that will be rendered, or to modify the renderer. * Please note that the xml to be rendered must be returned, regardless of whether it's changed. * - * @param (callable(Renderer $renderer, mixed $context, string $xml, ServerRequestInterface $request): string)|class-string $callback + * @param (callable(Renderer $renderer, mixed $context, string $xml, Request $request): string)|class-string $callback * * The callback can be a closure or invokable class, and should accept: * - \s9e\TextFormatter\Renderer $renderer * - mixed $context * - string $xml: The xml to be rendered. - * - ServerRequestInterface $request. This argument MUST either be nullable, or omitted entirely. + * - Request $request. This argument MUST either be nullable, or omitted entirely. * * The callback should return: * - string $xml: The xml to be rendered. diff --git a/framework/core/src/Extend/Frontend.php b/framework/core/src/Extend/Frontend.php index 67a204ffa..3edf1b535 100644 --- a/framework/core/src/Extend/Frontend.php +++ b/framework/core/src/Extend/Frontend.php @@ -9,6 +9,7 @@ namespace Flarum\Extend; +use Flarum\Extend\Concerns\ExtendsRoutes; use Flarum\Extension\Event\Disabled; use Flarum\Extension\Event\Enabled; use Flarum\Extension\Extension; @@ -20,19 +21,17 @@ use Flarum\Frontend\Document; use Flarum\Frontend\Driver\TitleDriverInterface; use Flarum\Frontend\Frontend as ActualFrontend; use Flarum\Frontend\RecompileFrontendAssets; -use Flarum\Http\RouteCollection; -use Flarum\Http\RouteHandlerFactory; use Flarum\Locale\LocaleManager; use Flarum\Settings\Event\Saved; use Illuminate\Contracts\Container\Container; -use Psr\Http\Message\ServerRequestInterface; +use Illuminate\Http\Request; class Frontend implements ExtenderInterface { + use ExtendsRoutes; + private array $css = []; private ?string $js = null; - private array $routes = []; - private array $removedRoutes = []; private array $content = []; private array $preloadArrs = []; private ?string $titleDriver = null; @@ -91,11 +90,11 @@ class Frontend implements ExtenderInterface * * @param string $path: The path of the route. * @param string $name: The name of the route, must be unique. - * @param (callable(Document $document, ServerRequestInterface $request): void)|class-string|null $content + * @param (callable(Document $document, Request $request): void)|class-string|null $content * * The content can be a closure or an invokable class, and should accept: * - \Flarum\Frontend\Document $document - * - \Psr\Http\Message\ServerRequestInterface $request + * - \Illuminate\Http\Request $request * * The callable should return void. * @@ -103,7 +102,9 @@ class Frontend implements ExtenderInterface */ public function route(string $path, string $name, callable|string $content = null): self { - $this->routes[] = compact('path', 'name', 'content'); + $method = 'GET'; + + $this->routes[] = compact('method', 'path', 'name', 'content'); return $this; } @@ -125,11 +126,11 @@ class Frontend implements ExtenderInterface /** * Modify the content of the frontend. * - * @param (callable(Document $document, ServerRequestInterface $request): void)|class-string|null $callback + * @param (callable(Document $document, Request $request): void)|class-string|null $callback * * The content can be a closure or an invokable class, and should accept: * - \Flarum\Frontend\Document $document - * - \Psr\Http\Message\ServerRequestInterface $request + * - \Illuminate\Http\Request $request * * The callable should return void. * @@ -266,33 +267,6 @@ class Frontend implements ExtenderInterface } } - private function registerRoutes(Container $container): void - { - if (empty($this->routes) && empty($this->removedRoutes)) { - return; - } - - $container->resolving( - "flarum.{$this->frontend}.routes", - function (RouteCollection $collection, Container $container) { - /** @var RouteHandlerFactory $factory */ - $factory = $container->make(RouteHandlerFactory::class); - - foreach ($this->removedRoutes as $routeName) { - $collection->removeRoute($routeName); - } - - foreach ($this->routes as $route) { - $collection->get( - $route['path'], - $route['name'], - $factory->toFrontend($this->frontend, $route['content']) - ); - } - } - ); - } - private function registerContent(Container $container): void { if (empty($this->content)) { diff --git a/framework/core/src/Extend/Routes.php b/framework/core/src/Extend/Routes.php index fd1b5950c..a7b9eaf76 100644 --- a/framework/core/src/Extend/Routes.php +++ b/framework/core/src/Extend/Routes.php @@ -9,22 +9,20 @@ namespace Flarum\Extend; +use Flarum\Extend\Concerns\ExtendsRoutes; use Flarum\Extension\Extension; -use Flarum\Http\RouteCollection; -use Flarum\Http\RouteHandlerFactory; +use Flarum\Http\Controller\AbstractController; use Illuminate\Contracts\Container\Container; -use Psr\Http\Server\RequestHandlerInterface; class Routes implements ExtenderInterface { - private array $routes = []; - private array $removedRoutes = []; + use ExtendsRoutes; /** - * @param string $appName: Name of the app (api, forum, admin). + * @param string $frontend: Name of the app (api, forum, admin). */ public function __construct( - private readonly string $appName + private readonly string $frontend ) { } @@ -33,9 +31,9 @@ class Routes implements ExtenderInterface * * @param string $path: The path of the route * @param string $name: The name of the route, must be unique. - * @param callable|class-string $handler: ::class attribute of the controller class, or a closure. + * @param callable|class-string $handler: ::class attribute of the controller class, or a closure. * - * If the handler is a controller class, it should implement \Psr\Http\Server\RequestHandlerInterface, + * If the handler is a controller class, it should extend \Flarum\Http\Controller, * or extend one of the Flarum Api controllers within \Flarum\Api\Controller. * * The handler should accept: @@ -57,9 +55,9 @@ class Routes implements ExtenderInterface * * @param string $path: The path of the route * @param string $name: The name of the route, must be unique. - * @param callable|class-string $handler: ::class attribute of the controller class, or a closure. + * @param callable|class-string $handler: ::class attribute of the controller class, or a closure. * - * If the handler is a controller class, it should implement \Psr\Http\Server\RequestHandlerInterface, + * If the handler is a controller class, it should extend \Flarum\Http\Controller, * or extend one of the Flarum Api controllers within \Flarum\Api\Controller. * * The handler should accept: @@ -81,9 +79,9 @@ class Routes implements ExtenderInterface * * @param string $path: The path of the route * @param string $name: The name of the route, must be unique. - * @param callable|class-string $handler: ::class attribute of the controller class, or a closure. + * @param callable|class-string $handler: ::class attribute of the controller class, or a closure. * - * If the handler is a controller class, it should implement \Psr\Http\Server\RequestHandlerInterface, + * If the handler is a controller class, it should extend \Flarum\Http\Controller, * or extend one of the Flarum Api controllers within \Flarum\Api\Controller. * * The handler should accept: @@ -105,9 +103,9 @@ class Routes implements ExtenderInterface * * @param string $path: The path of the route * @param string $name: The name of the route, must be unique. - * @param callable|class-string $handler: ::class attribute of the controller class, or a closure. + * @param callable|class-string $handler: ::class attribute of the controller class, or a closure. * - * If the handler is a controller class, it should implement \Psr\Http\Server\RequestHandlerInterface, + * If the handler is a controller class, it should extend \Flarum\Http\Controller, * or extend one of the Flarum Api controllers within \Flarum\Api\Controller. * * The handler should accept: @@ -129,9 +127,9 @@ class Routes implements ExtenderInterface * * @param string $path: The path of the route * @param string $name: The name of the route, must be unique. - * @param callable|class-string $handler: ::class attribute of the controller class, or a closure. + * @param callable|class-string $handler: ::class attribute of the controller class, or a closure. * - * If the handler is a controller class, it should implement \Psr\Http\Server\RequestHandlerInterface, + * If the handler is a controller class, it should extend \Flarum\Http\Controller, * or extend one of the Flarum Api controllers within \Flarum\Api\Controller. * * The handler should accept: @@ -176,29 +174,6 @@ class Routes implements ExtenderInterface public function extend(Container $container, Extension $extension = null): void { - if (empty($this->routes) && empty($this->removedRoutes)) { - return; - } - - $container->resolving( - "flarum.{$this->appName}.routes", - function (RouteCollection $collection, Container $container) { - /** @var RouteHandlerFactory $factory */ - $factory = $container->make(RouteHandlerFactory::class); - - foreach ($this->removedRoutes as $routeName) { - $collection->removeRoute($routeName); - } - - foreach ($this->routes as $route) { - $collection->addRoute( - $route['method'], - $route['path'], - $route['name'], - $factory->toController($route['handler']) - ); - } - } - ); + $this->registerRoutes($container); } } diff --git a/framework/core/src/Extend/ThrottleApi.php b/framework/core/src/Extend/ThrottleApi.php index ce05332c1..afb3a05a0 100644 --- a/framework/core/src/Extend/ThrottleApi.php +++ b/framework/core/src/Extend/ThrottleApi.php @@ -12,7 +12,7 @@ namespace Flarum\Extend; use Flarum\Extension\Extension; use Flarum\Foundation\ContainerUtil; use Illuminate\Contracts\Container\Container; -use Psr\Http\Message\ServerRequestInterface; +use Illuminate\Http\Request; class ThrottleApi implements ExtenderInterface { @@ -23,10 +23,10 @@ class ThrottleApi implements ExtenderInterface * Add a new throttler (or override one with the same name). * * @param string $name: The name of the throttler. - * @param (callable(ServerRequestInterface $request): bool)|class-string $callback + * @param (callable(Request $request): bool)|class-string $callback * * The callable can be a closure or invokable class, and should accept: - * - $request: The current `\Psr\Http\Message\ServerRequestInterface` request object. + * - $request: The current `\Illuminate\Http\Request` request object. * `\Flarum\Http\RequestUtil::getActor($request)` can be used to get the current user. * `$request->getAttribute('routeName')` can be used to get the current route. * Please note that every throttler runs by default on every route.