Allow plugins depend on environment

Fixes #98
This commit is contained in:
Dmitriy Simushev 2015-01-13 11:31:40 +00:00
parent 090383bc95
commit e5b8c84736
6 changed files with 128 additions and 1 deletions

View File

@ -205,7 +205,7 @@ class PluginController extends AbstractController
$plugins[] = array(
'name' => $plugin_name,
'version' => $plugin->isInstalled() ? $plugin->getInstalledVersion() : $plugin->getVersion(),
'dependencies' => $plugin->getDependencies(),
'dependencies' => array_merge($plugin->getSystemRequirements(), $plugin->getDependencies()),
'enabled' => $plugin->isEnabled(),
'installed' => $plugin->isInstalled(),
'needsUpdate' => $plugin->needsUpdate(),

View File

@ -136,6 +136,20 @@ abstract class AbstractPlugin
return array();
}
/**
* Implementation of
* {@link \Mibew\Plugin\PluginInterface::getSystemRequirements()}.
*
* Returns an empty array to tell the Plugin Manager that the plugin has
* no system requirements.
*
* @return array
*/
public static function getSystemRequirements()
{
return array();
}
/**
* Implementation of {@link \Mibew\Plugin\PluginInterface::getInfo()}.
*

View File

@ -165,6 +165,18 @@ class PluginInfo
return call_user_func(array($this->getClass(), 'getDependencies'));
}
/**
* Returns system requirements of the plugin.
*
* @return array Requirements list. See
* {@link \Mibew\Plugin\PluginInterface::getSystemRequirements()} for
* details of the array structure.
*/
public function getSystemRequirements()
{
return call_user_func(array($this->getClass(), 'getSystemRequirements'));
}
/**
* Returns list of dependent plugins.
*
@ -228,6 +240,31 @@ class PluginInfo
&& (version_compare($this->getVersion(), $this->getInstalledVersion()) > 0);
}
/**
* Checks if the plugin has unsatisfied system requirements.
*
* @return bool
*/
public function hasUnsatisfiedSystemRequirements()
{
$system_info = Utils::getSystemInfo();
foreach ($this->getSystemRequirements() as $lib => $required_version) {
// Check if the library exists
if (!isset($system_info[$lib])) {
return true;
}
// Check exact version of the library
$version_constrain = new VersionExpression($required_version);
if (!$version_constrain->satisfiedBy(new Version($system_info[$lib]))) {
return true;
}
}
return false;
}
/**
* Checks if the plugin can be enabled.
*
@ -240,6 +277,10 @@ class PluginInfo
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) {

View File

@ -82,6 +82,39 @@ interface PluginInterface
*/
public static function getDependencies();
/**
* Returns list of plugin's system requirements.
*
* Each key in the array is a string with a requirement name. Each value is
* plugin version constrain.
*
* A requirement name can be on of the following:
* - "mibew": Mibew core;
* - "php": PHP used in the system;
* - "ext-*": name of a PHP extension.
*
* A constrain can be in one of the following
* formats:
* - "1.2.3": exact version number;
* - ">1.2.3": grater than a specific version;
* - ">=1.2.3": greater than a specific version or equal to it;
* - "<1.2.3": less than a specific version;
* - "<=1.2.3": less than a specific version or equal to it;
* - "1.2.3 - 2.3.4": equals to ">=1.2.3 <=2.3.4";
* - "~1.2.3": equivalent for ">=1.2.3 <1.3.0";
* - "~1.2": equivalent for ">=1.2.0 <2.0.0";
* - "^1.2.3" equivalent for ">=1.2.3 <2.0.0";
* - "^0.1.2" equivalent for ">=0.1.2 <0.2.0";
* - "1.2.x": equivalent for ">=1.2.0 <2.0.0";
* - "1.x": equivalent for ">=1.0.0 <2.0.0";
*
* If the plugin have no system requirements an empty array should be
* returned.
*
* @return array List of plugin's requirements.
*/
public static function getSystemRequirements();
/**
* Returns some info about plugin such as human-readable name, description,
* version, etc.

View File

@ -140,6 +140,17 @@ class PluginManager
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);
}

View File

@ -115,6 +115,34 @@ class Utils
return true;
}
/**
* Returns list of environment items.
*
* @return array List of environment items. Each key is a string with one of
* the following value:
* - "mibew": represents Mibew core;
* - "php": represents PHP installed in the system;
* - "ext-*": represents one of PHP extensions.
* Each value of the array is version of the item.
*/
public static function getSystemInfo()
{
static $system = null;
if (is_null($system)) {
$system = array(
'php' => phpversion(),
'mibew' => MIBEW_VERSION,
);
foreach (get_loaded_extensions() as $ext) {
$system['ext-' . str_replace(' ', '-', $ext)] = phpversion($ext) ?: '0.0.0';
}
}
return $system;
}
/**
* This class should not be instantiated
*/