Skip to content

Commit

Permalink
Integrate new plugin hooks into the console runner
Browse files Browse the repository at this point in the history
Add the new plugin hooks to the console runner.
  • Loading branch information
markstory committed Feb 13, 2018
1 parent d3f8b55 commit bf8e83c
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 46 deletions.
9 changes: 9 additions & 0 deletions src/Console/CommandRunner.php
Expand Up @@ -22,8 +22,10 @@
use Cake\Console\Exception\StopException;
use Cake\Console\Shell;
use Cake\Core\ConsoleApplicationInterface;
use Cake\Core\PluginApplicationInterface;
use Cake\Event\EventDispatcherInterface;
use Cake\Event\EventDispatcherTrait;
use Cake\Event\EventManagerTrait;
use Cake\Utility\Inflector;
use RuntimeException;

Expand Down Expand Up @@ -112,13 +114,20 @@ public function setAliases(array $aliases)
*/
public function run(array $argv, ConsoleIo $io = null)
{
$hasPlugins = $this->app instanceof PluginApplicationInterface;
$this->app->bootstrap();
if ($hasPlugins) {
$this->app->pluginBootstrap();
}

$commands = new CommandCollection([
'version' => VersionCommand::class,
'help' => HelpCommand::class,
]);
$commands = $this->app->console($commands);
if ($hasPlugins) {
$commands = $this->app->pluginConsole($commands);
}

if (!($commands instanceof CommandCollection)) {
$type = getTypeName($commands);
Expand Down
105 changes: 59 additions & 46 deletions src/Http/BaseApplication.php
Expand Up @@ -16,8 +16,8 @@

use Cake\Core\ConsoleApplicationInterface;
use Cake\Core\HttpApplicationInterface;
use Cake\Core\PluginRegistry;
use Cake\Core\PluginRegistryInterface;
use Cake\Core\PluginApplicationInterface;
use Cake\Core\PluginCollection;
use Cake\Routing\DispatcherFactory;
use Cake\Routing\Router;
use InvalidArgumentException;
Expand All @@ -31,7 +31,10 @@
* and ensuring that middleware is attached. It is also invoked as the last piece
* of middleware, and delegates request/response handling to the correct controller.
*/
abstract class BaseApplication implements ConsoleApplicationInterface, HttpApplicationInterface
abstract class BaseApplication implements
ConsoleApplicationInterface,
HttpApplicationInterface,
PluginApplicationInterface
{

/**
Expand All @@ -40,18 +43,11 @@ abstract class BaseApplication implements ConsoleApplicationInterface, HttpAppli
protected $configDir;

/**
* Plugin Registry
* Plugin Collection
*
* @param \Cake\Core\PluginRegistry
* @param \Cake\Core\PluginCollection
*/
protected $pluginRegistry;

/**
* Default Plugin Registry Class
*
* @var string
*/
protected $defaultPluginRegistry = PluginRegistry::class;
protected $plugins;

/**
* Constructor
Expand All @@ -62,40 +58,33 @@ abstract class BaseApplication implements ConsoleApplicationInterface, HttpAppli
public function __construct($configDir, $pluginRegistry = null)
{
$this->configDir = $configDir;

$this->setPluginRegistry($pluginRegistry);
$this->plugins = new PluginCollection();
}

/**
* Sets the plugin registry
*
* @param string|null $pluginRegistry Plugin Registry Object
* @return void
* @param \Cake\Http\MiddlewareQueue $middleware The middleware queue to set in your App Class
* @return \Cake\Http\MiddlewareQueue
*/
public function setPluginRegistry($pluginRegistry = null)
{
if (empty($pluginRegistry)) {
$this->pluginRegistry = new $this->defaultPluginRegistry();

return;
}
abstract public function middleware($middleware);

if (!$pluginRegistry instanceof PluginRegistryInterface) {
throw new InvalidArgumentException(sprintf(
'`%s` is not an instance of `%s`',
get_class($pluginRegistry),
PluginRegistryInterface::class
));
/**
* {@inheritDoc}
*/
public function pluginMiddleware($middleware)
{
foreach ($this->plugins->with('middleware') as $plugin) {
$middleware = $plugin->middleware($middleware);
}

$this->pluginRegistry = $pluginRegistry;
return $middleware;
}

/**
* @param \Cake\Http\MiddlewareQueue $middleware The middleware queue to set in your App Class
* @return \Cake\Http\MiddlewareQueue
* {@inheritDoc}
*/
abstract public function middleware($middleware);
public function addPlugin($name, array $config = [])
{
}

/**
* {@inheritDoc}
Expand All @@ -107,6 +96,16 @@ public function bootstrap()
$this->pluginRegistry()->bootstrap();
}

/**
* {@inheritDoc}
*/
public function pluginBootstrap()
{
foreach ($this->plugins->with('bootstrap') as $plugin) {
$plugin->bootstrap();
}
}

/**
* {@inheritDoc}
*
Expand All @@ -124,6 +123,18 @@ public function routes($routes)
}
}

/**
* {@inheritDoc}
*/
public function pluginRoutes($routes)
{
foreach ($this->plugins->with('routes') as $plugin) {
$routes = $plugin->routes($routes);
}

return $routes;
}

/**
* Define the console commands for an application.
*
Expand All @@ -140,6 +151,18 @@ public function console($commands)
return $this->pluginRegistry()->console($commands);
}

/**
* {@inheritDoc}
*/
public function pluginConsole($commands)
{
foreach ($this->plugins->with('console') as $plugin) {
$commands = $plugin->console($commands);
}

return $commands;
}

/**
* Invoke the application.
*
Expand All @@ -166,14 +189,4 @@ protected function getDispatcher()
{
return new ActionDispatcher(null, null, DispatcherFactory::filters());
}

/**
* Plugins
*
* @return \Cake\Core\PluginRegistry
*/
public function getPluginRegistry()
{
return $this->pluginRegistry;
}
}
28 changes: 28 additions & 0 deletions tests/TestCase/Console/CommandRunnerTest.php
Expand Up @@ -338,6 +338,34 @@ public function testRunTriggersBuildCommandsEvent()
$this->assertTrue($this->eventTriggered, 'Should have triggered event.');
}

/**
* Test that run calls plugin hook methods
*
* @return void
*/
public function testRunCallsPluginHookMethods()
{
$app = $this->getMockBuilder(BaseApplication::class)
->setMethods(['middleware', 'bootstrap', 'pluginBootstrap', 'pluginConsole'])
->setConstructorArgs([$this->config])
->getMock();
$app->expects($this->once())
->method('pluginBootstrap');

$commands = new CommandCollection();
$app->expects($this->once())
->method('pluginConsole')
->with($this->isinstanceOf(CommandCollection::class))
->will($this->returnCallback(function ($commands) {
return $commands;
}));

$output = new ConsoleOutput();
$runner = new CommandRunner($app, 'cake');
$result = $runner->run(['cake', '--version'], $this->getMockIo($output));
$this->assertContains(Configure::version(), $output->messages()[0]);
}

protected function makeAppWithCommands($commands)
{
$app = $this->getMockBuilder(BaseApplication::class)
Expand Down

0 comments on commit bf8e83c

Please sign in to comment.