Browse Source

Backport Plugin::isLoaded()

Using Plugin::loaded() with a parameter will be removed in 4.0. This
backports the new method.
Mark Story 7 years ago
parent
commit
08c667fc9f

+ 1 - 1
src/Console/CommandScanner.php

@@ -83,7 +83,7 @@ class CommandScanner
      */
     public function scanPlugin($plugin)
     {
-        if (!Plugin::loaded($plugin)) {
+        if (!Plugin::isLoaded($plugin)) {
             return [];
         }
         $path = Plugin::classPath($plugin);

+ 1 - 1
src/Core/ConventionsTrait.php

@@ -135,7 +135,7 @@ trait ConventionsTrait
      */
     protected function _pluginPath($pluginName)
     {
-        if (Plugin::loaded($pluginName)) {
+        if (Plugin::isLoaded($pluginName)) {
             return Plugin::path($pluginName);
         }
 

+ 21 - 2
src/Core/Plugin.php

@@ -324,8 +324,22 @@ class Plugin
     }
 
     /**
-     * Returns true if the plugin $plugin is already loaded
-     * If plugin is null, it will return a list of all loaded plugins
+     * Check whether or not a plugin is loaded.
+     *
+     * @param string $plugin The name of the plugin to check.
+     * @return bool
+     */
+    public static function isLoaded($plugin)
+    {
+        return static::getCollection()->has($plugin);
+    }
+
+    /**
+     * Return a list of loaded plugins.
+     *
+     * If a plugin name is provided, the return value will be a bool
+     * indicating whether or not the named plugin is loaded. This usage
+     * is deprecated. Instead you should use Plugin::isLoaded($name)
      *
      * @param string|null $plugin Plugin name.
      * @return bool|array Boolean true if $plugin is already loaded.
@@ -334,6 +348,11 @@ class Plugin
     public static function loaded($plugin = null)
     {
         if ($plugin !== null) {
+            deprecationWarning(
+                'Checking a single plugin with Plugin::loaded() is deprecated. ' .
+                'Use Plugin::isLoaded() instead.'
+            );
+
             return static::getCollection()->has($plugin);
         }
         $names = [];

+ 1 - 1
src/I18n/MessagesFileLoader.php

@@ -171,7 +171,7 @@ class MessagesFileLoader
 
         // If space is not added after slash, the character after it remains lowercased
         $pluginName = Inflector::camelize(str_replace('/', '/ ', $this->_name));
-        if (Plugin::loaded($pluginName)) {
+        if (Plugin::isLoaded($pluginName)) {
             $basePath = Plugin::classPath($pluginName) . 'Locale' . DIRECTORY_SEPARATOR;
             foreach ($folders as $folder) {
                 $searchPaths[] = $basePath . $folder . DIRECTORY_SEPARATOR;

+ 1 - 1
src/Routing/Filter/AssetFilter.php

@@ -109,7 +109,7 @@ class AssetFilter extends DispatcherFilter
             }
             $pluginPart[] = Inflector::camelize($parts[$i]);
             $plugin = implode('/', $pluginPart);
-            if ($plugin && Plugin::loaded($plugin)) {
+            if ($plugin && Plugin::isLoaded($plugin)) {
                 $parts = array_slice($parts, $i + 1);
                 $fileFragment = implode(DIRECTORY_SEPARATOR, $parts);
                 $pluginWebroot = Plugin::path($plugin) . 'webroot' . DIRECTORY_SEPARATOR;

+ 1 - 1
src/Routing/Middleware/AssetMiddleware.php

@@ -142,7 +142,7 @@ class AssetMiddleware
             }
             $pluginPart[] = Inflector::camelize($parts[$i]);
             $plugin = implode('/', $pluginPart);
-            if ($plugin && Plugin::loaded($plugin)) {
+            if ($plugin && Plugin::isLoaded($plugin)) {
                 $parts = array_slice($parts, $i + 1);
                 $fileFragment = implode(DIRECTORY_SEPARATOR, $parts);
                 $pluginWebroot = Plugin::path($plugin) . 'webroot' . DIRECTORY_SEPARATOR;

+ 1 - 1
src/Shell/Task/AssetsTask.php

@@ -88,7 +88,7 @@ class AssetsTask extends Shell
         if ($name === null) {
             $pluginsList = Plugin::loaded();
         } else {
-            if (!Plugin::loaded($name)) {
+            if (!Plugin::isLoaded($name)) {
                 $this->err(sprintf('Plugin %s is not loaded.', $name));
 
                 return [];

+ 1 - 1
src/Shell/Task/ExtractTask.php

@@ -168,7 +168,7 @@ class ExtractTask extends Shell
             $this->_paths = explode(',', $this->params['paths']);
         } elseif (isset($this->params['plugin'])) {
             $plugin = Inflector::camelize($this->params['plugin']);
-            if (!Plugin::loaded($plugin)) {
+            if (!Plugin::isLoaded($plugin)) {
                 Plugin::load($plugin);
             }
             $this->_paths = [Plugin::classPath($plugin)];

+ 1 - 1
src/Template/Element/plugin_class_error.ctp

@@ -20,7 +20,7 @@ if (empty($plugin)) {
 
 echo '<br><br>';
 
-if (!Plugin::loaded($plugin)):
+if (!Plugin::isLoaded($plugin)):
     echo sprintf('Make sure your plugin <em>%s</em> is in the %s directory and was loaded.', h($plugin), $pluginPath);
 else:
     echo sprintf('Make sure your plugin was loaded from %s and Composer is able to autoload its classes, see %s and %s',

+ 2 - 2
src/Template/Error/missing_behavior.ctp

@@ -26,10 +26,10 @@ $pluginDot = empty($plugin) ? null : $plugin . '.';
 if (empty($plugin)) {
     $filePath = APP_DIR . DIRECTORY_SEPARATOR;
 }
-if (!empty($plugin) && Plugin::loaded($plugin)) {
+if (!empty($plugin) && Plugin::isLoaded($plugin)) {
     $filePath = Plugin::classPath($plugin);
 }
-if (!empty($plugin) && !Plugin::loaded($plugin)) {
+if (!empty($plugin) && !Plugin::isLoaded($plugin)) {
     $filePath = $pluginPath . h($plugin) . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR;
 }
 

+ 2 - 2
src/Template/Error/missing_component.ctp

@@ -25,10 +25,10 @@ $pluginDot = empty($plugin) ? null : $plugin . '.';
 if (empty($plugin)) {
     $filePath = APP_DIR . DIRECTORY_SEPARATOR;
 }
-if (!empty($plugin) && Plugin::loaded($plugin)) {
+if (!empty($plugin) && Plugin::isLoaded($plugin)) {
     $filePath = Plugin::classPath($plugin);
 }
-if (!empty($plugin) && !Plugin::loaded($plugin)) {
+if (!empty($plugin) && !Plugin::isLoaded($plugin)) {
     $filePath = $pluginPath . h($plugin) . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR;
 }
 

+ 2 - 2
src/Template/Error/missing_helper.ctp

@@ -25,10 +25,10 @@ $pluginDot = empty($plugin) ? null : $plugin . '.';
 if (empty($plugin)) {
     $filePath = APP_DIR . DIRECTORY_SEPARATOR;
 }
-if (!empty($plugin) && Plugin::loaded($plugin)) {
+if (!empty($plugin) && Plugin::isLoaded($plugin)) {
     $filePath = Plugin::classPath($plugin);
 }
-if (!empty($plugin) && !Plugin::loaded($plugin)) {
+if (!empty($plugin) && !Plugin::isLoaded($plugin)) {
     $filePath = $pluginPath . h($plugin) . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR;
 }
 

+ 3 - 3
src/Template/Error/missing_view.ctp

@@ -23,10 +23,10 @@ if (empty($plugin)) {
     $filePath = APP_DIR . DIRECTORY_SEPARATOR;
     $namespace = str_replace('/', '\\', $plugin);
 }
-if (!empty($plugin) && Plugin::loaded($plugin)) {
+if (!empty($plugin) && Plugin::isLoaded($plugin)) {
     $filePath = Plugin::classPath($plugin);
 }
-if (!empty($plugin) && !Plugin::loaded($plugin)) {
+if (!empty($plugin) && !Plugin::isLoaded($plugin)) {
     $filePath = $pluginPath . h($plugin) . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR;
 }
 
@@ -38,7 +38,7 @@ $this->start('subheading');
 ?>
     <strong>Error: </strong>
     <em><?= h($pluginDot . $class) ?></em> could not be found.
-    <?php if (!empty($plugin) && !Plugin::loaded($plugin)): ?>
+    <?php if (!empty($plugin) && !Plugin::isLoaded($plugin)): ?>
     Make sure your plugin <em><?= h($plugin) ?></em> is in the <?= h($pluginPath) ?> directory and was loaded.
     <?php endif ?>
     <?= $this->element('plugin_class_error', ['pluginPath' => $pluginPath]) ?>

+ 1 - 1
src/View/Helper/UrlHelper.php

@@ -248,7 +248,7 @@ class UrlHelper extends Helper
             }
             $segments = explode('/', ltrim($filepath, '/'));
             $plugin = Inflector::camelize($segments[0]);
-            if (Plugin::loaded($plugin)) {
+            if (Plugin::isLoaded($plugin)) {
                 unset($segments[0]);
                 $pluginPath = Plugin::path($plugin) . 'webroot' . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $segments);
                 if (file_exists($pluginPath)) {

+ 1 - 1
src/View/View.php

@@ -1612,7 +1612,7 @@ class View implements EventDispatcherInterface
     {
         $plugin = null;
         list($first, $second) = pluginSplit($name);
-        if (Plugin::loaded($first) === true) {
+        if (Plugin::isLoaded($first) === true) {
             $name = $second;
             $plugin = $first;
         }

+ 21 - 3
tests/TestCase/Core/PluginTest.php

@@ -92,9 +92,11 @@ class PluginTest extends TestCase
         Plugin::load('TestPlugin');
         $expected = ['TestPlugin'];
         $this->assertEquals($expected, Plugin::loaded());
+        $this->assertTrue(Plugin::isLoaded('TestPlugin'));
 
         Plugin::unload('TestPlugin');
         $this->assertEquals([], Plugin::loaded());
+        $this->assertFalse(Plugin::isLoaded('TestPlugin'));
 
         Plugin::load('TestPlugin');
         $expected = ['TestPlugin'];
@@ -102,6 +104,7 @@ class PluginTest extends TestCase
 
         Plugin::unload('TestFakePlugin');
         $this->assertEquals($expected, Plugin::loaded());
+        $this->assertFalse(Plugin::isLoaded('TestFakePlugin'));
     }
 
     /**
@@ -144,6 +147,21 @@ class PluginTest extends TestCase
     }
 
     /**
+     * Tests deprecated usage of loaded()
+     *
+     * @deprecated
+     * @return void
+     */
+    public function testIsLoaded()
+    {
+        $this->deprecated(function () {
+            Plugin::load('TestPlugin');
+            $this->assertTrue(Plugin::loaded('TestPlugin'));
+            $this->assertFalse(Plugin::loaded('Unknown'));
+        });
+    }
+
+    /**
      * Tests loading a plugin and its bootstrap file
      *
      * @return void
@@ -151,11 +169,11 @@ class PluginTest extends TestCase
     public function testLoadWithBootstrap()
     {
         Plugin::load('TestPlugin', ['bootstrap' => true]);
-        $this->assertTrue(Plugin::loaded('TestPlugin'));
+        $this->assertTrue(Plugin::isLoaded('TestPlugin'));
         $this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));
 
         Plugin::load('Company/TestPluginThree', ['bootstrap' => true]);
-        $this->assertTrue(Plugin::loaded('Company/TestPluginThree'));
+        $this->assertTrue(Plugin::isLoaded('Company/TestPluginThree'));
         $this->assertEquals('loaded plugin three bootstrap', Configure::read('PluginTest.test_plugin_three.bootstrap'));
     }
 
@@ -167,7 +185,7 @@ class PluginTest extends TestCase
     public function testLoadWithBootstrapDisableBootstrapHook()
     {
         Plugin::load('TestPlugin', ['bootstrap' => true]);
-        $this->assertTrue(Plugin::loaded('TestPlugin'));
+        $this->assertTrue(Plugin::isLoaded('TestPlugin'));
         $this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));
 
         $plugin = Plugin::getCollection()->get('TestPlugin');