Skip to content

Commit

Permalink
Remove events hooks.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
markstory committed Feb 24, 2018
1 parent 7b059d4 commit bda7b35
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 144 deletions.
5 changes: 0 additions & 5 deletions src/Console/CommandRunner.php
Expand Up @@ -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);
}
}

Expand Down
10 changes: 1 addition & 9 deletions src/Core/BasePlugin.php
Expand Up @@ -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)) {
Expand All @@ -263,12 +263,4 @@ public function middleware($middleware)
{
return $middleware;
}

/**
* {@inheritdoc}
*/
public function events(EventManagerInterface $events)
{
return $events;
}
}
23 changes: 5 additions & 18 deletions src/Core/PluginApplicationInterface.php
Expand Up @@ -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
{
Expand All @@ -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
*/
Expand Down Expand Up @@ -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);
}
16 changes: 6 additions & 10 deletions src/Core/PluginInterface.php
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
*
Expand Down
22 changes: 1 addition & 21 deletions src/Http/BaseApplication.php
Expand Up @@ -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}
*/
Expand Down Expand Up @@ -160,7 +148,7 @@ public function bootstrap()
public function pluginBootstrap()
{
foreach ($this->plugins->with('bootstrap') as $plugin) {
$plugin->bootstrap();
$plugin->bootstrap($this);
}
}

Expand Down Expand Up @@ -219,14 +207,6 @@ public function pluginConsole($commands)
return $commands;
}

/**
* {@inheritDoc}
*/
public function events(EventManagerInterface $eventManager)
{
return $eventManager;
}

/**
* Invoke the application.
*
Expand Down
5 changes: 0 additions & 5 deletions src/Http/Server.php
Expand Up @@ -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);
}
}

Expand Down
32 changes: 1 addition & 31 deletions tests/TestCase/Console/CommandRunnerTest.php
Expand Up @@ -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
*
Expand Down Expand Up @@ -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) {
Expand Down
13 changes: 9 additions & 4 deletions tests/TestCase/Core/BasePluginTest.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down
17 changes: 0 additions & 17 deletions tests/TestCase/Http/BaseApplicationTest.php
Expand Up @@ -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();
Expand Down
24 changes: 0 additions & 24 deletions tests/TestCase/Http/ServerTest.php
Expand Up @@ -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) {
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit bda7b35

Please sign in to comment.