From 7f5681470f62cb6b2e24b62650a83fc7cf486909 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Thu, 22 Feb 2018 21:49:10 -0500 Subject: [PATCH] Merge the EventApplication and PluginApplication interfaces. They had an awkward relationship that is better expressed by making them the same. This also lets the pluginEvents() hook be a 'transparent' hook method similar to routes, middleware and console. --- src/Console/CommandRunner.php | 9 +++---- src/Core/PluginApplicationInterface.php | 18 +++++++++++--- src/Core/composer.json | 3 +++ src/Event/EventApplicationInterface.php | 26 -------------------- src/Http/BaseApplication.php | 11 +++------ src/Http/Server.php | 10 ++++---- tests/TestCase/Console/CommandRunnerTest.php | 5 +++- tests/TestCase/Http/BaseApplicationTest.php | 2 +- tests/TestCase/Http/ServerTest.php | 5 +++- 9 files changed, 39 insertions(+), 50 deletions(-) delete mode 100644 src/Event/EventApplicationInterface.php diff --git a/src/Console/CommandRunner.php b/src/Console/CommandRunner.php index 64c2d50d09b..b599f5c3cee 100644 --- a/src/Console/CommandRunner.php +++ b/src/Console/CommandRunner.php @@ -191,13 +191,12 @@ public function run(array $argv, ConsoleIo $io = null) protected function bootstrap() { $this->app->bootstrap(); - if ($this->app instanceof EventApplicationInterface) { - $eventManager = $this->app->events($this->getEventManager()); - $this->setEventManager($eventManager); - } if ($this->app instanceof PluginApplicationInterface) { $this->app->pluginBootstrap(); - $this->app->pluginEvents(); + + $events = $this->app->events($this->getEventManager()); + $events = $this->app->pluginEvents($events); + $this->setEventManager($events); } } diff --git a/src/Core/PluginApplicationInterface.php b/src/Core/PluginApplicationInterface.php index e2de96e435b..bef428e1c9e 100644 --- a/src/Core/PluginApplicationInterface.php +++ b/src/Core/PluginApplicationInterface.php @@ -14,10 +14,13 @@ */ namespace Cake\Core; +use Cake\Event\EventDispatcherInterface; +use Cake\Event\EventManagerInterface; + /** * Interface for Applications that leverage plugins */ -interface PluginApplicationInterface +interface PluginApplicationInterface extends EventDispatcherInterface { /** * Add a plugin to the loaded plugin set. @@ -62,7 +65,16 @@ public function pluginConsole($commands); /** * Run events hook for plugins * - * @return void + * @param \Cake\Event\EventManagerInterface $eventManager Event manager instance. + * @return \Cake\Event\EventManagerInterface + */ + public function pluginEvents(EventManagerInterface $eventManager); + + /** + * Application hook for attaching events to the Application event manager instance. + * + * @param \Cake\Event\EventManagerInterface $eventManager Event manager instance. + * @return \Cake\Event\EventManagerInterface */ - public function pluginEvents(); + public function events(EventManagerInterface $eventManager); } diff --git a/src/Core/composer.json b/src/Core/composer.json index 1023003b364..3d49da99ec8 100644 --- a/src/Core/composer.json +++ b/src/Core/composer.json @@ -25,6 +25,9 @@ "php": ">=5.6.0", "cakephp/utility": "^3.0.0" }, + "suggest": { + "cakephp/event": "To use PluginApplicationInterface or plugin applications." + }, "autoload": { "psr-4": { "Cake\\Core\\": "." diff --git a/src/Event/EventApplicationInterface.php b/src/Event/EventApplicationInterface.php deleted file mode 100644 index a9c797b77b0..00000000000 --- a/src/Event/EventApplicationInterface.php +++ /dev/null @@ -1,26 +0,0 @@ -getEventManager(); - foreach ($this->plugins->with('events') as $plugin) { $events = $plugin->events($events); } - $this->setEventManager($events); + + return $events; } /** diff --git a/src/Http/Server.php b/src/Http/Server.php index 30c15ce7aca..6e54a61dc07 100644 --- a/src/Http/Server.php +++ b/src/Http/Server.php @@ -111,13 +111,13 @@ public function run(ServerRequestInterface $request = null, ResponseInterface $r protected function bootstrap() { $this->app->bootstrap(); - if ($this->app instanceof EventApplicationInterface) { - $eventManager = $this->app->events($this->getEventManager()); - $this->setEventManager($eventManager); - } + if ($this->app instanceof PluginApplicationInterface) { $this->app->pluginBootstrap(); - $this->app->pluginEvents(); + + $events = $this->app->events($this->getEventManager()); + $events = $this->app->pluginEvents($events); + $this->setEventManager($events); } } diff --git a/tests/TestCase/Console/CommandRunnerTest.php b/tests/TestCase/Console/CommandRunnerTest.php index 30501d4cf8e..08804fd699c 100644 --- a/tests/TestCase/Console/CommandRunnerTest.php +++ b/tests/TestCase/Console/CommandRunnerTest.php @@ -400,7 +400,10 @@ public function testRunCallsPluginHookMethods() $app->expects($this->at(0))->method('bootstrap'); $app->expects($this->at(1))->method('pluginBootstrap'); - $app->expects($this->at(2))->method('pluginEvents'); + $app->expects($this->at(2))->method('pluginEvents') + ->will($this->returnCallback(function ($events) { + return $events; + })); $commands = new CommandCollection(); $app->expects($this->at(3)) diff --git a/tests/TestCase/Http/BaseApplicationTest.php b/tests/TestCase/Http/BaseApplicationTest.php index 30fe5c7d507..8558265dade 100644 --- a/tests/TestCase/Http/BaseApplicationTest.php +++ b/tests/TestCase/Http/BaseApplicationTest.php @@ -122,7 +122,7 @@ public function testPluginEvents() $this->assertCount(0, $start->listeners('TestPlugin.load')); $app->addPlugin(TestPlugin::class); - $this->assertNull($app->pluginEvents()); + $this->assertSame($start, $app->pluginEvents($start)); $after = $app->getEventManager(); $this->assertSame($after, $start); diff --git a/tests/TestCase/Http/ServerTest.php b/tests/TestCase/Http/ServerTest.php index f05c6eccfe4..6fb4c1802e8 100644 --- a/tests/TestCase/Http/ServerTest.php +++ b/tests/TestCase/Http/ServerTest.php @@ -127,7 +127,10 @@ public function testRunCallingPluginHooks() $app->expects($this->at(0)) ->method('pluginBootstrap'); $app->expects($this->at(1)) - ->method('pluginBootstrap'); + ->method('pluginEvents') + ->will($this->returnCallback(function ($events) { + return $events; + })); $app->expects($this->at(2)) ->method('pluginMiddleware') ->with($this->isInstanceOf(MiddlewareQueue::class))