diff --git a/src/Console/CommandRunner.php b/src/Console/CommandRunner.php index 23cc2b9c5ba..0f4007ea50c 100644 --- a/src/Console/CommandRunner.php +++ b/src/Console/CommandRunner.php @@ -117,11 +117,7 @@ public function setAliases(array $aliases) */ public function run(array $argv, ConsoleIo $io = null) { - $this->app->bootstrap(); - if ($this->app instanceof EventApplicationInterface) { - $eventManager = $this->app->events($this->getEventManager()); - $this->setEventManager($eventManager); - } + $this->bootstrap(); $commands = new CommandCollection([ 'version' => VersionCommand::class, @@ -165,6 +161,22 @@ public function run(array $argv, ConsoleIo $io = null) return Shell::CODE_ERROR; } + /** + * Application bootstrap wrapper. + * + * Calls `bootstrap()` and `events()` if application implements `EventApplicationInterface`. + * + * @return void + */ + protected function bootstrap() + { + $this->app->bootstrap(); + if ($this->app instanceof EventApplicationInterface) { + $eventManager = $this->app->events($this->getEventManager()); + $this->setEventManager($eventManager); + } + } + /** * Get the shell instance for a given command name * diff --git a/src/Event/Middleware/EventMiddleware.php b/src/Event/Middleware/EventMiddleware.php deleted file mode 100644 index 32c13340576..00000000000 --- a/src/Event/Middleware/EventMiddleware.php +++ /dev/null @@ -1,74 +0,0 @@ -app = $app; - if ($dispatcher === null) { - if ($app instanceof EventDispatcherInterface) { - throw new RuntimeException('Event dispatcher has not been provided.'); - } - $dispatcher = $app; - } - $this->dispatcher = $dispatcher; - } - - /** - * Middleware invoke method. - * - * Executes `events()` callback on a `$dispatcher`'s event manager. - * - * @param \Psr\Http\Message\ServerRequestInterface $request Server request. - * @param \Psr\Http\Message\ResponseInterface $response Response. - * @param callable $next Callback to invoke the next middleware. - * @return \Psr\Http\Message\ResponseInterface - */ - public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) - { - $eventManager = $this->dispatcher->getEventManager(); - $eventManager = $this->app->events($eventManager); - $this->dispatcher->setEventManager($eventManager); - - return $next($request, $response); - } -} diff --git a/src/Http/Server.php b/src/Http/Server.php index 993a230f2f3..afbe74a0116 100644 --- a/src/Http/Server.php +++ b/src/Http/Server.php @@ -15,6 +15,8 @@ namespace Cake\Http; use Cake\Core\HttpApplicationInterface; +use Cake\Event\EventApplicationInterface; +use Cake\Event\EventDispatcherInterface; use Cake\Event\EventDispatcherTrait; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; @@ -24,7 +26,7 @@ /** * Runs an application invoking all the PSR7 middleware and the registered application. */ -class Server +class Server implements EventDispatcherInterface { use EventDispatcherTrait; @@ -68,7 +70,8 @@ public function __construct(HttpApplicationInterface $app) */ public function run(ServerRequestInterface $request = null, ResponseInterface $response = null) { - $this->app->bootstrap(); + $this->bootstrap(); + $response = $response ?: new Response(); $request = $request ?: ServerRequestFactory::fromGlobals(); @@ -90,6 +93,22 @@ public function run(ServerRequestInterface $request = null, ResponseInterface $r return $response; } + /** + * Application bootstrap wrapper. + * + * Calls `bootstrap()` and `events()` if application implements `EventApplicationInterface`. + * + * @return void + */ + protected function bootstrap() + { + $this->app->bootstrap(); + if ($this->app instanceof EventApplicationInterface) { + $eventManager = $this->app->events($this->getEventManager()); + $this->setEventManager($eventManager); + } + } + /** * Emit the response using the PHP SAPI. *