From bda7b35423e11ee7275b57e880c14a2ecd333a6a Mon Sep 17 00:00:00 2001 From: Mark Story Date: Fri, 23 Feb 2018 21:31:39 -0500 Subject: [PATCH] Remove events hooks. Now that plugin applications support events out of the box, we can remove the additional hook and leverage the bootstrap hook to attach events. This makes applications and plugins simpler. --- src/Console/CommandRunner.php | 5 --- src/Core/BasePlugin.php | 10 +----- src/Core/PluginApplicationInterface.php | 23 +++----------- src/Core/PluginInterface.php | 16 ++++------ src/Http/BaseApplication.php | 22 +------------- src/Http/Server.php | 5 --- tests/TestCase/Console/CommandRunnerTest.php | 32 +------------------- tests/TestCase/Core/BasePluginTest.php | 13 +++++--- tests/TestCase/Http/BaseApplicationTest.php | 17 ----------- tests/TestCase/Http/ServerTest.php | 24 --------------- 10 files changed, 23 insertions(+), 144 deletions(-) diff --git a/src/Console/CommandRunner.php b/src/Console/CommandRunner.php index 96c70437580..3554418e9a9 100644 --- a/src/Console/CommandRunner.php +++ b/src/Console/CommandRunner.php @@ -184,11 +184,6 @@ protected function bootstrap() $this->app->bootstrap(); if ($this->app instanceof PluginApplicationInterface) { $this->app->pluginBootstrap(); - - $events = $this->app->getEventManager(); - $events = $this->app->events($events); - $events = $this->app->pluginEvents($events); - $this->app->setEventManager($events); } } diff --git a/src/Core/BasePlugin.php b/src/Core/BasePlugin.php index 12cfa4c2bef..4645852a869 100644 --- a/src/Core/BasePlugin.php +++ b/src/Core/BasePlugin.php @@ -240,7 +240,7 @@ public function routes($routes) /** * {@inheritdoc} */ - public function bootstrap() + public function bootstrap(PluginApplicationInterface $app) { $bootstrap = $this->getConfigPath() . DS . 'bootstrap.php'; if (file_exists($bootstrap)) { @@ -263,12 +263,4 @@ public function middleware($middleware) { return $middleware; } - - /** - * {@inheritdoc} - */ - public function events(EventManagerInterface $events) - { - return $events; - } } diff --git a/src/Core/PluginApplicationInterface.php b/src/Core/PluginApplicationInterface.php index bef428e1c9e..b36e151b21a 100644 --- a/src/Core/PluginApplicationInterface.php +++ b/src/Core/PluginApplicationInterface.php @@ -18,7 +18,10 @@ use Cake\Event\EventManagerInterface; /** - * Interface for Applications that leverage plugins + * Interface for Applications that leverage plugins & events. + * + * Events can be bound to the application event manager during + * the application's bootstrap and plugin bootstrap. */ interface PluginApplicationInterface extends EventDispatcherInterface { @@ -32,7 +35,7 @@ interface PluginApplicationInterface extends EventDispatcherInterface public function addPlugin($name, array $config = []); /** - * Run bootstrap logic for loaded plugins + * Run bootstrap logic for loaded plugins. * * @return void */ @@ -61,20 +64,4 @@ public function pluginMiddleware($middleware); * @return \Cake\Console\CommandCollection */ public function pluginConsole($commands); - - /** - * Run events hook for plugins - * - * @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 events(EventManagerInterface $eventManager); } diff --git a/src/Core/PluginInterface.php b/src/Core/PluginInterface.php index 943303eb576..64ff8052118 100644 --- a/src/Core/PluginInterface.php +++ b/src/Core/PluginInterface.php @@ -22,7 +22,7 @@ interface PluginInterface /** * List of valid hooks. */ - const VALID_HOOKS = ['routes', 'bootstrap', 'console', 'middleware', 'events']; + const VALID_HOOKS = ['routes', 'bootstrap', 'console', 'middleware']; /** * Get the name of this plugin. @@ -58,9 +58,13 @@ public function getClassPath(); * The default implementation of this method will include the `config/bootstrap.php` in the plugin if it exist. You * can override this method to replace that behavior. * + * The host application is provided as an argument. This allows you to load additional + * plugin dependencies, or attach events. + * + * @param \Cake\Core\PluginApplicationInterface $app The host application * @return void */ - public function bootstrap(); + public function bootstrap(PluginApplicationInterface $app); /** * Add console commands for the plugin. @@ -89,14 +93,6 @@ public function middleware($middleware); */ public function routes($routes); - /** - * Add events for the plugin. - * - * @param \Cake\Event\EventManagerInterface $events The application's event manager - * @return \Cake\Event\EventManagerInterface The updated event manager. - */ - public function events(EventManagerInterface $events); - /** * Disables the named hook * diff --git a/src/Http/BaseApplication.php b/src/Http/BaseApplication.php index 48543f6ae1e..1c70ff8d1c8 100644 --- a/src/Http/BaseApplication.php +++ b/src/Http/BaseApplication.php @@ -74,18 +74,6 @@ public function __construct($configDir, EventManagerInterface $eventManager = nu */ abstract public function middleware($middleware); - /** - * {@inheritDoc} - */ - public function pluginEvents(EventManagerInterface $events) - { - foreach ($this->plugins->with('events') as $plugin) { - $events = $plugin->events($events); - } - - return $events; - } - /** * {@inheritDoc} */ @@ -160,7 +148,7 @@ public function bootstrap() public function pluginBootstrap() { foreach ($this->plugins->with('bootstrap') as $plugin) { - $plugin->bootstrap(); + $plugin->bootstrap($this); } } @@ -219,14 +207,6 @@ public function pluginConsole($commands) return $commands; } - /** - * {@inheritDoc} - */ - public function events(EventManagerInterface $eventManager) - { - return $eventManager; - } - /** * Invoke the application. * diff --git a/src/Http/Server.php b/src/Http/Server.php index fcfdc481162..98e2407922b 100644 --- a/src/Http/Server.php +++ b/src/Http/Server.php @@ -122,11 +122,6 @@ protected function bootstrap() if ($this->app instanceof PluginApplicationInterface) { $this->app->pluginBootstrap(); - - $events = $this->app->getEventManager(); - $events = $this->app->events($events); - $events = $this->app->pluginEvents($events); - $this->app->setEventManager($events); } } diff --git a/tests/TestCase/Console/CommandRunnerTest.php b/tests/TestCase/Console/CommandRunnerTest.php index deb569ab617..3ba4f3e3803 100644 --- a/tests/TestCase/Console/CommandRunnerTest.php +++ b/tests/TestCase/Console/CommandRunnerTest.php @@ -346,32 +346,6 @@ public function testRunValidCommandClass() $this->assertContains('Demo Command!', $messages); } - /** - * Test running a valid command - * - * @return void - */ - public function testRunEvents() - { - $app = new EventApplication($this->config); - $output = new ConsoleOutput(); - - $manager = $app->getEventManager(); - $manager->setEventList(new EventList()); - - $runner = new CommandRunner($app, 'cake'); - $this->assertSame($manager, $runner->getEventManager()); - - $result = $runner->run(['cake', 'ex'], $this->getMockIo($output)); - $this->assertSame(Shell::CODE_SUCCESS, $result); - - $messages = implode("\n", $output->messages()); - $this->assertContains('Demo Command!', $messages); - - $this->assertCount(1, $manager->listeners('My.event')); - $this->assertEventFired('Console.buildCommands', $manager); - } - /** * Test running a command class' help * @@ -427,13 +401,9 @@ public function testRunCallsPluginHookMethods() $app->expects($this->at(0))->method('bootstrap'); $app->expects($this->at(1))->method('pluginBootstrap'); - $app->expects($this->at(2))->method('pluginEvents') - ->will($this->returnCallback(function ($events) { - return $events; - })); $commands = new CommandCollection(); - $app->expects($this->at(3)) + $app->expects($this->at(2)) ->method('pluginConsole') ->with($this->isinstanceOf(CommandCollection::class)) ->will($this->returnCallback(function ($commands) { diff --git a/tests/TestCase/Core/BasePluginTest.php b/tests/TestCase/Core/BasePluginTest.php index b8a54864ec2..a871aac8cc1 100644 --- a/tests/TestCase/Core/BasePluginTest.php +++ b/tests/TestCase/Core/BasePluginTest.php @@ -15,7 +15,9 @@ use Cake\Console\CommandCollection; use Cake\Core\BasePlugin; +use Cake\Core\Configure; use Cake\Core\Plugin; +use Cake\Core\PluginApplicationInterface; use Cake\Event\EventManager; use Cake\Http\MiddlewareQueue; use Cake\TestSuite\TestCase; @@ -86,11 +88,14 @@ public function testConsole() $this->assertSame($commands, $plugin->console($commands)); } - public function testEvents() + public function testBootstrap() { - $plugin = new BasePlugin(); - $events = new EventManager(); - $this->assertSame($events, $plugin->events($events)); + $app = $this->createMock(PluginApplicationInterface::class); + $plugin = new TestPlugin(); + + $this->assertFalse(Configure::check('PluginTest.test_plugin.bootstrap')); + $this->assertNull($plugin->bootstrap($app)); + $this->assertTrue(Configure::check('PluginTest.test_plugin.bootstrap')); } public function testConstructorArguments() diff --git a/tests/TestCase/Http/BaseApplicationTest.php b/tests/TestCase/Http/BaseApplicationTest.php index 8558265dade..5d8b3e609e1 100644 --- a/tests/TestCase/Http/BaseApplicationTest.php +++ b/tests/TestCase/Http/BaseApplicationTest.php @@ -112,23 +112,6 @@ public function testAddPluginValid() $this->assertTrue($app->getPlugins()->has('TestPlugin')); } - public function testPluginEvents() - { - $app = $this->getMockForAbstractClass( - BaseApplication::class, - [$this->path] - ); - $start = $app->getEventManager(); - $this->assertCount(0, $start->listeners('TestPlugin.load')); - - $app->addPlugin(TestPlugin::class); - $this->assertSame($start, $app->pluginEvents($start)); - - $after = $app->getEventManager(); - $this->assertSame($after, $start); - $this->assertCount(1, $after->listeners('TestPlugin.load')); - } - public function testPluginMiddleware() { $start = new MiddlewareQueue(); diff --git a/tests/TestCase/Http/ServerTest.php b/tests/TestCase/Http/ServerTest.php index 4e2702fcd0c..1b21fe4069e 100644 --- a/tests/TestCase/Http/ServerTest.php +++ b/tests/TestCase/Http/ServerTest.php @@ -130,11 +130,6 @@ public function testRunCallingPluginHooks() $app->expects($this->at(0)) ->method('pluginBootstrap'); $app->expects($this->at(1)) - ->method('pluginEvents') - ->will($this->returnCallback(function ($events) { - return $events; - })); - $app->expects($this->at(2)) ->method('pluginMiddleware') ->with($this->isInstanceOf(MiddlewareQueue::class)) ->will($this->returnCallback(function ($middleware) { @@ -217,25 +212,6 @@ public function testRunMiddlewareNoResponse() $server->run(); } - /** - * Test application events. - * - * @return void - */ - public function testRunEvents() - { - $manager = new EventManager(); - $manager->setEventList(new EventList()); - $app = new EventApplication($this->config, $manager); - - $server = new Server($app); - $res = $server->run(); - - $this->assertCount(1, $manager->listeners('My.event')); - $this->assertEventFired('Server.buildMiddleware', $manager); - $this->assertInstanceOf(ResponseInterface::class, $res); - } - /** * Test that emit invokes the appropriate methods on the emitter. *