Skip to content

Commit

Permalink
Use root stability flags for already installed packages
Browse files Browse the repository at this point in the history
  • Loading branch information
francoispluchino committed Nov 25, 2014
1 parent 669870c commit 3f23fe3
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
41 changes: 41 additions & 0 deletions Repository/VcsPackageFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,50 @@ protected function getRequireStability(Link $require)
return $this->findInlineStabilities($matches[1]);
}

return $this->getMinimumStabilityFlag($require);
}

/**
* Get the minimum stability for the require dependency defined in root package.
*
* @param Link $require The require link defined in root package
*
* @return string The minimum stability defined in root package (in links or global project)
*/
protected function getMinimumStabilityFlag(Link $require)
{
$flags = $this->package->getStabilityFlags();

if (isset($flags[$require->getTarget()])) {
return $this->findFlagStabilityName($flags[$require->getTarget()]);
}

return $this->package->getMinimumStability();
}

/**
* Find the stability name with the stability value.
*
* @param int $level The stability level.
*
* @return string The stability name
*/
protected function findFlagStabilityName($level)
{
$stability = 'dev';

/* @var string $stabilityName */
/* @var int $stabilityLevel */
foreach (Package::$stabilities as $stabilityName => $stabilityLevel) {
if ($stabilityLevel === $level) {
$stability = $stabilityName;
break;
}
}

return $stability;
}

/**
* Find the lowest stability.
*
Expand Down
44 changes: 44 additions & 0 deletions Tests/Repository/VcsPackageFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Fxp\Composer\AssetPlugin\Tests\Repository;

use Composer\Package\Package;
use Composer\Package\RootPackageInterface;
use Composer\Package\Version\VersionParser;
use Composer\Repository\InstalledFilesystemRepository;
Expand Down Expand Up @@ -412,6 +413,24 @@ public function getDataForInstalledTests()
array($opt2, 'acme/foobar', 'v0.9.0', 'stable', null, null, false),
array($opt3, 'acme/foobar', 'v0.9.0', 'stable', null, null, false),
array($opt4, 'acme/foobar', 'v0.9.0', 'stable', null, null, false),

array($opt1, 'acme/foobar', 'v1.0.0', 'dev', '>=0.9@stable', '1.0.0', true),
array($opt2, 'acme/foobar', 'v1.0.0', 'dev', '>=0.9@stable', '1.0.0', true),
array($opt3, 'acme/foobar', 'v1.0.0', 'dev', '>=0.9@stable', '1.0.0', false),
array($opt4, 'acme/foobar', 'v1.0.0', 'dev', '>=0.9@stable', '1.0.0', false),
array($opt1, 'acme/foobar', 'v0.9.0', 'dev', '>=0.9@stable', '1.0.0', true),
array($opt2, 'acme/foobar', 'v0.9.0', 'dev', '>=0.9@stable', '1.0.0', true),
array($opt3, 'acme/foobar', 'v0.9.0', 'dev', '>=0.9@stable', '1.0.0', false),
array($opt4, 'acme/foobar', 'v0.9.0', 'dev', '>=0.9@stable', '1.0.0', false),

array($opt1, 'acme/foobar', 'v1.0.0', 'dev', '>=0.9@stable', null, false),
array($opt2, 'acme/foobar', 'v1.0.0', 'dev', '>=0.9@stable', null, false),
array($opt3, 'acme/foobar', 'v1.0.0', 'dev', '>=0.9@stable', null, false),
array($opt4, 'acme/foobar', 'v1.0.0', 'dev', '>=0.9@stable', null, false),
array($opt1, 'acme/foobar', 'v0.9.0', 'dev', '>=0.9@stable', null, false),
array($opt2, 'acme/foobar', 'v0.9.0', 'dev', '>=0.9@stable', null, false),
array($opt3, 'acme/foobar', 'v0.9.0', 'dev', '>=0.9@stable', null, false),
array($opt4, 'acme/foobar', 'v0.9.0', 'dev', '>=0.9@stable', null, false),
);
}

Expand Down Expand Up @@ -463,6 +482,7 @@ protected function init(array $requires = array(), $minimumStability = 'stable')
{
$parser = new VersionParser();
$linkRequires = $parser->parseLinks('__ROOT__', '1.0.0', 'requires', $requires);
$stabilityFlags = $this->findStabilityFlags($requires);

$this->package->expects($this->any())
->method('getRequires')
Expand All @@ -473,6 +493,9 @@ protected function init(array $requires = array(), $minimumStability = 'stable')
$this->package->expects($this->any())
->method('getMinimumStability')
->will($this->returnValue($minimumStability));
$this->package->expects($this->any())
->method('getStabilityFlags')
->will($this->returnValue($stabilityFlags));

/* @var RootPackageInterface $package */
$package = $this->package;
Expand Down Expand Up @@ -511,4 +534,25 @@ protected function convertInstalled(array $installed)

return $packages;
}

/**
* Find the stability flag of requires.
*
* @param array $requires The require dependencies
*
* @return array
*/
protected function findStabilityFlags(array $requires)
{
$flags = array();
$stabilities = Package::$stabilities;

foreach ($requires as $require => $prettyConstraint) {
if (preg_match_all('/@('.implode('|', array_keys($stabilities)).')/', $prettyConstraint, $matches)) {
$flags[$require] = $stabilities[$matches[1][0]];
}
}

return $flags;
}
}

0 comments on commit 3f23fe3

Please sign in to comment.