From 6f7e95f5198c0a4ac587ac56bc6cf6729104bb4f Mon Sep 17 00:00:00 2001 From: Kevin Pfeifer Date: Fri, 15 Sep 2023 15:59:20 +0200 Subject: [PATCH 1/2] use plugin loading via configuration array --- config/plugins.php | 33 ++++++++++++++++++++++++++++++ src/Application.php | 30 +-------------------------- src/Console/Installer.php | 2 +- tests/TestCase/ApplicationTest.php | 27 +++--------------------- 4 files changed, 38 insertions(+), 54 deletions(-) create mode 100644 config/plugins.php diff --git a/config/plugins.php b/config/plugins.php new file mode 100644 index 0000000000..72984dcdd4 --- /dev/null +++ b/config/plugins.php @@ -0,0 +1,33 @@ + ['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 +]; diff --git a/src/Application.php b/src/Application.php index 7d2481b52d..c85a1ecdb2 100644 --- a/src/Application.php +++ b/src/Application.php @@ -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 } /** @@ -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 - } } diff --git a/src/Console/Installer.php b/src/Console/Installer.php index e446aee09f..6f5579829b 100644 --- a/src/Console/Installer.php +++ b/src/Console/Installer.php @@ -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); diff --git a/tests/TestCase/ApplicationTest.php b/tests/TestCase/ApplicationTest.php index 7c42d856dc..6a65c52bde 100644 --- a/tests/TestCase/ApplicationTest.php +++ b/tests/TestCase/ApplicationTest.php @@ -24,7 +24,6 @@ use Cake\Routing\Middleware\RoutingMiddleware; use Cake\TestSuite\IntegrationTestTrait; use Cake\TestSuite\TestCase; -use InvalidArgumentException; /** * ApplicationTest class @@ -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(); @@ -58,33 +57,13 @@ 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 * @@ -92,7 +71,7 @@ public function testBootstrapPluginWithoutHalt() */ public function testMiddleware() { - $app = new Application(dirname(dirname(__DIR__)) . '/config'); + $app = new Application(dirname(__DIR__, 2) . '/config'); $middleware = new MiddlewareQueue(); $middleware = $app->middleware($middleware); From 53230d96d5e97dbd55fee9fb12df86de68c5da99 Mon Sep 17 00:00:00 2001 From: Kevin Pfeifer Date: Sat, 30 Sep 2023 11:35:29 -0700 Subject: [PATCH 2/2] adjust cakephp/cakephp minimum version --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 7b239f0e6c..bbcb4956fc 100644 --- a/composer.json +++ b/composer.json @@ -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"