Skip to content

Commit

Permalink
Merge the EventApplication and PluginApplication interfaces.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
markstory committed Feb 23, 2018
1 parent afd1fd6 commit 7f56814
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 50 deletions.
9 changes: 4 additions & 5 deletions src/Console/CommandRunner.php
Expand Up @@ -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);
}
}

Expand Down
18 changes: 15 additions & 3 deletions src/Core/PluginApplicationInterface.php
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
3 changes: 3 additions & 0 deletions src/Core/composer.json
Expand Up @@ -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\\": "."
Expand Down
26 changes: 0 additions & 26 deletions src/Event/EventApplicationInterface.php

This file was deleted.

11 changes: 3 additions & 8 deletions src/Http/BaseApplication.php
Expand Up @@ -19,8 +19,6 @@
use Cake\Core\Plugin;
use Cake\Core\PluginApplicationInterface;
use Cake\Core\PluginInterface;
use Cake\Event\EventApplicationInterface;
use Cake\Event\EventDispatcherInterface;
use Cake\Event\EventDispatcherTrait;
use Cake\Event\EventManager;
use Cake\Event\EventManagerInterface;
Expand All @@ -39,8 +37,6 @@
*/
abstract class BaseApplication implements
ConsoleApplicationInterface,
EventApplicationInterface,
EventDispatcherInterface,
HttpApplicationInterface,
PluginApplicationInterface
{
Expand Down Expand Up @@ -81,14 +77,13 @@ abstract public function middleware($middleware);
/**
* {@inheritDoc}
*/
public function pluginEvents()
public function pluginEvents($events)
{
$events = $this->getEventManager();

foreach ($this->plugins->with('events') as $plugin) {
$events = $plugin->events($events);
}
$this->setEventManager($events);

return $events;
}

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

Expand Down
5 changes: 4 additions & 1 deletion tests/TestCase/Console/CommandRunnerTest.php
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Http/BaseApplicationTest.php
Expand Up @@ -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);
Expand Down
5 changes: 4 additions & 1 deletion tests/TestCase/Http/ServerTest.php
Expand Up @@ -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))
Expand Down

0 comments on commit 7f56814

Please sign in to comment.