Browse Source

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.
Mark Story 8 years ago
parent
commit
bda7b35423

+ 0 - 5
src/Console/CommandRunner.php

@@ -184,11 +184,6 @@ class CommandRunner implements EventDispatcherInterface
         $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);
         }
     }
 

+ 1 - 9
src/Core/BasePlugin.php

@@ -240,7 +240,7 @@ class BasePlugin implements PluginInterface
     /**
      * {@inheritdoc}
      */
-    public function bootstrap()
+    public function bootstrap(PluginApplicationInterface $app)
     {
         $bootstrap = $this->getConfigPath() . DS . 'bootstrap.php';
         if (file_exists($bootstrap)) {
@@ -263,12 +263,4 @@ class BasePlugin implements PluginInterface
     {
         return $middleware;
     }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function events(EventManagerInterface $events)
-    {
-        return $events;
-    }
 }

+ 5 - 18
src/Core/PluginApplicationInterface.php

@@ -18,7 +18,10 @@ use Cake\Event\EventDispatcherInterface;
 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 @@ interface PluginApplicationInterface extends EventDispatcherInterface
      * @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);
 }

+ 6 - 10
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 @@ interface PluginInterface
      * 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.
@@ -90,14 +94,6 @@ interface PluginInterface
     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
      *
      * @param string $hook The hook to disable

+ 1 - 21
src/Http/BaseApplication.php

@@ -77,18 +77,6 @@ abstract class BaseApplication implements
     /**
      * {@inheritDoc}
      */
-    public function pluginEvents(EventManagerInterface $events)
-    {
-        foreach ($this->plugins->with('events') as $plugin) {
-            $events = $plugin->events($events);
-        }
-
-        return $events;
-    }
-
-    /**
-     * {@inheritDoc}
-     */
     public function pluginMiddleware($middleware)
     {
         foreach ($this->plugins->with('middleware') as $plugin) {
@@ -160,7 +148,7 @@ abstract class BaseApplication implements
     public function pluginBootstrap()
     {
         foreach ($this->plugins->with('bootstrap') as $plugin) {
-            $plugin->bootstrap();
+            $plugin->bootstrap($this);
         }
     }
 
@@ -220,14 +208,6 @@ abstract class BaseApplication implements
     }
 
     /**
-     * {@inheritDoc}
-     */
-    public function events(EventManagerInterface $eventManager)
-    {
-        return $eventManager;
-    }
-
-    /**
      * Invoke the application.
      *
      * - Convert the PSR response into CakePHP equivalents.

+ 0 - 5
src/Http/Server.php

@@ -122,11 +122,6 @@ class Server implements EventDispatcherInterface
 
         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);
         }
     }
 

+ 1 - 31
tests/TestCase/Console/CommandRunnerTest.php

@@ -347,32 +347,6 @@ class CommandRunnerTest extends TestCase
     }
 
     /**
-     * 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
      *
      * @return void
@@ -427,13 +401,9 @@ class CommandRunnerTest extends TestCase
 
         $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) {

+ 9 - 4
tests/TestCase/Core/BasePluginTest.php

@@ -15,7 +15,9 @@ namespace Cake\Test\TestCase\Core;
 
 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 @@ class BasePluginTest extends TestCase
         $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()

+ 0 - 17
tests/TestCase/Http/BaseApplicationTest.php

@@ -112,23 +112,6 @@ class BaseApplicationTest extends TestCase
         $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();

+ 0 - 24
tests/TestCase/Http/ServerTest.php

@@ -130,11 +130,6 @@ class ServerTest extends TestCase
         $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) {
@@ -218,25 +213,6 @@ class ServerTest extends TestCase
     }
 
     /**
-     * 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.
      *
      * @return void