function load_child_plugins($data, $index, $parent_id, $level) { $parent_id = $parent_id === NULL ? "NULL" : $parent_id; // load this plugin $plugin = $data[$parent_id]; // require plugin class $plugin_path = ROOT_PATH . 'plugins/' . $plugin->getName() . '/plugin.php'; if (file_exists($plugin_path)) { require_once $plugin_path; // init plugin class $className = $plugin->getClassName(); $pluginClass = new $className(); } else { $pluginClass = new DatawrapperPlugin($plugin->getName()); } // but before we load the required libraries foreach ($pluginClass->getRequiredLibraries() as $lib) { require_once ROOT_PATH . 'plugins/' . $plugin->getName() . '/' . $lib; } $pluginClass->init(); if (isset($index[$parent_id])) { foreach ($index[$parent_id] as $id) { load_child_plugins($data, $index, $id, $level + 1); } } }
function load_plugin($plugin) { $plugin_path = ROOT_PATH . 'plugins/' . $plugin->getName() . '/plugin.php'; if (file_exists($plugin_path)) { require $plugin_path; // init plugin class $className = $plugin->getClassName(); $pluginClass = new $className(); } else { $pluginClass = new DatawrapperPlugin($plugin->getName()); } // but before we load the libraries required by this lib foreach ($pluginClass->getRequiredLibraries() as $lib) { require_once ROOT_PATH . 'plugins/' . $plugin->getName() . '/' . $lib; } $pluginClass->init(); return $pluginClass; }
public static function load() { if (defined('NO_SESSION')) { $plugins = PluginQuery::create()->distinct()->filterByEnabled(true)->filterByIsPrivate(false)->find(); } else { $plugins = self::getUserPlugins(DatawrapperSession::getUser()->getId()); } $not_loaded_yet = array(); foreach ($plugins as $plugin) { if (!isset(self::$loaded[$plugin->getId()])) { $not_loaded_yet[] = $plugin; } } $could_not_install = array(); $init_queue = array(); $load_plugin = function ($plugin) use(&$init_queue) { $plugin_path = ROOT_PATH . 'plugins/' . $plugin->getName(); // first if this plugin uses composer, require the autoloader if (file_exists($plugin_path . '/vendor/autoload.php')) { require_once $plugin_path . '/vendor/autoload.php'; } if (file_exists($plugin_path . '/plugin.php')) { require_once $plugin_path . '/plugin.php'; // init plugin class $className = $plugin->getClassName(); $pluginClass = new $className(); } else { $pluginClass = new DatawrapperPlugin($plugin->getName()); if (file_exists($plugin_path . '/init.php')) { $pluginClass->injectInitFunction(function ($plugin) use($plugin_path) { include_once $plugin_path . '/init.php'; }); } } // then, lets also load the libraries required by this lib // this is DEPRECATED, all new plugins should use the autoload // method using composer or similar foreach ($pluginClass->getRequiredLibraries() as $lib) { require_once $plugin_path . '/' . $lib; } $init_queue[] = $pluginClass; return $pluginClass; }; while (count($not_loaded_yet) > 0) { $try = $not_loaded_yet; $not_loaded_yet = array(); while (count($try) > 0) { $plugin = array_shift($try); $id = $plugin->getId(); $deps = $plugin->getDependencies(); unset($deps['core']); // ignore core dependency $can_load = true; if (is_array($deps)) { foreach ($deps as $dep => $version) { if (!isset(self::$loaded[$dep])) { // dependency not loaded $can_load = false; if (!file_exists(ROOT_PATH . 'plugins/' . $dep) || isset($could_not_install[$dep])) { // dependency does not exists, not good $could_not_install[$id] = true; } break; } } } if (isset(self::$loaded[$id]) && self::$loaded[$id]) { // plugin already loaded by now continue; } if ($can_load) { // load plugin self::$loaded[$id] = true; self::$instances[$id] = $load_plugin($plugin); } else { if (!isset($could_not_install[$id])) { $not_loaded_yet[] = $plugin; // so try next time } } } } // now initialize all plugins while (count($init_queue) > 0) { $pluginClass = array_shift($init_queue); $pluginClass->init(); } }