Browse Source

add more info to plugin list command

Kevin Pfeifer 2 years ago
parent
commit
7d19fea137

+ 26 - 3
src/Command/PluginListCommand.php

@@ -20,7 +20,8 @@ use Cake\Console\Arguments;
 use Cake\Console\ConsoleIo;
 use Cake\Console\ConsoleOptionParser;
 use Cake\Core\Configure;
-use Cake\Core\PluginCollection;
+use Cake\Core\Plugin;
+use Cake\Utility\Hash;
 use function Cake\I18n\__d;
 
 /**
@@ -45,10 +46,32 @@ class PluginListCommand extends Command
      */
     public function execute(Arguments $args, ConsoleIo $io): ?int
     {
-        new PluginCollection();
+        Plugin::getCollection();
+
+        // phpcs:ignore
+        $pluginsConfig = @include CONFIG . 'plugins.php';
+        if (is_array($pluginsConfig)) {
+            $pluginsConfig = Hash::normalize($pluginsConfig);
+        }
+
+        $table = [
+            ['Plugin', 'onlyDebug', 'onlyCli', 'optional'],
+        ];
+
         $plugins = Configure::read('plugins', []);
         if ($plugins && is_array($plugins)) {
-            $io->out(array_keys($plugins));
+            foreach ($plugins as $pluginName => $configPath) {
+                if ($pluginsConfig && array_key_exists($pluginName, $pluginsConfig)) {
+                    $options = $pluginsConfig[$pluginName];
+                    $onlyDebug = $options['onlyDebug'] ?? false;
+                    $onlyCli = $options['onlyCli'] ?? false;
+                    $optional = $options['optional'] ?? false;
+                    $table[] = [$pluginName, $onlyDebug ? 'X' : '', $onlyCli ? 'X' : '', $optional ? 'X' : ''];
+                } else {
+                    $table[] = [$pluginName, '', '', ''];
+                }
+            }
+            $io->helper('Table')->output($table);
         } else {
             $io->warning(__d('cake', 'No plugins have been found.'));
         }

+ 10 - 0
src/Core/Plugin.php

@@ -129,4 +129,14 @@ class Plugin
     {
         return static::$plugins ??= new PluginCollection();
     }
+
+    /**
+     * Reset the state which holds the loaded plugins
+     *
+     * @return void
+     */
+    public static function resetCollection(): void
+    {
+        static::$plugins = null;
+    }
 }

+ 1 - 0
src/TestSuite/TestCase.php

@@ -227,6 +227,7 @@ abstract class TestCase extends BaseTestCase
         $this->getTableLocator()->clear();
         $this->_configure = [];
         $this->_tableLocator = null;
+        Plugin::resetCollection();
         if (class_exists(Mockery::class)) {
             Mockery::close();
         }

+ 50 - 8
tests/TestCase/Command/PluginListCommandTest.php

@@ -26,7 +26,9 @@ class PluginListCommandTest extends TestCase
 {
     use ConsoleIntegrationTestTrait;
 
-    protected string $configPath;
+    protected string $pluginsListPath;
+
+    protected string $pluginsConfigPath;
 
     /**
      * setUp method
@@ -36,17 +38,24 @@ class PluginListCommandTest extends TestCase
         parent::setUp();
 
         $this->setAppNamespace();
-        $this->configPath = ROOT . DS . 'cakephp-plugins.php';
-        if (file_exists($this->configPath)) {
-            unlink($this->configPath);
+        $this->pluginsListPath = ROOT . DS . 'cakephp-plugins.php';
+        if (file_exists($this->pluginsListPath)) {
+            unlink($this->pluginsListPath);
+        }
+        $this->pluginsConfigPath = CONFIG . DS . 'plugins.php';
+        if (file_exists($this->pluginsConfigPath)) {
+            unlink($this->pluginsConfigPath);
         }
     }
 
     protected function tearDown(): void
     {
         parent::tearDown();
-        if (file_exists($this->configPath)) {
-            unlink($this->configPath);
+        if (file_exists($this->pluginsListPath)) {
+            unlink($this->pluginsListPath);
+        }
+        if (file_exists($this->pluginsConfigPath)) {
+            unlink($this->pluginsConfigPath);
         }
     }
 
@@ -75,7 +84,7 @@ return [
     ]
 ];
 PHP;
-        file_put_contents($this->configPath, $file);
+        file_put_contents($this->pluginsListPath, $file);
 
         $this->exec('plugin list');
         $this->assertExitCode(CommandInterface::CODE_SUCCESS);
@@ -93,10 +102,43 @@ PHP;
 declare(strict_types=1);
 return [];
 PHP;
-        file_put_contents($this->configPath, $file);
+        file_put_contents($this->pluginsListPath, $file);
 
         $this->exec('plugin list');
         $this->assertExitCode(CommandInterface::CODE_SUCCESS);
         $this->assertErrorContains('No plugins have been found.');
     }
+
+    /**
+     * Test enabled plugins are being flagged as enabled
+     */
+    public function testListEnabled(): void
+    {
+        $file = <<<PHP
+<?php
+declare(strict_types=1);
+return [
+    'plugins' => [
+        'TestPlugin' => '/config/path',
+        'OtherPlugin' => '/config/path'
+    ]
+];
+PHP;
+        file_put_contents($this->pluginsListPath, $file);
+
+        $config = <<<PHP
+<?php
+declare(strict_types=1);
+return [
+    'TestPlugin',
+    'OtherPlugin' => ['onlyDebug' => true, 'onlyCli' => true, 'optional' => true]
+];
+PHP;
+        file_put_contents($this->pluginsConfigPath, $config);
+
+        $this->exec('plugin list');
+        $this->assertExitCode(CommandInterface::CODE_SUCCESS);
+        $this->assertOutputContains('TestPlugin');
+        $this->assertOutputContains('OtherPlugin');
+    }
 }

+ 0 - 2
tests/test_app/config/plugins.php

@@ -1,2 +0,0 @@
-<?php
-return [];