Skip to content

Commit

Permalink
Add events hook to plugins.
Browse files Browse the repository at this point in the history
This adds consistency between application hooks and plugin hooks.
  • Loading branch information
markstory committed Feb 19, 2018
1 parent 1e59a8d commit 0cbab2a
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 12 deletions.
3 changes: 3 additions & 0 deletions src/Console/CommandRunner.php
Expand Up @@ -183,6 +183,8 @@ public function run(array $argv, ConsoleIo $io = null)
* Application bootstrap wrapper.
*
* Calls `bootstrap()` and `events()` if application implements `EventApplicationInterface`.
* After the application is bootstrapped and events are attached, plugins are bootstrapped
* and have their events attached.
*
* @return void
*/
Expand All @@ -195,6 +197,7 @@ protected function bootstrap()
}
if ($this->app instanceof PluginApplicationInterface) {
$this->app->pluginBootstrap();
$this->app->pluginEvents();

This comment has been minimized.

Copy link
@robertpustulka

robertpustulka Feb 19, 2018

Member

This should be added to the Server class bootstrap as well.

This comment has been minimized.

Copy link
@markstory

markstory Feb 20, 2018

Author Member

It is.

This comment has been minimized.

Copy link
@robertpustulka

robertpustulka Feb 20, 2018

Member

Sorry, reviewing on a phone sucks.

}
}

Expand Down
14 changes: 14 additions & 0 deletions src/Core/PluginApp.php
Expand Up @@ -32,6 +32,13 @@ class PluginApp implements PluginInterface
*/
protected $bootstrapEnabled = true;

/**
* Are events enabled.
*
* @var bool
*/
protected $eventsEnabled = true;

/**
* Load routes or not
*
Expand Down Expand Up @@ -255,4 +262,11 @@ public function middleware($middleware)
{
return $middleware;
}

/**
* {@inheritdoc}
*/
public function events($events)
{
}
}
7 changes: 7 additions & 0 deletions src/Core/PluginApplicationInterface.php
Expand Up @@ -58,4 +58,11 @@ public function pluginMiddleware($middleware);
* @return \Cake\Console\CommandCollection
*/
public function pluginConsole($commands);

/**
* Run events hook for plugins
*
* @return void
*/
public function pluginEvents();
}
18 changes: 13 additions & 5 deletions src/Core/PluginInterface.php
Expand Up @@ -18,9 +18,9 @@
interface PluginInterface
{
/**
* The list of valid hooks
* List of valid hooks.
*/
const VALID_HOOKS = ['routes', 'bootstrap', 'console', 'middleware'];
const VALID_HOOKS = ['routes', 'bootstrap', 'console', 'middleware', 'events'];

/**
* Get the name of this plugin.
Expand Down Expand Up @@ -60,29 +60,37 @@ public function getClassPath();
public function bootstrap();

/**
* Add a plugins console commands
* Add console commands for the plugin.
*
* @param \Cake\Console\CommandCollection $commands The command collection to update
* @return \Cake\Console\CommandCollection
*/
public function console($commands);

/**
* Add a plugins middleware
* Add middleware for the plugin.
*
* @param \Cake\Http\MiddlewareQueue $middleware The middleware queue to update.
* @return \Cake\Http\MiddlewareQueue
*/
public function middleware($middleware);

/**
* Add a routes
* Add routes for the plugin.
*
* @param \Cake\Routing\RouteBuilder $routes The route builder to update.
* @return \Cake\Routing\RouteBuilder
*/
public function routes($routes);

/**
* Add events for the plugin.
*
* @param \Cake\Event\EventManager $events The application's event manager
* @return void

This comment has been minimized.

Copy link
@robertpustulka

robertpustulka Feb 19, 2018

Member

EventApplicationInterface returns the manager instance. Do we want to do the same here?

*/
public function events($events);

This comment has been minimized.

Copy link
@robertpustulka

robertpustulka Feb 19, 2018

Member

Event application hints an EventManagerInterface. Do we want to add the type hint here as well or maybe remove it in the EventApplication interface to keep the method consistent with other hooks? Type hints can be added back in 4.0


/**
* Disables the named hook
*
Expand Down
12 changes: 12 additions & 0 deletions src/Http/BaseApplication.php
Expand Up @@ -78,6 +78,18 @@ public function __construct($configDir, EventManagerInterface $eventManager = nu
*/
abstract public function middleware($middleware);

/**
* {@inheritDoc}
*/
public function pluginEvents()
{
$events = $this->getEventManager();

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

/**
* {@inheritDoc}
*/
Expand Down
3 changes: 3 additions & 0 deletions src/Http/Server.php
Expand Up @@ -103,6 +103,8 @@ public function run(ServerRequestInterface $request = null, ResponseInterface $r
* Application bootstrap wrapper.
*
* Calls `bootstrap()` and `events()` if application implements `EventApplicationInterface`.
* After the application is bootstrapped and events are attached, plugins are bootstrapped
* and have their events attached.
*
* @return void
*/
Expand All @@ -115,6 +117,7 @@ protected function bootstrap()
}
if ($this->app instanceof PluginApplicationInterface) {
$this->app->pluginBootstrap();
$this->app->pluginEvents();
}
}

Expand Down
10 changes: 6 additions & 4 deletions tests/TestCase/Console/CommandRunnerTest.php
Expand Up @@ -394,14 +394,16 @@ public function testRunTriggersBuildCommandsEvent()
public function testRunCallsPluginHookMethods()
{
$app = $this->getMockBuilder(BaseApplication::class)
->setMethods(['middleware', 'bootstrap', 'pluginBootstrap', 'pluginConsole'])
->setMethods(['middleware', 'bootstrap', 'pluginBootstrap', 'pluginEvents', 'pluginConsole'])
->setConstructorArgs([$this->config])
->getMock();
$app->expects($this->once())
->method('pluginBootstrap');

$app->expects($this->at(0))->method('bootstrap');
$app->expects($this->at(1))->method('pluginBootstrap');
$app->expects($this->at(2))->method('pluginEvents');

$commands = new CommandCollection();
$app->expects($this->once())
$app->expects($this->at(3))
->method('pluginConsole')
->with($this->isinstanceOf(CommandCollection::class))
->will($this->returnCallback(function ($commands) {
Expand Down
8 changes: 8 additions & 0 deletions tests/TestCase/Core/PluginAppTest.php
Expand Up @@ -16,6 +16,7 @@
use Cake\Console\CommandCollection;
use Cake\Core\Plugin;
use Cake\Core\PluginApp;
use Cake\Event\EventManager;
use Cake\Http\MiddlewareQueue;
use Cake\TestSuite\TestCase;
use Company\TestPluginThree\Plugin as TestPluginThree;
Expand Down Expand Up @@ -85,6 +86,13 @@ public function testConsole()
$this->assertSame($commands, $plugin->console($commands));
}

public function testEvents()
{
$plugin = new PluginApp();
$events = new EventManager();
$this->assertNull($plugin->events($events));
}

public function testConstructorArguments()
{
$plugin = new PluginApp([
Expand Down
8 changes: 5 additions & 3 deletions tests/TestCase/Http/ServerTest.php
Expand Up @@ -121,12 +121,14 @@ public function testRunCallingPluginHooks()
$request = $request->withHeader('X-pass', 'request header');

$app = $this->getMockBuilder(MiddlewareApplication::class)
->setMethods(['pluginBootstrap', 'pluginMiddleware'])
->setMethods(['pluginBootstrap', 'pluginEvents', 'pluginMiddleware'])
->setConstructorArgs([$this->config])
->getMock();
$app->expects($this->once())
$app->expects($this->at(0))
->method('pluginBootstrap');
$app->expects($this->once())
$app->expects($this->at(1))
->method('pluginBootstrap');
$app->expects($this->at(2))
->method('pluginMiddleware')
->with($this->isInstanceOf(MiddlewareQueue::class))
->will($this->returnCallback(function ($middleware) {
Expand Down

0 comments on commit 0cbab2a

Please sign in to comment.