/** * Checks if the plugin can be enabled. * * @return boolean */ public function canBeEnabled() { if ($this->isEnabled()) { // The plugin cannot be enabled twice return false; } if ($this->hasUnsatisfiedSystemRequirements()) { return false; } // Make sure all plugin's dependencies exist, are enabled and have // appropriate versions foreach ($this->getDependencies() as $plugin_name => $required_version) { if (!Utils::pluginExists($plugin_name)) { return false; } $plugin = new PluginInfo($plugin_name); if (!$plugin->isInstalled() || !$plugin->isEnabled()) { return false; } $version_constrain = new VersionExpression($required_version); if (!$version_constrain->satisfiedBy(new Version($plugin->getInstalledVersion()))) { return false; } } return true; }
/** * Updates a plugin. * * @param Request $request Incoming request. * @return string Rendered page content. * @throws NotFoundException If the plugin with specified name is not found * in the system. */ public function updateAction(Request $request) { csrf_check_token($request); $plugin_name = $request->attributes->get('plugin_name'); if (!PluginUtils::pluginExists($plugin_name)) { throw new NotFoundException('The plugin is not found.'); } // Update the plugin if (!PluginManager::getInstance()->update($plugin_name)) { $error = getlocal('Plugin "{0}" cannot be updated.', array($plugin_name)); $request->attributes->set('errors', array($error)); // The plugin cannot be updated by some reasons. Just rebuild // index page and show errors there. return $this->indexAction($request); } return $this->redirect($this->generateUrl('plugins')); }
/** * 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(); } }