Merge "Pass "services" through from coreRoutes.json to ObjectFactory"
[lhc/web/wiklou.git] / includes / registration / VersionChecker.php
index 93b4a14..a5d1fa1 100644 (file)
@@ -40,6 +40,21 @@ class VersionChecker {
         */
        private $phpVersion = false;
 
+       /**
+        * @var string[] List of installed PHP extensions
+        */
+       private $phpExtensions = [];
+
+       /**
+        * @var bool[] List of provided abilities
+        */
+       private $abilities = [];
+
+       /**
+        * @var string[] List of provided ability errors
+        */
+       private $abilityErrors = [];
+
        /**
         * @var array Loaded extensions
         */
@@ -52,11 +67,21 @@ class VersionChecker {
 
        /**
         * @param string $coreVersion Current version of core
+        * @param string $phpVersion Current PHP version
+        * @param string[] $phpExtensions List of installed PHP extensions
+        * @param bool[] $abilities List of provided abilities
+        * @param string[] $abilityErrors Error messages for the abilities
         */
-       public function __construct( $coreVersion, $phpVersion ) {
+       public function __construct(
+               $coreVersion, $phpVersion, array $phpExtensions,
+               array $abilities = [], array $abilityErrors = []
+       ) {
                $this->versionParser = new VersionParser();
                $this->setCoreVersion( $coreVersion );
                $this->setPhpVersion( $phpVersion );
+               $this->phpExtensions = $phpExtensions;
+               $this->abilities = $abilities;
+               $this->abilityErrors = $abilityErrors;
        }
 
        /**
@@ -112,7 +137,9 @@ class VersionChecker {
         *       'FooBar' => {
         *         'MediaWiki' => '>= 1.25.0',
         *         'platform': {
-        *           'php': '>= 7.0.0'
+        *           'php': '>= 7.0.0',
+        *           'ext-foo': '*',
+        *           'ability-bar': true
         *         },
         *         'extensions' => {
         *           'FooBaz' => '>= 1.25.0'
@@ -151,6 +178,7 @@ class VersionChecker {
                                        case 'platform':
                                                foreach ( $values as $dependency => $constraint ) {
                                                        if ( $dependency === 'php' ) {
+                                                               // PHP version
                                                                $phpError = $this->handleDependency(
                                                                        $this->phpVersion,
                                                                        $constraint,
@@ -166,6 +194,54 @@ class VersionChecker {
                                                                                'type' => 'incompatible-php',
                                                                        ];
                                                                }
+                                                       } elseif ( substr( $dependency, 0, 4 ) === 'ext-' ) {
+                                                               // PHP extensions
+                                                               $phpExtension = substr( $dependency, 4 );
+                                                               if ( $constraint !== '*' ) {
+                                                                       throw new UnexpectedValueException( 'Version constraints for '
+                                                                               . 'PHP extensions are not supported in ' . $extension );
+                                                               }
+                                                               if ( !in_array( $phpExtension, $this->phpExtensions, true ) ) {
+                                                                       $errors[] = [
+                                                                               'msg' =>
+                                                                                       "{$extension} requires {$phpExtension} PHP extension "
+                                                                                       . "to be installed."
+                                                                               ,
+                                                                               'type' => 'missing-phpExtension',
+                                                                               'missing' => $phpExtension,
+                                                                       ];
+                                                               }
+                                                       } elseif ( substr( $dependency, 0, 8 ) === 'ability-' ) {
+                                                               // Other abilities the environment might provide.
+                                                               $ability = substr( $dependency, 8 );
+                                                               if ( !isset( $this->abilities[$ability] ) ) {
+                                                                       throw new UnexpectedValueException( 'Dependency type '
+                                                                       . $dependency . ' unknown in ' . $extension );
+                                                               }
+                                                               if ( !is_bool( $constraint ) ) {
+                                                                       throw new UnexpectedValueException( 'Only booleans are '
+                                                                               . 'allowed to to indicate the presence of abilities '
+                                                                               . 'in ' . $extension );
+                                                               }
+
+                                                               if ( $constraint === true &&
+                                                                       $this->abilities[$ability] !== true
+                                                               ) {
+                                                                       // add custom error message for missing ability if specified
+                                                                       $customMessage = '';
+                                                                       if ( isset( $this->abilityErrors[$ability] ) ) {
+                                                                               $customMessage = ': ' . $this->abilityErrors[$ability];
+                                                                       }
+
+                                                                       $errors[] = [
+                                                                               'msg' =>
+                                                                                       "{$extension} requires \"{$ability}\" ability"
+                                                                                       . $customMessage
+                                                                               ,
+                                                                               'type' => 'missing-ability',
+                                                                               'missing' => $ability,
+                                                                       ];
+                                                               }
                                                        } else {
                                                                // add other platform dependencies here
                                                                throw new UnexpectedValueException( 'Dependency type ' . $dependency .