Skip to content

Commit e01065e

Browse files
burzummarkstory
authored andcommitted
Allow plugins to disable / enable bootstrap and routes
1 parent 5d12b92 commit e01065e

File tree

6 files changed

+200
-18
lines changed

6 files changed

+200
-18
lines changed

src/Core/PluginApp.php

Lines changed: 78 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,58 @@
11
<?php
2+
/**
3+
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
4+
* Copyright 2005-2011, Cake Software Foundation, Inc. (https://cakefoundation.org)
5+
*
6+
* Licensed under The MIT License
7+
* Redistributions of files must retain the above copyright notice.
8+
*
9+
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
10+
* @link https://cakephp.org CakePHP(tm) Project
11+
* @since 3.6.0
12+
* @license https://opensource.org/licenses/mit-license.php MIT License
13+
*/
214
namespace Cake\Core;
315

4-
use Cake\Routing\RouteBuilder;
516
use Psr\Http\Message\ResponseInterface;
617
use Psr\Http\Message\ServerRequestInterface;
718

8-
class PluginApp implements ConsoleApplicationInterface, HttpApplicationInterface
19+
/**
20+
* Base Plugin Class
21+
*
22+
* Every plugin should extends from this class or implement the interfaces and
23+
* include a plugin class in it's src root folder.
24+
*/
25+
class PluginApp implements ConsoleApplicationInterface, HttpApplicationInterface, PluginInterface
926
{
1027

28+
/**
29+
* Do bootstrapping or not
30+
*
31+
* @var bool
32+
*/
33+
protected $doBootstrap = true;
34+
35+
/**
36+
* Load routes or not
37+
*
38+
* @var bool
39+
*/
40+
protected $loadRoutes = true;
41+
42+
/**
43+
* Constructor
44+
*
45+
* @param array $options Options
46+
*/
1147
public function __construct(array $options = [])
1248
{
49+
if (isset($options['bootstrap'])) {
50+
$this->disableBootstrap((bool)$options['bootstrap']);
51+
}
52+
if (isset($options['routes'])) {
53+
$this->disableRoutes((bool)$options['routes']);
54+
}
55+
1356
$this->initialize();
1457
}
1558

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

2265
}
2366

67+
/**
68+
* {@inheritdoc}
69+
*/
70+
public function disableRoutes($disabled)
71+
{
72+
$this->loadRoutes = $disabled;
73+
}
74+
75+
/**
76+
* {@inheritdoc}
77+
*/
78+
public function disableBootstrap($disabled)
79+
{
80+
$this->doBootstrap = $disabled;
81+
}
82+
83+
/**
84+
* {@inheritdoc}
85+
*/
86+
public function isRouteLoadingEnabled()
87+
{
88+
return $this->loadRoutes;
89+
}
90+
91+
/**
92+
* {@inheritdoc}
93+
*/
94+
public function isBootstrapEnabled()
95+
{
96+
return $this->doBootstrap;
97+
}
98+
2499
/**
25100
* {@inheritdoc}
26101
*/
@@ -60,12 +135,7 @@ public function middleware($middleware)
60135
}
61136

62137
/**
63-
* Invoke the application.
64-
*
65-
* @param \Psr\Http\Message\ServerRequestInterface $request The request
66-
* @param \Psr\Http\Message\ResponseInterface $response The response
67-
* @param callable $next The next middleware
68-
* @return \Psr\Http\Message\ResponseInterface
138+
* {@inheritdoc}
69139
*/
70140
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $next)
71141
{

src/Core/PluginInterface.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
4+
* Copyright 2005-2011, Cake Software Foundation, Inc. (https://cakefoundation.org)
5+
* Licensed under The MIT License
6+
* Redistributions of files must retain the above copyright notice.
7+
*
8+
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
9+
* @link https://cakephp.org CakePHP(tm) Project
10+
* @since 3.6.0
11+
* @license https://opensource.org/licenses/mit-license.php MIT License
12+
*/
13+
14+
namespace Cake\Core;
15+
16+
/**
17+
* Plugin Interface
18+
*/
19+
interface PluginInterface
20+
{
21+
22+
/**
23+
* Disables route loading for the plugin
24+
*
25+
* @param bool $disabled True to disable it, false to enable it
26+
* @return $this
27+
*/
28+
public function disableRoutes($disabled);
29+
30+
/**
31+
* Disables bootstrapping for the plugin
32+
*
33+
* @param bool $disabled True to disable it, false to enable it
34+
* @return $this
35+
*/
36+
public function disableBootstrap($disabled);
37+
38+
/**
39+
* If the routes should be loaded or not for this plugin
40+
*
41+
* @return bool
42+
*/
43+
public function isRouteLoadingEnabled();
44+
45+
/**
46+
* If bootstrapping should be done or not for this plugin
47+
*
48+
* @return bool
49+
*/
50+
public function isBootstrapEnabled();
51+
}

src/Core/PluginRegistry.php

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ class PluginRegistry extends ObjectRegistry implements ConsoleApplicationInterfa
3131
*/
3232
protected function _resolveClassName($class)
3333
{
34+
if (class_exists($class)) {
35+
return $class;
36+
}
37+
38+
list($plugin, $name) = pluginSplit($class, true);
39+
40+
if (empty($plugin)) {
41+
$class = $name . '\\' . 'Plugin';
42+
} else {
43+
$class = $plugin . $name;
44+
}
45+
3446
return $class;
3547
}
3648

@@ -45,7 +57,7 @@ protected function _resolveClassName($class)
4557
protected function _throwMissingClassError($class, $plugin)
4658
{
4759
new RuntimeException(sprintf(
48-
'Plugin class `%s` is missing',
60+
'Plugin class `%s` not found.',
4961
$class
5062
));
5163
}
@@ -64,9 +76,15 @@ protected function _create($class, $alias, $config)
6476
{
6577
$instance = new $class($config);
6678

67-
if (!$instance instanceof HttpApplicationInterface
68-
&& !$instance instanceof ConsoleApplicationInterface) {
69-
throw new \RuntimeException(sprintf('%s is not a valid plugin .', get_class($instance)));
79+
if ((!$instance instanceof HttpApplicationInterface
80+
&& !$instance instanceof ConsoleApplicationInterface)
81+
|| !$instance instanceof PluginInterface) {
82+
throw new \RuntimeException(sprintf(
83+
'`%s` is not a valid plugin object. It\'s not implementing `%s` or `%s`',
84+
get_class($instance)),
85+
HttpApplicationInterface::class,
86+
ConsoleApplicationInterface::class
87+
);
7088
}
7189

7290
return $instance;
@@ -81,7 +99,9 @@ protected function _create($class, $alias, $config)
8199
public function bootstrap()
82100
{
83101
foreach ($this as $plugin) {
84-
$plugin->bootstrap();
102+
if ($plugin->isBootstrapEnabled()) {
103+
$plugin->bootstrap();
104+
}
85105
}
86106
}
87107

@@ -110,7 +130,9 @@ public function console($commands)
110130
public function routes($routes)
111131
{
112132
foreach ($this as $plugin) {
113-
$plugin->middleware($routes);
133+
if ($plugin->isRouteLoadingEnabled()) {
134+
$plugin->routes($routes);
135+
}
114136
}
115137
}
116138

tests/TestCase/Core/PluginAppTest.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@
1313
*/
1414
namespace Cake\Test\TestCase\Core;
1515

16-
use Cake\Core\App;
1716
use Cake\Core\Plugin;
17+
use Cake\Core\PluginApp;
1818
use Cake\TestSuite\TestCase;
19-
use TestApp\Core\TestApp;
2019

2120
/**
2221
* AppTest class
@@ -29,8 +28,25 @@ class PluginAppTest extends TestCase
2928
*
3029
* @return void
3130
*/
32-
public function tearDown() {
31+
public function tearDown()
32+
{
3333
parent::tearDown();
3434
Plugin::unload();
3535
}
36+
37+
/**
38+
* testConfigForRoutesAndBootstrap
39+
*
40+
* @return void
41+
*/
42+
public function testConfigForRoutesAndBootstrap()
43+
{
44+
$plugin = new PluginApp([
45+
'bootstrap' => false,
46+
'routes' => false
47+
]);
48+
49+
$this->assertFalse($plugin->isBootstrapEnabled());
50+
$this->assertFalse($plugin->isRouteLoadingEnabled());
51+
}
3652
}

tests/TestCase/Core/PluginRegistryTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,27 @@ public function testRegistry()
4242
$result = $registry->loaded();
4343
$this->assertEquals(['TestPlugin'], $result);
4444
}
45+
46+
public function testDotSyntax()
47+
{
48+
/*
49+
$registry = new PluginRegistry();
50+
//$result = $registry->load('TestPlugin');
51+
//$this->assertInstanceOf(TestPlugin::class, $result);
52+
53+
$result = $registry->load('Company\TestPluginFive');
54+
//$this->assertInstanceOf(TestPlugin::class, $result);
55+
debug($result);
56+
*/
57+
}
58+
59+
/**
60+
* @expectedException \Error
61+
* @expectedExceptionMessage Class 'DoesNotExist\Plugin' not found
62+
*/
63+
public function testClassNotFoundException()
64+
{
65+
$registry = new PluginRegistry();
66+
$registry->load('DoesNotExist');
67+
}
4568
}

tests/test_app/Plugin/Company/TestPluginFive/src/Plugin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* @since 3.6.0
1212
* @license https://opensource.org/licenses/mit-license.php MIT License
1313
*/
14-
namespace TestPlugin;
14+
namespace Company\TestPluginFive;
1515

1616
use Cake\Core\PluginApp;
1717

0 commit comments

Comments
 (0)