Merge pull request #1443 from flarum/fl/controller-to-handlers

Replace ControllerInterface with PSR-15 interface
This commit is contained in:
Franz Liedke 2018-05-30 09:54:13 +02:00 committed by GitHub
commit 6b723a86e3
22 changed files with 86 additions and 90 deletions

View File

@ -13,10 +13,10 @@ namespace Flarum\Api;
use Exception;
use Flarum\Foundation\Application;
use Flarum\Http\Controller\ControllerInterface;
use Flarum\User\User;
use InvalidArgumentException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Zend\Diactoros\ServerRequestFactory;
class Client
@ -43,7 +43,7 @@ class Client
/**
* Execute the given API action class, pass the input and return its response.
*
* @param string|ControllerInterface $controller
* @param string|RequestHandlerInterface $controller
* @param User|null $actor
* @param array $queryParams
* @param array $body
@ -60,9 +60,9 @@ class Client
$controller = $this->app->make($controller);
}
if (! ($controller instanceof ControllerInterface)) {
if (! ($controller instanceof RequestHandlerInterface)) {
throw new InvalidArgumentException(
'Endpoint must be an instance of '.ControllerInterface::class
'Endpoint must be an instance of '.RequestHandlerInterface::class
);
}

View File

@ -11,6 +11,7 @@
namespace Flarum\Api\Controller;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
abstract class AbstractCreateController extends AbstractShowController
@ -18,7 +19,7 @@ abstract class AbstractCreateController extends AbstractShowController
/**
* {@inheritdoc}
*/
public function handle(ServerRequestInterface $request)
public function handle(ServerRequestInterface $request): ResponseInterface
{
return parent::handle($request)->withStatus(201);
}

View File

@ -11,16 +11,17 @@
namespace Flarum\Api\Controller;
use Flarum\Http\Controller\ControllerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Zend\Diactoros\Response\EmptyResponse;
abstract class AbstractDeleteController implements ControllerInterface
abstract class AbstractDeleteController implements RequestHandlerInterface
{
/**
* {@inheritdoc}
*/
public function handle(ServerRequestInterface $request)
public function handle(ServerRequestInterface $request): ResponseInterface
{
$this->delete($request);

View File

@ -14,15 +14,16 @@ namespace Flarum\Api\Controller;
use Flarum\Api\Event\WillGetData;
use Flarum\Api\Event\WillSerializeData;
use Flarum\Api\JsonApiResponse;
use Flarum\Http\Controller\ControllerInterface;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Events\Dispatcher;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Tobscure\JsonApi\Document;
use Tobscure\JsonApi\Parameters;
use Tobscure\JsonApi\SerializerInterface;
abstract class AbstractSerializeController implements ControllerInterface
abstract class AbstractSerializeController implements RequestHandlerInterface
{
/**
* The name of the serializer class to output results with.
@ -86,7 +87,7 @@ abstract class AbstractSerializeController implements ControllerInterface
/**
* {@inheritdoc}
*/
public function handle(ServerRequestInterface $request)
public function handle(ServerRequestInterface $request): ResponseInterface
{
$document = new Document;

View File

@ -11,14 +11,15 @@
namespace Flarum\Api\Controller;
use Flarum\Http\Controller\ControllerInterface;
use Flarum\User\Command\RequestPasswordReset;
use Flarum\User\UserRepository;
use Illuminate\Contracts\Bus\Dispatcher;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Zend\Diactoros\Response\EmptyResponse;
class ForgotPasswordController implements ControllerInterface
class ForgotPasswordController implements RequestHandlerInterface
{
/**
* @var \Flarum\User\UserRepository
@ -43,7 +44,7 @@ class ForgotPasswordController implements ControllerInterface
/**
* {@inheritdoc}
*/
public function handle(ServerRequestInterface $request)
public function handle(ServerRequestInterface $request): ResponseInterface
{
$email = array_get($request->getParsedBody(), 'email');

View File

@ -11,7 +11,6 @@
namespace Flarum\Api\Controller;
use Flarum\Http\Controller\ControllerInterface;
use Flarum\Http\UrlGenerator;
use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\User\AssertPermissionTrait;
@ -19,11 +18,13 @@ use Flarum\User\EmailToken;
use Flarum\User\Exception\PermissionDeniedException;
use Illuminate\Contracts\Mail\Mailer;
use Illuminate\Mail\Message;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Symfony\Component\Translation\TranslatorInterface;
use Zend\Diactoros\Response\EmptyResponse;
class SendConfirmationEmailController implements ControllerInterface
class SendConfirmationEmailController implements RequestHandlerInterface
{
use AssertPermissionTrait;
@ -64,7 +65,7 @@ class SendConfirmationEmailController implements ControllerInterface
/**
* {@inheritdoc}
*/
public function handle(ServerRequestInterface $request)
public function handle(ServerRequestInterface $request): ResponseInterface
{
$id = array_get($request->getQueryParams(), 'id');
$actor = $request->getAttribute('actor');

View File

@ -12,19 +12,20 @@
namespace Flarum\Api\Controller;
use Flarum\Group\Permission;
use Flarum\Http\Controller\ControllerInterface;
use Flarum\User\AssertPermissionTrait;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Zend\Diactoros\Response\EmptyResponse;
class SetPermissionController implements ControllerInterface
class SetPermissionController implements RequestHandlerInterface
{
use AssertPermissionTrait;
/**
* {@inheritdoc}
*/
public function handle(ServerRequestInterface $request)
public function handle(ServerRequestInterface $request): ResponseInterface
{
$this->assertAdmin($request->getAttribute('actor'));

View File

@ -11,16 +11,17 @@
namespace Flarum\Api\Controller;
use Flarum\Http\Controller\ControllerInterface;
use Flarum\Settings\Event\Saved;
use Flarum\Settings\Event\Serializing;
use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\User\AssertPermissionTrait;
use Illuminate\Contracts\Events\Dispatcher;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Zend\Diactoros\Response\EmptyResponse;
class SetSettingsController implements ControllerInterface
class SetSettingsController implements RequestHandlerInterface
{
use AssertPermissionTrait;
@ -46,7 +47,7 @@ class SetSettingsController implements ControllerInterface
/**
* {@inheritdoc}
*/
public function handle(ServerRequestInterface $request)
public function handle(ServerRequestInterface $request): ResponseInterface
{
$this->assertAdmin($request->getAttribute('actor'));

View File

@ -12,15 +12,16 @@
namespace Flarum\Api\Controller;
use Flarum\Http\AccessToken;
use Flarum\Http\Controller\ControllerInterface;
use Flarum\User\Exception\PermissionDeniedException;
use Flarum\User\UserRepository;
use Illuminate\Contracts\Bus\Dispatcher as BusDispatcher;
use Illuminate\Contracts\Events\Dispatcher as EventDispatcher;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Zend\Diactoros\Response\JsonResponse;
class TokenController implements ControllerInterface
class TokenController implements RequestHandlerInterface
{
/**
* @var \Flarum\User\UserRepository
@ -52,7 +53,7 @@ class TokenController implements ControllerInterface
/**
* {@inheritdoc}
*/
public function handle(ServerRequestInterface $request)
public function handle(ServerRequestInterface $request): ResponseInterface
{
$body = $request->getParsedBody();

View File

@ -12,12 +12,13 @@
namespace Flarum\Api\Controller;
use Flarum\Extension\ExtensionManager;
use Flarum\Http\Controller\ControllerInterface;
use Flarum\User\AssertPermissionTrait;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Zend\Diactoros\Response\EmptyResponse;
class UpdateExtensionController implements ControllerInterface
class UpdateExtensionController implements RequestHandlerInterface
{
use AssertPermissionTrait;
@ -37,7 +38,7 @@ class UpdateExtensionController implements ControllerInterface
/**
* {@inheritdoc}
*/
public function handle(ServerRequestInterface $request)
public function handle(ServerRequestInterface $request): ResponseInterface
{
$this->assertAdmin($request->getAttribute('actor'));

View File

@ -12,12 +12,13 @@
namespace Flarum\Forum\Controller;
use Flarum\Forum\AuthenticationResponseFactory;
use Flarum\Http\Controller\ControllerInterface;
use League\OAuth2\Client\Provider\ResourceOwnerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface;
use Zend\Diactoros\Response\RedirectResponse;
abstract class AbstractOAuth2Controller implements ControllerInterface
abstract class AbstractOAuth2Controller implements RequestHandlerInterface
{
/**
* @var AuthenticationResponseFactory
@ -46,9 +47,9 @@ abstract class AbstractOAuth2Controller implements ControllerInterface
/**
* @param Request $request
* @return \Psr\Http\Message\ResponseInterface|RedirectResponse
* @return ResponseInterface
*/
public function handle(Request $request)
public function handle(Request $request): ResponseInterface
{
$redirectUri = (string) $request->getAttribute('originalUri', $request->getUri())->withQuery('');

View File

@ -12,16 +12,17 @@
namespace Flarum\Forum\Controller;
use Flarum\Foundation\Application;
use Flarum\Http\Controller\ControllerInterface;
use Flarum\Http\SessionAuthenticator;
use Flarum\User\Command\ConfirmEmail;
use Flarum\User\Exception\InvalidConfirmationTokenException;
use Illuminate\Contracts\Bus\Dispatcher;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface;
use Zend\Diactoros\Response\HtmlResponse;
use Zend\Diactoros\Response\RedirectResponse;
class ConfirmEmailController implements ControllerInterface
class ConfirmEmailController implements RequestHandlerInterface
{
/**
* @var Dispatcher
@ -52,9 +53,9 @@ class ConfirmEmailController implements ControllerInterface
/**
* @param Request $request
* @return \Psr\Http\Message\ResponseInterface
* @return ResponseInterface
*/
public function handle(Request $request)
public function handle(Request $request): ResponseInterface
{
try {
$token = array_get($request->getQueryParams(), 'token');

View File

@ -14,14 +14,15 @@ namespace Flarum\Forum\Controller;
use Flarum\Api\Client;
use Flarum\Api\Controller\TokenController;
use Flarum\Http\AccessToken;
use Flarum\Http\Controller\ControllerInterface;
use Flarum\Http\Rememberer;
use Flarum\Http\SessionAuthenticator;
use Flarum\User\Event\LoggedIn;
use Flarum\User\UserRepository;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface;
class LogInController implements ControllerInterface
class LogInController implements RequestHandlerInterface
{
/**
* @var \Flarum\User\UserRepository
@ -60,7 +61,7 @@ class LogInController implements ControllerInterface
/**
* {@inheritdoc}
*/
public function handle(Request $request)
public function handle(Request $request): ResponseInterface
{
$actor = $request->getAttribute('actor');
$body = $request->getParsedBody();

View File

@ -12,7 +12,6 @@
namespace Flarum\Forum\Controller;
use Flarum\Foundation\Application;
use Flarum\Http\Controller\ControllerInterface;
use Flarum\Http\Exception\TokenMismatchException;
use Flarum\Http\Rememberer;
use Flarum\Http\SessionAuthenticator;
@ -21,11 +20,13 @@ use Flarum\User\AssertPermissionTrait;
use Flarum\User\Event\LoggedOut;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\View\Factory;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface;
use Zend\Diactoros\Response\HtmlResponse;
use Zend\Diactoros\Response\RedirectResponse;
class LogOutController implements ControllerInterface
class LogOutController implements RequestHandlerInterface
{
use AssertPermissionTrait;
@ -85,10 +86,10 @@ class LogOutController implements ControllerInterface
/**
* @param Request $request
* @return \Psr\Http\Message\ResponseInterface
* @return ResponseInterface
* @throws TokenMismatchException
*/
public function handle(Request $request)
public function handle(Request $request): ResponseInterface
{
$session = $request->getAttribute('session');
$actor = $request->getAttribute('actor');

View File

@ -13,12 +13,13 @@ namespace Flarum\Forum\Controller;
use Flarum\Api\Client;
use Flarum\Api\Controller\CreateUserController;
use Flarum\Http\Controller\ControllerInterface;
use Flarum\Http\Rememberer;
use Flarum\Http\SessionAuthenticator;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface;
class RegisterController implements ControllerInterface
class RegisterController implements RequestHandlerInterface
{
/**
* @var Client
@ -50,7 +51,7 @@ class RegisterController implements ControllerInterface
/**
* {@inheritdoc}
*/
public function handle(Request $request)
public function handle(Request $request): ResponseInterface
{
$controller = CreateUserController::class;
$actor = $request->getAttribute('actor');

View File

@ -12,7 +12,6 @@
namespace Flarum\Forum\Controller;
use Flarum\Foundation\DispatchEventsTrait;
use Flarum\Http\Controller\ControllerInterface;
use Flarum\Http\SessionAuthenticator;
use Flarum\Http\UrlGenerator;
use Flarum\User\PasswordToken;
@ -20,10 +19,12 @@ use Flarum\User\UserValidator;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Validation\Factory;
use Illuminate\Validation\ValidationException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface;
use Zend\Diactoros\Response\RedirectResponse;
class SavePasswordController implements ControllerInterface
class SavePasswordController implements RequestHandlerInterface
{
use DispatchEventsTrait;
@ -64,9 +65,9 @@ class SavePasswordController implements ControllerInterface
/**
* @param Request $request
* @return RedirectResponse
* @return ResponseInterface
*/
public function handle(Request $request)
public function handle(Request $request): ResponseInterface
{
$input = $request->getParsedBody();

View File

@ -12,16 +12,18 @@
namespace Flarum\Http\Controller;
use Illuminate\Contracts\Support\Renderable;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface;
use Zend\Diactoros\Response\HtmlResponse;
abstract class AbstractHtmlController implements ControllerInterface
abstract class AbstractHtmlController implements RequestHandlerInterface
{
/**
* @param Request $request
* @return HtmlResponse
*/
public function handle(Request $request)
public function handle(Request $request): ResponseInterface
{
$view = $this->render($request);

View File

@ -1,23 +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\Controller;
use Psr\Http\Message\ServerRequestInterface;
interface ControllerInterface
{
/**
* @param ServerRequestInterface $request
* @return \Psr\Http\Message\ResponseInterface
*/
public function handle(ServerRequestInterface $request);
}

View File

@ -11,11 +11,11 @@
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;
use Psr\Http\Server\RequestHandlerInterface;
class ControllerRouteHandler
{
@ -55,15 +55,15 @@ class ControllerRouteHandler
/**
* @param string $class
* @return ControllerInterface
* @return RequestHandlerInterface
*/
protected function resolveController($class)
{
$controller = $this->container->make($class);
if (! ($controller instanceof ControllerInterface)) {
if (! ($controller instanceof RequestHandlerInterface)) {
throw new InvalidArgumentException(
'Controller must be an instance of '.ControllerInterface::class
'Controller must be an instance of '.RequestHandlerInterface::class
);
}

View File

@ -12,17 +12,18 @@
namespace Flarum\Install\Controller;
use Exception;
use Flarum\Http\Controller\ControllerInterface;
use Flarum\Http\SessionAuthenticator;
use Flarum\Install\Console\DefaultsDataProvider;
use Flarum\Install\Console\InstallCommand;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\StreamOutput;
use Zend\Diactoros\Response;
use Zend\Diactoros\Response\HtmlResponse;
class InstallController implements ControllerInterface
class InstallController implements RequestHandlerInterface
{
protected $command;
@ -44,9 +45,9 @@ class InstallController implements ControllerInterface
/**
* @param Request $request
* @return \Psr\Http\Message\ResponseInterface
* @return ResponseInterface
*/
public function handle(Request $request)
public function handle(Request $request): ResponseInterface
{
$input = $request->getParsedBody();

View File

@ -14,14 +14,15 @@ namespace Flarum\Update\Controller;
use Exception;
use Flarum\Database\Console\MigrateCommand;
use Flarum\Foundation\Application;
use Flarum\Http\Controller\ControllerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\StreamOutput;
use Zend\Diactoros\Response;
use Zend\Diactoros\Response\HtmlResponse;
class UpdateController implements ControllerInterface
class UpdateController implements RequestHandlerInterface
{
protected $command;
@ -42,9 +43,9 @@ class UpdateController implements ControllerInterface
/**
* @param Request $request
* @return \Psr\Http\Message\ResponseInterface
* @return ResponseInterface
*/
public function handle(Request $request)
public function handle(Request $request): ResponseInterface
{
$input = $request->getParsedBody();

View File

@ -11,16 +11,16 @@
namespace Flarum\Tests\Api\Controller;
use Flarum\Http\Controller\ControllerInterface;
use Flarum\Tests\Test\TestCase;
use Flarum\User\User;
use Illuminate\Support\Arr;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Server\RequestHandlerInterface;
abstract class ApiControllerTestCase extends TestCase
{
/**
* @var ControllerInterface
* @var RequestHandlerInterface
*/
protected $controller;