From 1ea8dbed03aa0eb1bb71d7efa903475ce226592b Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Fri, 16 Nov 2018 13:55:10 +1030 Subject: [PATCH] Consolidate ControllerRouteHandler into RouteHandlerFactory Also allow closure to be passed for frontend content when creating routes --- .../core/src/Http/ControllerRouteHandler.php | 75 ------------------- .../core/src/Http/RouteHandlerFactory.php | 55 ++++++++------ 2 files changed, 32 insertions(+), 98 deletions(-) delete mode 100644 framework/core/src/Http/ControllerRouteHandler.php diff --git a/framework/core/src/Http/ControllerRouteHandler.php b/framework/core/src/Http/ControllerRouteHandler.php deleted file mode 100644 index 78e795d86..000000000 --- a/framework/core/src/Http/ControllerRouteHandler.php +++ /dev/null @@ -1,75 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Flarum\Http; - -use Illuminate\Contracts\Container\Container; -use InvalidArgumentException; -use Psr\Http\Message\ResponseInterface; -use Psr\Http\Message\ServerRequestInterface; -use Psr\Http\Server\RequestHandlerInterface; - -class ControllerRouteHandler -{ - /** - * @var Container - */ - protected $container; - - /** - * @var string|callable - */ - protected $controller; - - /** - * @param Container $container - * @param string|callable $controller - */ - public function __construct(Container $container, $controller) - { - $this->container = $container; - $this->controller = $controller; - } - - /** - * @param ServerRequestInterface $request - * @param array $routeParams - * @return ResponseInterface - */ - public function __invoke(ServerRequestInterface $request, array $routeParams) - { - $controller = $this->resolveController(); - - $request = $request->withQueryParams(array_merge($request->getQueryParams(), $routeParams)); - - return $controller->handle($request); - } - - /** - * @return RequestHandlerInterface - */ - protected function resolveController() - { - if (is_callable($this->controller)) { - $controller = $this->container->call($this->controller); - } else { - $controller = $this->container->make($this->controller); - } - - if (! ($controller instanceof RequestHandlerInterface)) { - throw new InvalidArgumentException( - 'Controller must be an instance of '.RequestHandlerInterface::class - ); - } - - return $controller; - } -} diff --git a/framework/core/src/Http/RouteHandlerFactory.php b/framework/core/src/Http/RouteHandlerFactory.php index b735f3166..62cd2f7df 100644 --- a/framework/core/src/Http/RouteHandlerFactory.php +++ b/framework/core/src/Http/RouteHandlerFactory.php @@ -11,8 +11,12 @@ namespace Flarum\Http; +use Closure; use Flarum\Frontend\Controller as FrontendController; use Illuminate\Contracts\Container\Container; +use InvalidArgumentException; +use Psr\Http\Message\ServerRequestInterface as Request; +use Psr\Http\Server\RequestHandlerInterface as Handler; class RouteHandlerFactory { @@ -21,56 +25,61 @@ class RouteHandlerFactory */ protected $container; - /** - * @param Container $container - */ public function __construct(Container $container) { $this->container = $container; } - /** - * @param string|callable $controller - * @return ControllerRouteHandler - */ - public function toController($controller) + public function toController($controller): Closure { - return new ControllerRouteHandler($this->container, $controller); + return function (Request $request, array $routeParams) use ($controller) { + $controller = $this->resolveController($controller); + + $request = $request->withQueryParams(array_merge($request->getQueryParams(), $routeParams)); + + return $controller->handle($request); + }; } /** * @param string $frontend - * @param string|null $content - * @return ControllerRouteHandler + * @param string|callable|null $content */ - public function toFrontend(string $frontend, string $content = null) + public function toFrontend(string $frontend, $content = null): Closure { return $this->toController(function (Container $container) use ($frontend, $content) { $frontend = $container->make("flarum.frontend.$frontend"); if ($content) { - $frontend->add($container->make($content)); + $frontend->content(is_callable($content) ? $content : $container->make($content)); } return new FrontendController($frontend); }); } - /** - * @param string|null $content - * @return ControllerRouteHandler - */ - public function toForum(string $content = null) + public function toForum(string $content = null): Closure { return $this->toFrontend('forum', $content); } - /** - * @param string|null $content - * @return ControllerRouteHandler - */ - public function toAdmin(string $content = null) + public function toAdmin(string $content = null): Closure { return $this->toFrontend('admin', $content); } + + private function resolveController($controller): Handler + { + if (is_callable($controller)) { + $controller = $this->container->call($controller); + } else { + $controller = $this->container->make($controller); + } + + if (! $controller instanceof Handler) { + throw new InvalidArgumentException('Controller must be an instance of '.Handler::class); + } + + return $controller; + } }