mirror of
https://github.com/flarum/core.git
synced 2025-06-18 08:01:28 +02:00
Consolidate ControllerRouteHandler into RouteHandlerFactory
Also allow closure to be passed for frontend content when creating routes
This commit is contained in:
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
@ -11,8 +11,12 @@
|
|||||||
|
|
||||||
namespace Flarum\Http;
|
namespace Flarum\Http;
|
||||||
|
|
||||||
|
use Closure;
|
||||||
use Flarum\Frontend\Controller as FrontendController;
|
use Flarum\Frontend\Controller as FrontendController;
|
||||||
use Illuminate\Contracts\Container\Container;
|
use Illuminate\Contracts\Container\Container;
|
||||||
|
use InvalidArgumentException;
|
||||||
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||||
|
use Psr\Http\Server\RequestHandlerInterface as Handler;
|
||||||
|
|
||||||
class RouteHandlerFactory
|
class RouteHandlerFactory
|
||||||
{
|
{
|
||||||
@ -21,56 +25,61 @@ class RouteHandlerFactory
|
|||||||
*/
|
*/
|
||||||
protected $container;
|
protected $container;
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Container $container
|
|
||||||
*/
|
|
||||||
public function __construct(Container $container)
|
public function __construct(Container $container)
|
||||||
{
|
{
|
||||||
$this->container = $container;
|
$this->container = $container;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function toController($controller): Closure
|
||||||
* @param string|callable $controller
|
|
||||||
* @return ControllerRouteHandler
|
|
||||||
*/
|
|
||||||
public function toController($controller)
|
|
||||||
{
|
{
|
||||||
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 $frontend
|
||||||
* @param string|null $content
|
* @param string|callable|null $content
|
||||||
* @return ControllerRouteHandler
|
|
||||||
*/
|
*/
|
||||||
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) {
|
return $this->toController(function (Container $container) use ($frontend, $content) {
|
||||||
$frontend = $container->make("flarum.frontend.$frontend");
|
$frontend = $container->make("flarum.frontend.$frontend");
|
||||||
|
|
||||||
if ($content) {
|
if ($content) {
|
||||||
$frontend->add($container->make($content));
|
$frontend->content(is_callable($content) ? $content : $container->make($content));
|
||||||
}
|
}
|
||||||
|
|
||||||
return new FrontendController($frontend);
|
return new FrontendController($frontend);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function toForum(string $content = null): Closure
|
||||||
* @param string|null $content
|
|
||||||
* @return ControllerRouteHandler
|
|
||||||
*/
|
|
||||||
public function toForum(string $content = null)
|
|
||||||
{
|
{
|
||||||
return $this->toFrontend('forum', $content);
|
return $this->toFrontend('forum', $content);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function toAdmin(string $content = null): Closure
|
||||||
* @param string|null $content
|
|
||||||
* @return ControllerRouteHandler
|
|
||||||
*/
|
|
||||||
public function toAdmin(string $content = null)
|
|
||||||
{
|
{
|
||||||
return $this->toFrontend('admin', $content);
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user