Browse Source

Integrate new & old style plugins.

Old style plugin loading (via `Plugin`) now uses the new style plugin
classes. It won't use concrete plugin classes should they exist as old
style plugins are likely not updated.

This does allow plugins loaded via `BaseApplication::addPlugin()` to be
available to `Plugin` and thus work with locators/iterators using that
class.
mark_story 8 years ago
parent
commit
dc8e17da1a
3 changed files with 59 additions and 58 deletions
  1. 57 45
      src/Core/Plugin.php
  2. 2 1
      src/Http/BaseApplication.php
  3. 0 12
      tests/TestCase/Core/PluginTest.php

+ 57 - 45
src/Core/Plugin.php

@@ -30,9 +30,9 @@ class Plugin
     /**
      * Holds a list of all loaded plugins and their configuration
      *
-     * @var array
+     * @var \Cake\Core\PluginCollection
      */
-    protected static $_plugins = [];
+    protected static $plugins;
 
     /**
      * Class loader instance
@@ -127,7 +127,8 @@ class Plugin
             'bootstrap' => false,
             'routes' => false,
             'classBase' => 'src',
-            'ignoreMissing' => false
+            'ignoreMissing' => false,
+            'name' => $plugin
         ];
 
         if (!isset($config['path'])) {
@@ -154,7 +155,8 @@ class Plugin
             $config['configPath'] = $config['path'] . 'config' . DIRECTORY_SEPARATOR;
         }
 
-        static::$_plugins[$plugin] = $config;
+        // Use stub plugins as this method will be removed long term.
+        static::getCollection()->add(new PluginApp($config));
 
         if ($config['autoload'] === true) {
             if (empty(static::$_loader)) {
@@ -257,70 +259,63 @@ class Plugin
     /**
      * Returns the filesystem path for a plugin
      *
-     * @param string $plugin name of the plugin in CamelCase format
+     * @param string $name name of the plugin in CamelCase format
      * @return string path to the plugin folder
      * @throws \Cake\Core\Exception\MissingPluginException if the folder for plugin was not found or plugin has not been loaded
      */
-    public static function path($plugin)
+    public static function path($name)
     {
-        if (empty(static::$_plugins[$plugin])) {
-            throw new MissingPluginException(['plugin' => $plugin]);
-        }
+        $plugin = static::getCollection()->get($name);
 
-        return static::$_plugins[$plugin]['path'];
+        return $plugin->getPath();
     }
 
     /**
      * Returns the filesystem path for plugin's folder containing class folders.
      *
-     * @param string $plugin name of the plugin in CamelCase format.
+     * @param string $name name of the plugin in CamelCase format.
      * @return string Path to the plugin folder container class folders.
      * @throws \Cake\Core\Exception\MissingPluginException If plugin has not been loaded.
      */
-    public static function classPath($plugin)
+    public static function classPath($name)
     {
-        if (empty(static::$_plugins[$plugin])) {
-            throw new MissingPluginException(['plugin' => $plugin]);
-        }
+        $plugin = static::getCollection()->get($name);
 
-        return static::$_plugins[$plugin]['classPath'];
+        return $plugin->getClassPath();
     }
 
     /**
      * Returns the filesystem path for plugin's folder containing config files.
      *
-     * @param string $plugin name of the plugin in CamelCase format.
+     * @param string $name name of the plugin in CamelCase format.
      * @return string Path to the plugin folder container config files.
      * @throws \Cake\Core\Exception\MissingPluginException If plugin has not been loaded.
      */
-    public static function configPath($plugin)
+    public static function configPath($name)
     {
-        if (empty(static::$_plugins[$plugin])) {
-            throw new MissingPluginException(['plugin' => $plugin]);
-        }
+        $plugin = static::getCollection()->get($name);
 
-        return static::$_plugins[$plugin]['configPath'];
+        return $plugin->getConfigPath();
     }
 
     /**
      * Loads the bootstrapping files for a plugin, or calls the initialization setup in the configuration
      *
-     * @param string $plugin name of the plugin
+     * @param string $name name of the plugin
      * @return mixed
      * @see \Cake\Core\Plugin::load() for examples of bootstrap configuration
      */
-    public static function bootstrap($plugin)
+    public static function bootstrap($name)
     {
-        $config = static::$_plugins[$plugin];
-        if ($config['bootstrap'] === false) {
+        $plugin = static::getCollection()->get($name);
+        if (!$plugin->isBootstrapEnabled()) {
             return false;
         }
-        if ($config['bootstrap'] === true) {
-            return static::_includeFile(
-                $config['configPath'] . 'bootstrap.php',
-                $config['ignoreMissing']
-            );
-        }
+
+        return static::_includeFile(
+            $plugin->getConfigPath() . 'bootstrap.php',
+            true
+        );
     }
 
     /**
@@ -329,27 +324,27 @@ class Plugin
      * If you need fine grained control over how routes are loaded for plugins, you
      * can use {@see Cake\Routing\RouteBuilder::loadPlugin()}
      *
-     * @param string|null $plugin name of the plugin, if null will operate on all
+     * @param string|null $name name of the plugin, if null will operate on all
      *   plugins having enabled the loading of routes files.
      * @return bool
      */
-    public static function routes($plugin = null)
+    public static function routes($name = null)
     {
-        if ($plugin === null) {
+        if ($name === null) {
             foreach (static::loaded() as $p) {
                 static::routes($p);
             }
 
             return true;
         }
-        $config = static::$_plugins[$plugin];
-        if ($config['routes'] === false) {
+        $plugin = static::getCollection()->get($name);
+        if (!$plugin->isRoutesEnabled()) {
             return false;
         }
 
         return (bool)static::_includeFile(
-            $config['configPath'] . 'routes.php',
-            $config['ignoreMissing']
+            $plugin->getConfigPath() . 'routes.php',
+            true
         );
     }
 
@@ -364,12 +359,15 @@ class Plugin
     public static function loaded($plugin = null)
     {
         if ($plugin !== null) {
-            return isset(static::$_plugins[$plugin]);
+            return static::getCollection()->has($plugin);
+        }
+        $names = [];
+        foreach (static::getCollection() as $plugin) {
+            $names[] = $plugin->getName();
         }
-        $return = array_keys(static::$_plugins);
-        sort($return);
+        sort($names);
 
-        return $return;
+        return $names;
     }
 
     /**
@@ -381,9 +379,9 @@ class Plugin
     public static function unload($plugin = null)
     {
         if ($plugin === null) {
-            static::$_plugins = [];
+            static::$plugins = null;
         } else {
-            unset(static::$_plugins[$plugin]);
+            static::getCollection()->remove($plugin);
         }
     }
 
@@ -402,4 +400,18 @@ class Plugin
 
         return include $file;
     }
+
+    /**
+     * Get the shared plugin collection.
+     *
+     * @return \Cake\Core\PluginCollection
+     */
+    public static function getCollection()
+    {
+        if (!isset(static::$plugins)) {
+            static::$plugins = new PluginCollection();
+        }
+
+        return static::$plugins;
+    }
 }

+ 2 - 1
src/Http/BaseApplication.php

@@ -16,6 +16,7 @@ namespace Cake\Http;
 
 use Cake\Core\ConsoleApplicationInterface;
 use Cake\Core\HttpApplicationInterface;
+use Cake\Core\Plugin;
 use Cake\Core\PluginApplicationInterface;
 use Cake\Core\PluginCollection;
 use Cake\Core\PluginInterface;
@@ -58,7 +59,7 @@ abstract class BaseApplication implements
     public function __construct($configDir)
     {
         $this->configDir = $configDir;
-        $this->plugins = new PluginCollection();
+        $this->plugins = Plugin::getCollection();
     }
 
     /**

+ 0 - 12
tests/TestCase/Core/PluginTest.php

@@ -195,18 +195,6 @@ class PluginTest extends TestCase
     }
 
     /**
-     * Tests that loading a missing routes file throws a warning
-     *
-     * @return void
-     */
-    public function testLoadMultipleWithDefaultsMissingFile()
-    {
-        $this->expectException(\PHPUnit\Framework\Error\Warning::class);
-        Plugin::load(['TestPlugin', 'TestPluginTwo'], ['bootstrap' => true, 'routes' => true]);
-        Plugin::routes();
-    }
-
-    /**
      * Test ignoring missing bootstrap/routes file
      *
      * @return void