Beispiel #1
0
 /**
  * Loads plugins.
  *
  * The method checks dependences and plugin availability before loading and
  * invokes PluginInterface::run() after loading.
  *
  * @param array $configs List of plugins' configurations. Each key is a
  * plugin name and each value is a configurations array.
  *
  * @see \Mibew\Plugin\PluginInterface::run()
  */
 public function loadPlugins($configs)
 {
     // Builds Dependency graph with available plugins.
     $graph = new DependencyGraph();
     foreach (State::loadAllEnabled() as $plugin_state) {
         if (!PluginUtils::pluginExists($plugin_state->pluginName)) {
             trigger_error(sprintf('Plugin "%s" exists in database base but is not found in file system!', $plugin_state->pluginName), E_USER_WARNING);
             continue;
         }
         $plugin_info = PluginInfo::fromState($plugin_state);
         if ($plugin_info->needsUpdate()) {
             trigger_error(sprintf('Update "%s" plugin before use it!', $plugin_info->getName()), E_USER_WARNING);
             continue;
         }
         if ($plugin_info->hasUnsatisfiedSystemRequirements()) {
             trigger_error(sprintf('Plugin "%s" has unsatisfied system requirements!', $plugin_info->getName()), E_USER_WARNING);
             continue;
         }
         $graph->addPlugin($plugin_info);
     }
     $offset = 0;
     $running_queue = array();
     foreach ($graph->getLoadingQueue() as $plugin_info) {
         // Make sure all depedendencies are loaded
         foreach (array_keys($plugin_info->getDependencies()) as $dependency) {
             if (!isset($this->loadedPlugins[$dependency])) {
                 trigger_error(sprintf('Plugin "%s" was not loaded yet, but exists in "%s" dependencies list!', $dependency, $plugin_info->getName()), E_USER_WARNING);
                 continue 2;
             }
         }
         // Try to load the plugin.
         $name = $plugin_info->getName();
         $config = isset($configs[$name]) ? $configs[$name] : array();
         $instance = $plugin_info->getInstance($config);
         if ($instance->initialized()) {
             // Store the plugin and add it to running queue
             $this->loadedPlugins[$name] = $instance;
             $running_queue[$instance->getWeight() . "_" . $offset] = $instance;
             $offset++;
         } else {
             // The plugin cannot be loaded. Just skip it.
             trigger_error("Plugin '{$name}' was not initialized correctly!", E_USER_WARNING);
         }
     }
     // Sort queue in order to plugins' weights and run plugins one by one
     uksort($running_queue, 'strnatcmp');
     foreach ($running_queue as $plugin) {
         $plugin->run();
     }
 }