]> 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 dca7e63e571621461c72e64e3b42b0c2cd29fea2..1ed4db4b30c09448227d028caf484a699bb0265a 100644 (file)
@@ -24,6 +24,11 @@ class PluginManager
      */
     protected $conf;
 
+    /**
+     * @var array List of plugin errors.
+     */
+    protected $errors;
+
     /**
      * Plugins subdirectory.
      * @var string $PLUGINS_PATH
@@ -44,6 +49,7 @@ class PluginManager
     public function __construct(&$conf)
     {
         $this->conf = $conf;
+        $this->errors = array();
     }
 
     /**
@@ -69,8 +75,7 @@ class PluginManager
 
             try {
                 $this->loadPlugin($dirs[$index], $plugin);
-            }
-            catch (PluginFileNotFoundException $e) {
+            } catch (PluginFileNotFoundException $e) {
                 error_log($e->getMessage());
             }
         }
@@ -106,6 +111,7 @@ class PluginManager
 
     /**
      * 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.
@@ -128,6 +134,14 @@ class PluginManager
         $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;
     }
 
@@ -173,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']);
@@ -185,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;
+    }
 }
 
 /**
@@ -208,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
+}