mirror of
https://github.com/flarum/core.git
synced 2025-10-17 17:56:14 +02:00
The custom interface already had the same signature as the one from the standard (except for the return type hint), so why not use that one now? :)
73 lines
1.7 KiB
PHP
73 lines
1.7 KiB
PHP
<?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
|
|
*/
|
|
protected $controller;
|
|
|
|
/**
|
|
* @param Container $container
|
|
* @param string $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($this->controller);
|
|
|
|
$request = $request->withQueryParams(array_merge($request->getQueryParams(), $routeParams));
|
|
|
|
return $controller->handle($request);
|
|
}
|
|
|
|
/**
|
|
* @param string $class
|
|
* @return RequestHandlerInterface
|
|
*/
|
|
protected function resolveController($class)
|
|
{
|
|
$controller = $this->container->make($class);
|
|
|
|
if (! ($controller instanceof RequestHandlerInterface)) {
|
|
throw new InvalidArgumentException(
|
|
'Controller must be an instance of '.RequestHandlerInterface::class
|
|
);
|
|
}
|
|
|
|
return $controller;
|
|
}
|
|
}
|