<?php
+use Composer\Semver\Semver;
+
/**
* ExtensionRegistry class
*
/**
* 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 );
}
/**
$registry->loadFromQueue();
$this->assertArrayHasKey( 'FooBar', $registry->getAllThings() );
$this->assertTrue( $registry->isLoaded( 'FooBar' ) );
+ $this->assertTrue( $registry->isLoaded( 'FooBar', '*' ) );
$this->assertSame( [ 'test' ], $registry->getAttribute( 'FooBarAttr' ) );
$this->assertSame( [], $registry->getAttribute( 'NotLoadedAttr' ) );
}
+ public function testLoadFromQueueWithConstraintWithVersion() {
+ $registry = new ExtensionRegistry();
+ $registry->queue( "{$this->dataDir}/good_with_version.json" );
+ $registry->loadFromQueue();
+ $this->assertTrue( $registry->isLoaded( 'FooBar', '>= 1.2.0' ) );
+ $this->assertFalse( $registry->isLoaded( 'FooBar', '^1.3.0' ) );
+ }
+
+ /**
+ * @expectedException LogicException
+ */
+ public function testLoadFromQueueWithConstraintWithoutVersion() {
+ $registry = new ExtensionRegistry();
+ $registry->queue( "{$this->dataDir}/good.json" );
+ $registry->loadFromQueue();
+ $registry->isLoaded( 'FooBar', '>= 1.2.0' );
+ }
+
/**
* @expectedException PHPUnit_Framework_Error
*/