From 99a2bf13cf84ce7aaed4e715d139a4d3c4ec0414 Mon Sep 17 00:00:00 2001 From: Aleksei Khudiakov Date: Thu, 8 Feb 2018 15:39:20 +1000 Subject: [PATCH] Drop run() and response emitter from application --- src/Application.php | 50 -------- src/ApplicationInterface.php | 9 -- src/Container/ApplicationFactory.php | 11 -- src/Emitter/EmitterStack.php | 107 ------------------ test/Application/BadControllerTrait.php | 5 - .../InvalidControllerTypeTrait.php | 5 - test/Application/MissingControllerTrait.php | 5 - test/Application/PathControllerTrait.php | 5 - test/ApplicationTest.php | 32 +----- test/Container/ApplicationFactoryTest.php | 30 ----- 10 files changed, 1 insertion(+), 258 deletions(-) delete mode 100644 src/Emitter/EmitterStack.php diff --git a/src/Application.php b/src/Application.php index 7a48562ab..7082152d2 100644 --- a/src/Application.php +++ b/src/Application.php @@ -9,14 +9,10 @@ namespace Zend\Mvc; -use InvalidArgumentException; use Psr\Container\ContainerInterface; use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ResponseInterface; -use UnexpectedValueException; use Zend\Diactoros\Response; -use Zend\Diactoros\Response\EmitterInterface; -use Zend\Diactoros\ServerRequestFactory; use Zend\EventManager\EventManagerAwareInterface; use Zend\EventManager\EventManagerInterface; use Zend\EventManager\ListenerAggregateInterface; @@ -98,11 +94,6 @@ class Application implements */ protected $container; - /** - * @var EmitterInterface - */ - private $emitter; - /** * @var RouteStackInterface */ @@ -121,8 +112,6 @@ class Application implements * @param ContainerInterface $container IoC container from which to pull services * @param RouteStackInterface $router Configured router for RouteListener * @param EventManagerInterface $events - * @param EmitterInterface $emitter Response emitter to use when `run()` - * is invoked * @param array $listeners Extra listeners to attach on bootstrap * Can be container keys or instances of ListenerAggregateInterface */ @@ -130,13 +119,11 @@ public function __construct( ContainerInterface $container, RouteStackInterface $router, EventManagerInterface $events, - EmitterInterface $emitter, array $listeners = [] ) { $this->container = $container; $this->router = $router; $this->setEventManager($events); - $this->emitter = $emitter; $this->listeners = $listeners; // @TODO response prototype? @@ -236,38 +223,6 @@ public function getEventManager() : EventManagerInterface return $this->events; } - /** - * Run the application - * - * @triggers route(MvcEvent) - * Routes the request, and sets the RouteMatch object in the event. - * @triggers dispatch(MvcEvent) - * Dispatches a request, using the discovered RouteMatch and - * provided request. - * @triggers dispatch.error(MvcEvent) - * On errors (controller not found, action not supported, etc.), - * populates the event with information about the error type, - * discovered controller, and controller class (if known). - * Typically, a handler should return a populated Response object - * that can be returned immediately. - * @param Request|null $request - * @return void - */ - public function run(Request $request = null) : void - { - try { - $request = $request ?: ServerRequestFactory::fromGlobals(); - } catch (InvalidArgumentException | UnexpectedValueException $e) { - // emit bad request - throw new \Exception('Not implemented'); - } - - $response = $this->handle($request); - - $emitter = $this->getEmitter(); - $emitter->emit($response); - } - public function handle(Request $request) : ResponseInterface { if (! $this->bootstrapped) { @@ -328,11 +283,6 @@ public function handle(Request $request) : ResponseInterface return $this->completeRequest($event); } - public function getEmitter() : EmitterInterface - { - return $this->emitter; - } - /** * Complete the request * diff --git a/src/ApplicationInterface.php b/src/ApplicationInterface.php index f159fa964..07d34b391 100644 --- a/src/ApplicationInterface.php +++ b/src/ApplicationInterface.php @@ -10,7 +10,6 @@ namespace Zend\Mvc; use Psr\Container\ContainerInterface; -use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; use Zend\EventManager\EventsCapableInterface; @@ -22,12 +21,4 @@ interface ApplicationInterface extends EventsCapableInterface, RequestHandlerInt * @return ContainerInterface */ public function getContainer() : ContainerInterface; - - /** - * Run the application - * - * @param ServerRequestInterface|null $request - * @return void - */ - public function run(ServerRequestInterface $request = null) : void; } diff --git a/src/Container/ApplicationFactory.php b/src/Container/ApplicationFactory.php index 0c3134739..0a7f3769a 100644 --- a/src/Container/ApplicationFactory.php +++ b/src/Container/ApplicationFactory.php @@ -10,10 +10,7 @@ namespace Zend\Mvc\Container; use Psr\Container\ContainerInterface; -use Zend\Diactoros\Response\EmitterInterface; -use Zend\Diactoros\Response\SapiEmitter; use Zend\Mvc\Application; -use Zend\Mvc\Emitter\EmitterStack; class ApplicationFactory { @@ -29,13 +26,6 @@ class ApplicationFactory */ public function __invoke(ContainerInterface $container, string $name, array $options = null) : Application { - if ($container->has(EmitterInterface::class)) { - $emitter = $container->get(EmitterInterface::class); - } else { - $emitter = new EmitterStack(); - $emitter->push(new SapiEmitter()); - } - $config = $container->get('config') ?? []; $listeners = $config[Application::class]['listeners'] ?? []; @@ -43,7 +33,6 @@ public function __invoke(ContainerInterface $container, string $name, array $opt $container, $container->get('Zend\Mvc\Router'), $container->get('EventManager'), - $emitter, $listeners ); diff --git a/src/Emitter/EmitterStack.php b/src/Emitter/EmitterStack.php deleted file mode 100644 index 47e9c2e48..000000000 --- a/src/Emitter/EmitterStack.php +++ /dev/null @@ -1,107 +0,0 @@ -emit($response)) { - return null; - } - } - - return false; - } - - /** - * Set an emitter on the stack by index. - * - * @param mixed $index - * @param EmitterInterface $emitter - * @return void - * @throws InvalidArgumentException if not an EmitterInterface instance - */ - public function offsetSet($index, $emitter) - { - $this->validateEmitter($emitter); - parent::offsetSet($index, $emitter); - } - - /** - * Push an emitter to the stack. - * - * @param EmitterInterface $emitter - * @return void - * @throws InvalidArgumentException if not an EmitterInterface instance - */ - public function push($emitter) - { - $this->validateEmitter($emitter); - parent::push($emitter); - } - - /** - * Unshift an emitter to the stack. - * - * @param EmitterInterface $emitter - * @return void - * @throws InvalidArgumentException if not an EmitterInterface instance - */ - public function unshift($emitter) - { - $this->validateEmitter($emitter); - parent::unshift($emitter); - } - - /** - * Validate that an emitter implements EmitterInterface. - * - * @param mixed $emitter - * @return void - * @throws InvalidArgumentException for non-emitter instances - */ - private function validateEmitter($emitter) - { - if (! $emitter instanceof EmitterInterface) { - throw new Exception\InvalidArgumentException(sprintf( - '%s expects an EmitterInterface implementation', - __CLASS__ - )); - } - } -} diff --git a/test/Application/BadControllerTrait.php b/test/Application/BadControllerTrait.php index cd8b6ac6b..f824218ea 100644 --- a/test/Application/BadControllerTrait.php +++ b/test/Application/BadControllerTrait.php @@ -9,7 +9,6 @@ namespace ZendTest\Mvc\Application; -use Zend\Diactoros\Response\EmitterInterface; use Zend\Mvc\Application; use Zend\Mvc\Controller\ControllerManager; use Zend\Mvc\View\Http\ViewManager; @@ -32,10 +31,6 @@ public function prepareApplication() }, ]]); }, - EmitterInterface::class => function () { - $emitter = $this->prophesize(EmitterInterface::class); - return $emitter->reveal(); - }, ], 'invokables' => [ ViewManager::class => TestAsset\MockViewManager::class, diff --git a/test/Application/InvalidControllerTypeTrait.php b/test/Application/InvalidControllerTypeTrait.php index 5b1ca8294..3e041fd85 100644 --- a/test/Application/InvalidControllerTypeTrait.php +++ b/test/Application/InvalidControllerTypeTrait.php @@ -10,7 +10,6 @@ namespace ZendTest\Mvc\Application; use stdClass; -use Zend\Diactoros\Response\EmitterInterface; use Zend\Mvc\Application; use Zend\Mvc\Controller\ControllerManager; use Zend\Mvc\View\Http\ViewManager; @@ -32,10 +31,6 @@ public function prepareApplication() }, ]]); }, - EmitterInterface::class => function () { - $emitter = $this->prophesize(EmitterInterface::class); - return $emitter->reveal(); - }, ], 'invokables' => [ ViewManager::class => TestAsset\MockViewManager::class, diff --git a/test/Application/MissingControllerTrait.php b/test/Application/MissingControllerTrait.php index d313d2e9d..a34d9ef7d 100644 --- a/test/Application/MissingControllerTrait.php +++ b/test/Application/MissingControllerTrait.php @@ -9,7 +9,6 @@ namespace ZendTest\Mvc\Application; -use Zend\Diactoros\Response\EmitterInterface; use Zend\Mvc\Application; use Zend\Mvc\View\Http\ViewManager; use Zend\Router; @@ -23,10 +22,6 @@ public function prepareApplication() $config = ApplicationConfigHelper::getConfig([ 'dependencies' => [ 'factories' => [ - EmitterInterface::class => function () { - $emitter = $this->prophesize(EmitterInterface::class); - return $emitter->reveal(); - }, ], 'invokables' => [ ViewManager::class => TestAsset\MockViewManager::class, diff --git a/test/Application/PathControllerTrait.php b/test/Application/PathControllerTrait.php index ac40a5e44..bd6cfac77 100644 --- a/test/Application/PathControllerTrait.php +++ b/test/Application/PathControllerTrait.php @@ -9,7 +9,6 @@ namespace ZendTest\Mvc\Application; -use Zend\Diactoros\Response\EmitterInterface; use Zend\Mvc\Application; use Zend\Mvc\Controller\ControllerManager; use Zend\Mvc\View\Http\ViewManager; @@ -31,10 +30,6 @@ public function prepareApplication() }, ]]); }, - EmitterInterface::class => function () { - $emitter = $this->prophesize(EmitterInterface::class); - return $emitter->reveal(); - }, ], 'invokables' => [ ViewManager::class => TestAsset\MockViewManager::class, diff --git a/test/ApplicationTest.php b/test/ApplicationTest.php index f1c51d1bf..c56b8ffcd 100644 --- a/test/ApplicationTest.php +++ b/test/ApplicationTest.php @@ -13,7 +13,6 @@ use Prophecy\Prophecy\ObjectProphecy; use ReflectionProperty; use Zend\Diactoros\Response; -use Zend\Diactoros\Response\EmitterInterface; use Zend\Diactoros\ServerRequest; use Zend\EventManager\EventManager; use Zend\EventManager\ListenerAggregateInterface; @@ -51,11 +50,6 @@ class ApplicationTest extends TestCase */ private $router; - /** - * @var ObjectProphecy - */ - private $emitter; - /** * @var EventManager */ @@ -71,7 +65,6 @@ public function setUp() $this->container = $this->mockContainerInterface(); $this->router = $this->prophesize(RouteStackInterface::class); - $this->emitter = $this->prophesize(EmitterInterface::class); $this->events = new EventManager(new SharedEventManager()); $this->injectServiceInContainer($this->container, 'config', []); @@ -99,8 +92,7 @@ public function setUp() $this->application = new Application( $this->container->reveal(), $this->router->reveal(), - $this->events, - $this->emitter->reveal() + $this->events ); } @@ -161,7 +153,6 @@ public function testBootstrapAlwaysRegistersDefaultListeners() $this->container->reveal(), $this->router->reveal(), $this->events, - $this->emitter->reveal(), ['Custom'] ); @@ -189,7 +180,6 @@ public function testBootstrapAttachesInstanceOfListenerAggregate() $this->container->reveal(), $this->router->reveal(), $this->events, - $this->emitter->reveal(), [$custom->reveal()] ); @@ -504,26 +494,6 @@ function (MvcEvent $e) use ($testResponse) { $this->assertSame($testResponse, $response); } - public function testEmitterGetterReturnsInjectedEmitter() - { - $emitter = $this->application->getEmitter(); - $this->assertSame($this->emitter->reveal(), $emitter); - } - - public function testRunInvokesEmitterForResponse() - { - $response = new Response(); - $this->application->getEventManager()->attach( - MvcEvent::EVENT_DISPATCH, - function () use ($response) { - return $response; - } - ); - $this->emitter->emit($response)->shouldBeCalled(); - $request = new ServerRequest([], [], 'http://example.local/sample', 'GET', 'php://memory'); - $this->application->run($request); - } - public function eventPropagation() { return [ diff --git a/test/Container/ApplicationFactoryTest.php b/test/Container/ApplicationFactoryTest.php index 3b08b3820..4b5989df5 100644 --- a/test/Container/ApplicationFactoryTest.php +++ b/test/Container/ApplicationFactoryTest.php @@ -10,8 +10,6 @@ use Prophecy\Prophecy\ObjectProphecy; use Psr\Container\ContainerInterface; -use Zend\Diactoros\Response\EmitterInterface; -use Zend\Diactoros\Response\SapiEmitter; use Zend\EventManager\EventManager; use Zend\EventManager\EventManagerInterface; use Zend\EventManager\ListenerAggregateInterface; @@ -19,7 +17,6 @@ use Zend\Mvc\Container\ApplicationFactory; use PHPUnit\Framework\TestCase; use Zend\Mvc\DispatchListener; -use Zend\Mvc\Emitter\EmitterStack; use Zend\Mvc\HttpMethodListener; use Zend\Mvc\MiddlewareListener; use Zend\Mvc\RouteListener; @@ -49,11 +46,6 @@ class ApplicationFactoryTest extends TestCase */ private $router; - /** - * @var ObjectProphecy|EmitterInterface - */ - private $emitter; - /** * @var EventManager */ @@ -65,34 +57,12 @@ public function setUp() $this->factory = new ApplicationFactory(); $this->router = $this->prophesize(RouteStackInterface::class); - $this->emitter = $this->prophesize(EmitterInterface::class); $this->events = $this->prophesize(EventManagerInterface::class); $this->injectServiceInContainer($this->container, 'Zend\Mvc\Router', $this->router); $this->injectServiceInContainer($this->container, 'EventManager', $this->events); $this->injectServiceInContainer($this->container, 'config', []); } - - public function testInjectsEmitterWhenAvailable() - { - $this->injectServiceInContainer( - $this->container, - EmitterInterface::class, - $this->emitter->reveal() - ); - $app = $this->factory->__invoke($this->container->reveal(), Application::class); - $this->assertSame($this->emitter->reveal(), $app->getEmitter()); - } - - public function testInjectsNewEmitterStackWhenEmitterNotInContainer() - { - $app = $this->factory->__invoke($this->container->reveal(), Application::class); - $emitter = $app->getEmitter(); - $this->assertInstanceOf(EmitterStack::class, $emitter); - $this->assertCount(1, $emitter); - $this->assertInstanceOf(SapiEmitter::class, $emitter[0]); - } - public function testInjectsListenersFromConfig() { // application default listeners