1
0
mirror of https://github.com/flarum/core.git synced 2025-10-11 23:14:29 +02:00
Files
php-flarum/src/Http/GenerateRouteHandlerTrait.php
2016-02-26 13:10:32 +09:00

42 lines
1.2 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 Flarum\Http\Controller\ControllerInterface;
use Illuminate\Contracts\Container\Container;
use InvalidArgumentException;
use Psr\Http\Message\ServerRequestInterface;
trait GenerateRouteHandlerTrait
{
/**
* @return \Closure
*/
protected function getHandlerGenerator(Container $container)
{
return function ($class) use ($container) {
return function (ServerRequestInterface $request, $routeParams) use ($class, $container) {
$controller = $container->make($class);
if (! ($controller instanceof ControllerInterface)) {
throw new InvalidArgumentException(
'Route handler must be an instance of '.ControllerInterface::class
);
}
$request = $request->withQueryParams(array_merge($request->getQueryParams(), $routeParams));
return $controller->handle($request);
};
};
}
}