Consolidate ControllerRouteHandler into RouteHandlerFactory

Also allow closure to be passed for frontend content when creating routes
This commit is contained in:
Toby Zerner 2018-11-16 13:55:10 +10:30
parent 171f9184d9
commit 1ea8dbed03
2 changed files with 32 additions and 98 deletions

View File

@ -1,75 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* 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;
}
}

View File

@ -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;
}
}