1
0
mirror of https://github.com/flarum/core.git synced 2025-10-13 07:54:25 +02:00

Flatten Flarum\Http namespace

This commit is contained in:
Franz Liedke
2017-06-24 13:19:56 +02:00
parent 551e76f296
commit 33e3d757c3
11 changed files with 12 additions and 12 deletions

View File

@@ -0,0 +1,72 @@
<?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 Flarum\Http\Controller\ControllerInterface;
use Illuminate\Contracts\Container\Container;
use InvalidArgumentException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
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 ControllerInterface
*/
protected function resolveController($class)
{
$controller = $this->container->make($class);
if (! ($controller instanceof ControllerInterface)) {
throw new InvalidArgumentException(
'Controller must be an instance of '.ControllerInterface::class
);
}
return $controller;
}
}