Browse Source

Use Plugin::load() when creating classless plugins.

I completely missed that Plugin::load() does a bunch of work to generate
the correct paths for plugins. By using Plugin::load() and fetching the
created plugin we can leverage the same logic. There is an argument to
be had around deprecating Plugin::load(). Given the troubles plugin
classes have given us I'm not comfortable in doing that right now.

Refs #122271
mark_story 7 years ago
parent
commit
7ceefd90e8
2 changed files with 20 additions and 6 deletions
  1. 5 4
      src/Http/BaseApplication.php
  2. 15 2
      tests/TestCase/Http/BaseApplicationTest.php

+ 5 - 4
src/Http/BaseApplication.php

@@ -125,11 +125,12 @@ abstract class BaseApplication implements
         if (strpos($className, '\\') === false) {
             $className = str_replace('/', '\\', $className) . '\\' . 'Plugin';
         }
-        if (!class_exists($className)) {
-            $config['name'] = $name;
-            $className = BasePlugin::class;
+        if (class_exists($className)) {
+            $plugin = new $className($config);
+        } else {
+            Plugin::load($name, $config);
+            $plugin = $this->plugins->get($name);
         }
-        $plugin = new $className($config);
         if (!$plugin instanceof PluginInterface) {
             throw new InvalidArgumentException("The `{$name}` plugin does not implement Cake\Core\PluginInterface.");
         }

+ 15 - 2
tests/TestCase/Http/BaseApplicationTest.php

@@ -84,9 +84,22 @@ class BaseApplicationTest extends TestCase
     public function testAddPluginUnknownClass()
     {
         $app = $this->getMockForAbstractClass(BaseApplication::class, [$this->path]);
-        $app->addPlugin('SomethingBad');
-        $plugin = $app->getPlugins()->get('SomethingBad');
+        $app->addPlugin('PluginJs');
+        $plugin = $app->getPlugins()->get('PluginJs');
         $this->assertInstanceOf(BasePlugin::class, $plugin);
+
+        $this->assertEquals(
+            TEST_APP . 'Plugin' . DS . 'PluginJs' . DS,
+            $plugin->getPath()
+        );
+        $this->assertEquals(
+            TEST_APP . 'Plugin' . DS . 'PluginJs' . DS . 'config' . DS,
+            $plugin->getConfigPath()
+        );
+        $this->assertEquals(
+            TEST_APP . 'Plugin' . DS . 'PluginJs' . DS . 'src' . DS,
+            $plugin->getClassPath()
+        );
     }
 
     /**