Ver código fonte

Allow manual plugin loading

Daniel Rudolf 9 anos atrás
pai
commit
56b2ed6c7d
1 arquivos alterados com 43 adições e 1 exclusões
  1. 43 1
      lib/Pico.php

+ 43 - 1
lib/Pico.php

@@ -383,6 +383,7 @@ class Pico
      * - 60 to 79: Plugins hooking into template or markdown parsing
      * - 60 to 79: Plugins hooking into template or markdown parsing
      * - 80 to 99: Plugins using the `onPageRendered` event
      * - 80 to 99: Plugins using the `onPageRendered` event
      *
      *
+     * @see    Pico::loadPlugin()
      * @see    Pico::getPlugin()
      * @see    Pico::getPlugin()
      * @see    Pico::getPlugins()
      * @see    Pico::getPlugins()
      * @return void
      * @return void
@@ -404,11 +405,52 @@ class Pico
                 $this->plugins[$className] = $plugin;
                 $this->plugins[$className] = $plugin;
             } else {
             } else {
                 // TODO: breaks backward compatibility
                 // TODO: breaks backward compatibility
-                //throw new RuntimeException("Unable to load plugin '".$className."'");
+                //throw new RuntimeException("Unable to load plugin '" . $className . "'");
             }
             }
         }
         }
     }
     }
 
 
+    /**
+     * Manually loads a plugin
+     *
+     * Manually loaded plugins must implement {@see PicoPluginInterface}.
+     *
+     * @see    Pico::loadPlugins()
+     * @see    Pico::getPlugin()
+     * @see    Pico::getPlugins()
+     * @param  PicoPluginInterface|string $plugin either the class name of a
+     *     plugin to instantiate or a plugin instance
+     * @return PicoPluginInterface                instance of the loaded plugin
+     * @throws RuntimeException                   thrown when a plugin couldn't
+     *     be loaded
+     */
+    public function loadPlugin($plugin)
+    {
+        if (!is_object($plugin)) {
+            $className = (string) $plugin;
+            if (class_exists($className)) {
+                $plugin = new $className($this);
+            } else {
+                throw new RuntimeException("Unable to load plugin '" . $className . "'");
+            }
+        }
+
+        $className = get_class($plugin);
+        if (!is_a($plugin, 'PicoPluginInterface')) {
+            throw new RuntimeException(
+                "Manually loaded plugins must implement 'PicoPluginInterface', "
+                . "'" . $className . "' given"
+            );
+        }
+
+        if ($this->plugins === null) {
+            $this->plugins = array();
+        }
+        $this->plugins[$className] = $plugin;
+
+        return $plugin;
+    }
+
     /**
     /**
      * Returns the instance of a named plugin
      * Returns the instance of a named plugin
      *
      *