]> git.immae.eu Git - github/shaarli/Shaarli.git/blobdiff - application/PluginManager.php
lint: apply phpcbf to application/
[github/shaarli/Shaarli.git] / application / PluginManager.php
index 787ac6a930f63f4c1245a3178b6de444be6569a9..1ed4db4b30c09448227d028caf484a699bb0265a 100644 (file)
@@ -4,17 +4,9 @@
  * Class PluginManager
  *
  * Use to manage, load and execute plugins.
- *
- * Using Singleton design pattern.
  */
 class PluginManager
 {
-    /**
-     * PluginManager singleton instance.
-     * @var PluginManager $instance
-     */
-    private static $instance;
-
     /**
      * List of authorized plugins from configuration file.
      * @var array $authorizedPlugins
@@ -28,45 +20,36 @@ class PluginManager
     private $loadedPlugins = array();
 
     /**
-     * Plugins subdirectory.
-     * @var string $PLUGINS_PATH
+     * @var ConfigManager Configuration Manager instance.
      */
-    public static $PLUGINS_PATH = 'plugins';
+    protected $conf;
 
     /**
-     * Plugins meta files extension.
-     * @var string $META_EXT
+     * @var array List of plugin errors.
      */
-    public static $META_EXT = 'meta';
+    protected $errors;
 
     /**
-     * Private constructor: new instances not allowed.
+     * Plugins subdirectory.
+     * @var string $PLUGINS_PATH
      */
-    private function __construct()
-    {
-    }
+    public static $PLUGINS_PATH = 'plugins';
 
     /**
-     * Cloning isn't allowed either.
-     *
-     * @return void
+     * Plugins meta files extension.
+     * @var string $META_EXT
      */
-    private function __clone()
-    {
-    }
+    public static $META_EXT = 'meta';
 
     /**
-     * Return existing instance of PluginManager, or create it.
+     * Constructor.
      *
-     * @return PluginManager instance.
+     * @param ConfigManager $conf Configuration Manager instance.
      */
-    public static function getInstance()
+    public function __construct(&$conf)
     {
-        if (!(self::$instance instanceof self)) {
-            self::$instance = new self();
-        }
-
-        return self::$instance;
+        $this->conf = $conf;
+        $this->errors = array();
     }
 
     /**
@@ -92,8 +75,7 @@ class PluginManager
 
             try {
                 $this->loadPlugin($dirs[$index], $plugin);
-            }
-            catch (PluginFileNotFoundException $e) {
+            } catch (PluginFileNotFoundException $e) {
                 error_log($e->getMessage());
             }
         }
@@ -102,9 +84,9 @@ class PluginManager
     /**
      * Execute all plugins registered hook.
      *
-     * @param string $hook   name of the hook to trigger.
-     * @param array  $data   list of data to manipulate passed by reference.
-     * @param array  $params additional parameters such as page target.
+     * @param string        $hook   name of the hook to trigger.
+     * @param array         $data   list of data to manipulate passed by reference.
+     * @param array         $params additional parameters such as page target.
      *
      * @return void
      */
@@ -122,13 +104,14 @@ class PluginManager
             $hookFunction = $this->buildHookName($hook, $plugin);
 
             if (function_exists($hookFunction)) {
-                $data = call_user_func($hookFunction, $data);
+                $data = call_user_func($hookFunction, $data, $this->conf);
             }
         }
     }
 
     /**
      * Load a single plugin from its files.
+     * Call the init function if it exists, and collect errors.
      * Add them in $loadedPlugins if successful.
      *
      * @param string $dir        plugin's directory.
@@ -148,8 +131,17 @@ class PluginManager
             throw new PluginFileNotFoundException($pluginName);
         }
 
+        $conf = $this->conf;
         include_once $pluginFilePath;
 
+        $initFunction = $pluginName . '_init';
+        if (function_exists($initFunction)) {
+            $errors = call_user_func($initFunction, $this->conf);
+            if (!empty($errors)) {
+                $this->errors = array_merge($this->errors, $errors);
+            }
+        }
+
         $this->loadedPlugins[] = $pluginName;
     }
 
@@ -195,6 +187,9 @@ class PluginManager
             $metaData[$plugin] = parse_ini_file($metaFile);
             $metaData[$plugin]['order'] = array_search($plugin, $this->authorizedPlugins);
 
+            if (isset($metaData[$plugin]['description'])) {
+                $metaData[$plugin]['description'] = t($metaData[$plugin]['description']);
+            }
             // Read parameters and format them into an array.
             if (isset($metaData[$plugin]['parameters'])) {
                 $params = explode(';', $metaData[$plugin]['parameters']);
@@ -207,12 +202,26 @@ class PluginManager
                     continue;
                 }
 
-                $metaData[$plugin]['parameters'][$param] = '';
+                $metaData[$plugin]['parameters'][$param]['value'] = '';
+                // Optional parameter description in parameter.PARAM_NAME=
+                if (isset($metaData[$plugin]['parameter.'. $param])) {
+                    $metaData[$plugin]['parameters'][$param]['desc'] = t($metaData[$plugin]['parameter.'. $param]);
+                }
             }
         }
 
         return $metaData;
     }
+
+    /**
+     * Return the list of encountered errors.
+     *
+     * @return array List of errors (empty array if none exists).
+     */
+    public function getErrors()
+    {
+        return $this->errors;
+    }
 }
 
 /**
@@ -230,6 +239,6 @@ class PluginFileNotFoundException extends Exception
      */
     public function __construct($pluginName)
     {
-        $this->message = 'Plugin "'. $pluginName .'" files not found.';
+        $this->message = sprintf(t('Plugin "%s" files not found.'), $pluginName);
     }
-}
\ No newline at end of file
+}