Browse Source

Add three convenience methods to load plugins

- addOptionalPlugin(), do not halt when the plugin is not installed
- addCliPlugin(), only load the plugin when in CLI
- addOptionalCliPlugin(), only load the plugin when in CLI and do not halt when the plugin is not installed
Marc Würth 6 years ago
parent
commit
eb0374f97e
1 changed files with 56 additions and 0 deletions
  1. 56 0
      src/Http/BaseApplication.php

+ 56 - 0
src/Http/BaseApplication.php

@@ -14,11 +14,13 @@ declare(strict_types=1);
  * @since         3.3.0
  * @license       https://opensource.org/licenses/mit-license.php MIT License
  */
+
 namespace Cake\Http;
 
 use Cake\Console\CommandCollection;
 use Cake\Controller\ControllerFactory;
 use Cake\Core\ConsoleApplicationInterface;
+use Cake\Core\Exception\MissingPluginException;
 use Cake\Core\HttpApplicationInterface;
 use Cake\Core\Plugin;
 use Cake\Core\PluginApplicationInterface;
@@ -122,6 +124,60 @@ abstract class BaseApplication implements
     }
 
     /**
+     * Add an optional plugin
+     *
+     * If it isn't available, ignore it.
+     *
+     * @param string|\Cake\Core\PluginInterface $name The plugin name or plugin object.
+     * @param array $config The configuration data for the plugin if using a string for $name
+     * @return $this
+     */
+    public function addOptionalPlugin($name, array $config = [])
+    {
+        try {
+            $this->addPlugin($name, $config);
+        } catch (MissingPluginException $e) {
+            // Do not halt if the plugin is missing
+        }
+
+        return $this;
+    }
+
+    /**
+     * Add a plugin, when in CLI
+     *
+     * @param string|\Cake\Core\PluginInterface $name The plugin name or plugin object.
+     * @param array $config The configuration data for the plugin if using a string for $name
+     * @return $this
+     */
+    public function addCliPlugin($name, array $config = [])
+    {
+        if (PHP_SAPI === 'cli') {
+            $this->addPlugin($name, $config);
+        }
+
+        return $this;
+    }
+
+    /**
+     * Add an optional plugin, when in CLI
+     *
+     * If it isn't available, ignore it.
+     *
+     * @param string|\Cake\Core\PluginInterface $name The plugin name or plugin object.
+     * @param array $config The configuration data for the plugin if using a string for $name
+     * @return $this
+     */
+    public function addOptionalCliPlugin($name, array $config = [])
+    {
+        if (PHP_SAPI === 'cli') {
+            $this->addOptionalCliPlugin($name, $config);
+        }
+
+        return $this;
+    }
+
+    /**
      * Get the plugin collection in use.
      *
      * @return \Cake\Core\PluginCollection