Skip to content

Commit

Permalink
Allow plugins to disable / enable bootstrap and routes
Browse files Browse the repository at this point in the history
  • Loading branch information
burzum authored and markstory committed Feb 13, 2018
1 parent 5d12b92 commit e01065e
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 18 deletions.
86 changes: 78 additions & 8 deletions src/Core/PluginApp.php
@@ -1,15 +1,58 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright 2005-2011, Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 3.6.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Core;

use Cake\Routing\RouteBuilder;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

class PluginApp implements ConsoleApplicationInterface, HttpApplicationInterface
/**
* Base Plugin Class
*
* Every plugin should extends from this class or implement the interfaces and
* include a plugin class in it's src root folder.
*/
class PluginApp implements ConsoleApplicationInterface, HttpApplicationInterface, PluginInterface
{

/**
* Do bootstrapping or not
*
* @var bool
*/
protected $doBootstrap = true;

/**
* Load routes or not
*
* @var bool
*/
protected $loadRoutes = true;

/**
* Constructor
*
* @param array $options Options
*/
public function __construct(array $options = [])
{
if (isset($options['bootstrap'])) {
$this->disableBootstrap((bool)$options['bootstrap']);
}
if (isset($options['routes'])) {
$this->disableRoutes((bool)$options['routes']);
}

$this->initialize();
}

Expand All @@ -21,6 +64,38 @@ public function initialize()

}

/**
* {@inheritdoc}
*/
public function disableRoutes($disabled)
{
$this->loadRoutes = $disabled;
}

/**
* {@inheritdoc}
*/
public function disableBootstrap($disabled)
{
$this->doBootstrap = $disabled;
}

/**
* {@inheritdoc}
*/
public function isRouteLoadingEnabled()
{
return $this->loadRoutes;
}

/**
* {@inheritdoc}
*/
public function isBootstrapEnabled()
{
return $this->doBootstrap;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -60,12 +135,7 @@ public function middleware($middleware)
}

/**
* Invoke the application.
*
* @param \Psr\Http\Message\ServerRequestInterface $request The request
* @param \Psr\Http\Message\ResponseInterface $response The response
* @param callable $next The next middleware
* @return \Psr\Http\Message\ResponseInterface
* {@inheritdoc}
*/
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $next)
{
Expand Down
51 changes: 51 additions & 0 deletions src/Core/PluginInterface.php
@@ -0,0 +1,51 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright 2005-2011, Cake Software Foundation, Inc. (https://cakefoundation.org)
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 3.6.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/

namespace Cake\Core;

/**
* Plugin Interface
*/
interface PluginInterface
{

/**
* Disables route loading for the plugin
*
* @param bool $disabled True to disable it, false to enable it
* @return $this
*/
public function disableRoutes($disabled);

/**
* Disables bootstrapping for the plugin
*
* @param bool $disabled True to disable it, false to enable it
* @return $this
*/
public function disableBootstrap($disabled);

/**
* If the routes should be loaded or not for this plugin
*
* @return bool
*/
public function isRouteLoadingEnabled();

/**
* If bootstrapping should be done or not for this plugin
*
* @return bool
*/
public function isBootstrapEnabled();
}
34 changes: 28 additions & 6 deletions src/Core/PluginRegistry.php
Expand Up @@ -31,6 +31,18 @@ class PluginRegistry extends ObjectRegistry implements ConsoleApplicationInterfa
*/
protected function _resolveClassName($class)
{
if (class_exists($class)) {
return $class;
}

list($plugin, $name) = pluginSplit($class, true);

if (empty($plugin)) {
$class = $name . '\\' . 'Plugin';
} else {
$class = $plugin . $name;
}

return $class;
}

Expand All @@ -45,7 +57,7 @@ protected function _resolveClassName($class)
protected function _throwMissingClassError($class, $plugin)
{
new RuntimeException(sprintf(
'Plugin class `%s` is missing',
'Plugin class `%s` not found.',
$class
));
}
Expand All @@ -64,9 +76,15 @@ protected function _create($class, $alias, $config)
{
$instance = new $class($config);

if (!$instance instanceof HttpApplicationInterface
&& !$instance instanceof ConsoleApplicationInterface) {
throw new \RuntimeException(sprintf('%s is not a valid plugin .', get_class($instance)));
if ((!$instance instanceof HttpApplicationInterface
&& !$instance instanceof ConsoleApplicationInterface)
|| !$instance instanceof PluginInterface) {
throw new \RuntimeException(sprintf(
'`%s` is not a valid plugin object. It\'s not implementing `%s` or `%s`',
get_class($instance)),
HttpApplicationInterface::class,
ConsoleApplicationInterface::class
);
}

return $instance;
Expand All @@ -81,7 +99,9 @@ protected function _create($class, $alias, $config)
public function bootstrap()
{
foreach ($this as $plugin) {
$plugin->bootstrap();
if ($plugin->isBootstrapEnabled()) {
$plugin->bootstrap();
}
}
}

Expand Down Expand Up @@ -110,7 +130,9 @@ public function console($commands)
public function routes($routes)
{
foreach ($this as $plugin) {
$plugin->middleware($routes);
if ($plugin->isRouteLoadingEnabled()) {
$plugin->routes($routes);
}
}
}

Expand Down
22 changes: 19 additions & 3 deletions tests/TestCase/Core/PluginAppTest.php
Expand Up @@ -13,10 +13,9 @@
*/
namespace Cake\Test\TestCase\Core;

use Cake\Core\App;
use Cake\Core\Plugin;
use Cake\Core\PluginApp;
use Cake\TestSuite\TestCase;
use TestApp\Core\TestApp;

/**
* AppTest class
Expand All @@ -29,8 +28,25 @@ class PluginAppTest extends TestCase
*
* @return void
*/
public function tearDown() {
public function tearDown()
{
parent::tearDown();
Plugin::unload();
}

/**
* testConfigForRoutesAndBootstrap
*
* @return void
*/
public function testConfigForRoutesAndBootstrap()
{
$plugin = new PluginApp([
'bootstrap' => false,
'routes' => false
]);

$this->assertFalse($plugin->isBootstrapEnabled());
$this->assertFalse($plugin->isRouteLoadingEnabled());
}
}
23 changes: 23 additions & 0 deletions tests/TestCase/Core/PluginRegistryTest.php
Expand Up @@ -42,4 +42,27 @@ public function testRegistry()
$result = $registry->loaded();
$this->assertEquals(['TestPlugin'], $result);
}

public function testDotSyntax()
{
/*
$registry = new PluginRegistry();
//$result = $registry->load('TestPlugin');
//$this->assertInstanceOf(TestPlugin::class, $result);
$result = $registry->load('Company\TestPluginFive');
//$this->assertInstanceOf(TestPlugin::class, $result);
debug($result);
*/
}

/**
* @expectedException \Error
* @expectedExceptionMessage Class 'DoesNotExist\Plugin' not found
*/
public function testClassNotFoundException()
{
$registry = new PluginRegistry();
$registry->load('DoesNotExist');
}
}
Expand Up @@ -11,7 +11,7 @@
* @since 3.6.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace TestPlugin;
namespace Company\TestPluginFive;

use Cake\Core\PluginApp;

Expand Down

0 comments on commit e01065e

Please sign in to comment.