Skip to content

Commit b335138

Browse files
authored
Merge pull request #971 from cakephp/5.x-plugins-config
5.x: use plugin loading via configuration array
2 parents 259132b + 53230d9 commit b335138

File tree

5 files changed

+39
-55
lines changed

5 files changed

+39
-55
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"license": "MIT",
77
"require": {
88
"php": ">=8.1",
9-
"cakephp/cakephp": "^5.0.0",
9+
"cakephp/cakephp": "^5.0.1",
1010
"cakephp/migrations": "^4.0.0",
1111
"cakephp/plugin-installer": "^2.0",
1212
"mobiledetect/mobiledetectlib": "^3.74"

config/plugins.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* Plugin configuration.
4+
*
5+
* In this file, you configure which plugins are loaded in the different states your app can be.
6+
* It's loaded via the `parent::bootstrap();` call inside your `Application::bootstrap()` method.
7+
* For more information see https://book.cakephp.org/5/en/plugins.html#loading-plugins-via-configuration-array
8+
*
9+
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
10+
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
11+
*
12+
* Licensed under The MIT License
13+
* For full copyright and license information, please see the LICENSE.txt
14+
* Redistributions of files must retain the above copyright notice.
15+
*
16+
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
17+
* @link https://cakephp.org CakePHP(tm) Project
18+
* @since 5.0.0
19+
* @license https://opensource.org/licenses/mit-license.php MIT License
20+
*/
21+
22+
return [
23+
// Plugins only needed when in debug mode
24+
'DebugKit' => ['onlyDebug' => true],
25+
26+
// Optional plugins which are only needed in CLI commands
27+
'Bake' => ['onlyCli' => true, 'optional' => true],
28+
29+
// Required plugins only in CLI commands
30+
'Migrations' => ['onlyCli' => true],
31+
32+
// Add your custom plugins here
33+
];

src/Application.php

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -48,24 +48,12 @@ public function bootstrap(): void
4848
// Call parent to load bootstrap from files.
4949
parent::bootstrap();
5050

51-
if (PHP_SAPI === 'cli') {
52-
$this->bootstrapCli();
53-
} else {
51+
if (PHP_SAPI !== 'cli') {
5452
FactoryLocator::add(
5553
'Table',
5654
(new TableLocator())->allowFallbackClass(false)
5755
);
5856
}
59-
60-
/*
61-
* Only try to load DebugKit in development mode
62-
* Debug Kit should not be installed on a production system
63-
*/
64-
if (Configure::read('debug')) {
65-
$this->addPlugin('DebugKit');
66-
}
67-
68-
// Load more plugins here
6957
}
7058

7159
/**
@@ -116,20 +104,4 @@ public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue
116104
public function services(ContainerInterface $container): void
117105
{
118106
}
119-
120-
/**
121-
* Bootstrapping for CLI application.
122-
*
123-
* That is when running commands.
124-
*
125-
* @return void
126-
*/
127-
protected function bootstrapCli(): void
128-
{
129-
$this->addOptionalPlugin('Bake');
130-
131-
$this->addPlugin('Migrations');
132-
133-
// Load more plugins here
134-
}
135107
}

src/Console/Installer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public static function postInstall(Event $event): void
5757
{
5858
$io = $event->getIO();
5959

60-
$rootDir = dirname(dirname(__DIR__));
60+
$rootDir = dirname(__DIR__, 2);
6161

6262
static::createAppLocalConfig($rootDir, $io);
6363
static::createWritableDirectories($rootDir, $io);

tests/TestCase/ApplicationTest.php

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
use Cake\Routing\Middleware\RoutingMiddleware;
2525
use Cake\TestSuite\IntegrationTestTrait;
2626
use Cake\TestSuite\TestCase;
27-
use InvalidArgumentException;
2827

2928
/**
3029
* ApplicationTest class
@@ -41,7 +40,7 @@ class ApplicationTest extends TestCase
4140
public function testBootstrap()
4241
{
4342
Configure::write('debug', false);
44-
$app = new Application(dirname(dirname(__DIR__)) . '/config');
43+
$app = new Application(dirname(__DIR__, 2) . '/config');
4544
$app->bootstrap();
4645
$plugins = $app->getPlugins();
4746

@@ -58,41 +57,21 @@ public function testBootstrap()
5857
public function testBootstrapInDebug()
5958
{
6059
Configure::write('debug', true);
61-
$app = new Application(dirname(dirname(__DIR__)) . '/config');
60+
$app = new Application(dirname(__DIR__, 2) . '/config');
6261
$app->bootstrap();
6362
$plugins = $app->getPlugins();
6463

6564
$this->assertTrue($plugins->has('DebugKit'), 'plugins has DebugKit?');
6665
}
6766

68-
/**
69-
* testBootstrapPluginWitoutHalt
70-
*
71-
* @return void
72-
*/
73-
public function testBootstrapPluginWithoutHalt()
74-
{
75-
$this->expectException(InvalidArgumentException::class);
76-
77-
$app = $this->getMockBuilder(Application::class)
78-
->setConstructorArgs([dirname(dirname(__DIR__)) . '/config'])
79-
->onlyMethods(['addPlugin'])
80-
->getMock();
81-
82-
$app->method('addPlugin')
83-
->will($this->throwException(new InvalidArgumentException('test exception.')));
84-
85-
$app->bootstrap();
86-
}
87-
8867
/**
8968
* testMiddleware
9069
*
9170
* @return void
9271
*/
9372
public function testMiddleware()
9473
{
95-
$app = new Application(dirname(dirname(__DIR__)) . '/config');
74+
$app = new Application(dirname(__DIR__, 2) . '/config');
9675
$middleware = new MiddlewareQueue();
9776

9877
$middleware = $app->middleware($middleware);

0 commit comments

Comments
 (0)