Merge "Linker: Remove outdated comment"
[lhc/web/wiklou.git] / includes / registration / ExtensionRegistry.php
index c58b55e..1f8a27e 100644 (file)
@@ -1,5 +1,7 @@
 <?php
 
+use Composer\Semver\Semver;
+
 /**
  * ExtensionRegistry class
  *
@@ -36,7 +38,7 @@ class ExtensionRegistry {
        /**
         * Bump whenever the registration cache needs resetting
         */
-       const CACHE_VERSION = 6;
+       const CACHE_VERSION = 7;
 
        /**
         * Special key that defines the merge strategy
@@ -381,10 +383,24 @@ class ExtensionRegistry {
        /**
         * Whether a thing has been loaded
         * @param string $name
+        * @param string $constraint The required version constraint for this dependency
+        * @throws LogicException if a specific contraint is asked for,
+        *                        but the extension isn't versioned
         * @return bool
         */
-       public function isLoaded( $name ) {
-               return isset( $this->loaded[$name] );
+       public function isLoaded( $name, $constraint = '*' ) {
+               $isLoaded = isset( $this->loaded[$name] );
+               if ( $constraint === '*' || !$isLoaded ) {
+                       return $isLoaded;
+               }
+               // if a specific constraint is requested, but no version is set, throw an exception
+               if ( !isset( $this->loaded[$name]['version'] ) ) {
+                       $msg = "{$name} does not expose its version, but an extension or a skin"
+                                       . " requires: {$constraint}.";
+                       throw new LogicException( $msg );
+               }
+
+               return SemVer::satisfies( $this->loaded[$name]['version'], $constraint );
        }
 
        /**
@@ -392,11 +408,7 @@ class ExtensionRegistry {
         * @return array
         */
        public function getAttribute( $name ) {
-               if ( isset( $this->attributes[$name] ) ) {
-                       return $this->attributes[$name];
-               } else {
-                       return [];
-               }
+               return $this->attributes[$name] ?? [];
        }
 
        /**