Skip to content

Commit

Permalink
Merge pull request #971 from cakephp/5.x-plugins-config
Browse files Browse the repository at this point in the history
5.x: use plugin loading via configuration array
  • Loading branch information
markstory authored Sep 30, 2023
2 parents 259132b + 53230d9 commit b335138
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 55 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"license": "MIT",
"require": {
"php": ">=8.1",
"cakephp/cakephp": "^5.0.0",
"cakephp/cakephp": "^5.0.1",
"cakephp/migrations": "^4.0.0",
"cakephp/plugin-installer": "^2.0",
"mobiledetect/mobiledetectlib": "^3.74"
Expand Down
33 changes: 33 additions & 0 deletions config/plugins.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* Plugin configuration.
*
* In this file, you configure which plugins are loaded in the different states your app can be.
* It's loaded via the `parent::bootstrap();` call inside your `Application::bootstrap()` method.
* For more information see https://book.cakephp.org/5/en/plugins.html#loading-plugins-via-configuration-array
*
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* 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 5.0.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/

return [
// Plugins only needed when in debug mode
'DebugKit' => ['onlyDebug' => true],

// Optional plugins which are only needed in CLI commands
'Bake' => ['onlyCli' => true, 'optional' => true],

// Required plugins only in CLI commands
'Migrations' => ['onlyCli' => true],

// Add your custom plugins here
];
30 changes: 1 addition & 29 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,12 @@ public function bootstrap(): void
// Call parent to load bootstrap from files.
parent::bootstrap();

if (PHP_SAPI === 'cli') {
$this->bootstrapCli();
} else {
if (PHP_SAPI !== 'cli') {
FactoryLocator::add(
'Table',
(new TableLocator())->allowFallbackClass(false)
);
}

/*
* Only try to load DebugKit in development mode
* Debug Kit should not be installed on a production system
*/
if (Configure::read('debug')) {
$this->addPlugin('DebugKit');
}

// Load more plugins here
}

/**
Expand Down Expand Up @@ -116,20 +104,4 @@ public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue
public function services(ContainerInterface $container): void
{
}

/**
* Bootstrapping for CLI application.
*
* That is when running commands.
*
* @return void
*/
protected function bootstrapCli(): void
{
$this->addOptionalPlugin('Bake');

$this->addPlugin('Migrations');

// Load more plugins here
}
}
2 changes: 1 addition & 1 deletion src/Console/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static function postInstall(Event $event): void
{
$io = $event->getIO();

$rootDir = dirname(dirname(__DIR__));
$rootDir = dirname(__DIR__, 2);

static::createAppLocalConfig($rootDir, $io);
static::createWritableDirectories($rootDir, $io);
Expand Down
27 changes: 3 additions & 24 deletions tests/TestCase/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
use Cake\Routing\Middleware\RoutingMiddleware;
use Cake\TestSuite\IntegrationTestTrait;
use Cake\TestSuite\TestCase;
use InvalidArgumentException;

/**
* ApplicationTest class
Expand All @@ -41,7 +40,7 @@ class ApplicationTest extends TestCase
public function testBootstrap()
{
Configure::write('debug', false);
$app = new Application(dirname(dirname(__DIR__)) . '/config');
$app = new Application(dirname(__DIR__, 2) . '/config');
$app->bootstrap();
$plugins = $app->getPlugins();

Expand All @@ -58,41 +57,21 @@ public function testBootstrap()
public function testBootstrapInDebug()
{
Configure::write('debug', true);
$app = new Application(dirname(dirname(__DIR__)) . '/config');
$app = new Application(dirname(__DIR__, 2) . '/config');
$app->bootstrap();
$plugins = $app->getPlugins();

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

/**
* testBootstrapPluginWitoutHalt
*
* @return void
*/
public function testBootstrapPluginWithoutHalt()
{
$this->expectException(InvalidArgumentException::class);

$app = $this->getMockBuilder(Application::class)
->setConstructorArgs([dirname(dirname(__DIR__)) . '/config'])
->onlyMethods(['addPlugin'])
->getMock();

$app->method('addPlugin')
->will($this->throwException(new InvalidArgumentException('test exception.')));

$app->bootstrap();
}

/**
* testMiddleware
*
* @return void
*/
public function testMiddleware()
{
$app = new Application(dirname(dirname(__DIR__)) . '/config');
$app = new Application(dirname(__DIR__, 2) . '/config');
$middleware = new MiddlewareQueue();

$middleware = $app->middleware($middleware);
Expand Down

0 comments on commit b335138

Please sign in to comment.