Browse Source

Try autoloading plugin in PluginCollection::get().

Refs #13067.
ADmad 6 years ago
parent
commit
fd49befb72

+ 16 - 3
src/Core/PluginCollection.php

@@ -206,11 +206,24 @@ class PluginCollection implements Iterator, Countable
      */
     public function get(string $name): PluginInterface
     {
-        if (!$this->has($name)) {
-            throw new MissingPluginException(['plugin' => $name]);
+        if ($this->has($name)) {
+            return $this->plugins[$name];
         }
 
-        return $this->plugins[$name];
+        $config = ['name' => $name];
+        $className = str_replace('/', '\\', $name) . '\\' . 'Plugin';
+        if (class_exists($className)) {
+            $plugin = new $className($config);
+            $this->add($plugin);
+
+            return $plugin;
+        }
+
+        $config['path'] = $this->findPath($name);
+        $plugin = new $className($config);
+        $this->add($plugin);
+
+        return $plugin;
     }
 
     /**

+ 7 - 0
tests/TestCase/Core/PluginCollectionTest.php

@@ -90,6 +90,13 @@ class PluginCollectionTest extends TestCase
         $this->assertSame($plugin, $plugins->get('TestPlugin'));
     }
 
+    public function testGetAutoload()
+    {
+        $plugins = new PluginCollection();
+        $plugin = $plugins->get('ParentPlugin');
+        $this->assertInstanceOf(\ParentPlugin\Plugin::class, $plugin);
+    }
+
     public function testGetInvalid()
     {
         $this->expectException(MissingPluginException::class);

+ 5 - 4
tests/TestCase/Core/PluginTest.php

@@ -16,6 +16,7 @@ declare(strict_types=1);
 namespace Cake\Test\TestCase\Core;
 
 use Cake\Core\BasePlugin;
+use Cake\Core\Exception\MissingPluginException;
 use Cake\Core\Plugin;
 use Cake\TestSuite\TestCase;
 use TestPlugin\Plugin as TestPlugin;
@@ -96,8 +97,8 @@ class PluginTest extends TestCase
      */
     public function testPathNotFound()
     {
-        $this->expectException(\Cake\Core\Exception\MissingPluginException::class);
-        Plugin::path('TestPlugin');
+        $this->expectException(MissingPluginException::class);
+        Plugin::path('NonExistentPlugin');
     }
 
     /**
@@ -143,7 +144,7 @@ class PluginTest extends TestCase
      */
     public function testClassPathNotFound()
     {
-        $this->expectException(\Cake\Core\Exception\MissingPluginException::class);
-        Plugin::classPath('TestPlugin');
+        $this->expectException(MissingPluginException::class);
+        Plugin::classPath('NonExistentPlugin');
     }
 }