Browse Source

Merge branch '4.x' into 4.next

Mark Story 3 years ago
parent
commit
0cb0b79283

+ 1 - 1
VERSION.txt

@@ -16,4 +16,4 @@
 // @license       https://opensource.org/licenses/mit-license.php MIT License
 // +--------------------------------------------------------------------------------------------+ //
 ////////////////////////////////////////////////////////////////////////////////////////////////////
-4.5.0-dev
+4.5.0-dev

+ 8 - 0
src/Command/PluginLoadCommand.php

@@ -109,6 +109,14 @@ class PluginLoadCommand extends Command
             $this->abort();
         }
 
+        // Check if plugin is already loaded
+        $regex = '/->addPlugin\(\'' . $plugin . '\'/mu';
+        if (preg_match($regex, $contents, $otherMatches, PREG_OFFSET_CAPTURE)) {
+            $this->io->info('The specified plugin is already loaded!');
+
+            return;
+        }
+
         $append = "$indent    \$this->addPlugin('%s');\n";
         $insert = str_replace(', []', '', sprintf($append, $plugin));
 

+ 1 - 1
src/Log/Log.php

@@ -273,7 +273,7 @@ class Log
      * ```
      *
      * @param array<string, mixed>|string $key The name of the logger config, or an array of multiple configs.
-     * @param array<string, mixed>|null $config An array of name => config data for adapter.
+     * @param array<string, mixed>|\Closure|null $config An array of name => config data for adapter.
      * @return void
      * @throws \BadMethodCallException When trying to modify an existing config.
      */

+ 35 - 0
tests/TestCase/Command/PluginLoadCommandTest.php

@@ -83,6 +83,41 @@ class PluginLoadCommandTest extends TestCase
     }
 
     /**
+     * Test loading a plugin twice prevents it being added twice
+     */
+    public function testPreventDuplicatePluginLoad(): void
+    {
+        $this->exec('plugin load TestPlugin');
+        $this->assertExitCode(Command::CODE_SUCCESS);
+
+        $contents = file_get_contents($this->app);
+        $this->assertMatchesRegularExpression('/Check plugins added here\n {8}\$this->addPlugin\(\'TestPlugin\'\);\n {4}\}\n/u', $contents);
+
+        $this->exec('plugin load TestPlugin');
+        $this->assertExitCode(Command::CODE_SUCCESS);
+        $this->assertOutputContains('The specified plugin is already loaded!');
+    }
+
+    /**
+     * Test loading a plugin twice prevents it being added twice if it has config
+     */
+    public function testPreventDuplicatePluginLoadWithConfig(): void
+    {
+        $this->exec('plugin load TestPlugin');
+        $this->assertExitCode(Command::CODE_SUCCESS);
+
+        $contents = file_get_contents($this->app);
+        $this->assertMatchesRegularExpression('/Check plugins added here\n {8}\$this->addPlugin\(\'TestPlugin\'\);\n {4}\}\n/u', $contents);
+
+        $contents = str_replace("->addPlugin('TestPlugin')", "->addPlugin('TestPlugin', ['bootstrap' => false])", $contents);
+        file_put_contents($this->app, $contents);
+
+        $this->exec('plugin load TestPlugin');
+        $this->assertExitCode(Command::CODE_SUCCESS);
+        $this->assertOutputContains('The specified plugin is already loaded!');
+    }
+
+    /**
      * Test loading an unknown plugin
      */
     public function testLoadUnknownPlugin(): void