123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- <?php
- /*
- * This file is part of Composer.
- *
- * (c) Nils Adermann <naderman@naderman.de>
- * Jordi Boggiano <j.boggiano@seld.be>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Composer\DependencyResolver;
- use Composer\Package\CompletePackage;
- use Composer\Package\PackageInterface;
- use Composer\Package\Link;
- /**
- * @author Nils Adermann <naderman@naderman.de>
- */
- class Rule
- {
- // reason constants
- const RULE_INTERNAL_ALLOW_UPDATE = 1;
- const RULE_JOB_INSTALL = 2;
- const RULE_JOB_REMOVE = 3;
- const RULE_PACKAGE_CONFLICT = 6;
- const RULE_PACKAGE_REQUIRES = 7;
- const RULE_PACKAGE_OBSOLETES = 8;
- const RULE_INSTALLED_PACKAGE_OBSOLETES = 9;
- const RULE_PACKAGE_SAME_NAME = 10;
- const RULE_PACKAGE_IMPLICIT_OBSOLETES = 11;
- const RULE_LEARNED = 12;
- const RULE_PACKAGE_ALIAS = 13;
- // bitfield defs
- const BITFIELD_TYPE = 0;
- const BITFIELD_REASON = 8;
- const BITFIELD_DISABLED = 16;
- /**
- * READ-ONLY: The literals this rule consists of.
- * @var array
- */
- public $literals;
- protected $bitfield;
- protected $reasonData;
- /**
- * @param array $literals
- * @param int $reason A RULE_* constant describing the reason for generating this rule
- * @param Link|PackageInterface $reasonData
- * @param array $job The job this rule was created from
- */
- public function __construct(array $literals, $reason, $reasonData, $job = null)
- {
- // sort all packages ascending by id
- sort($literals);
- $this->literals = $literals;
- $this->reasonData = $reasonData;
- if ($job) {
- $this->job = $job;
- }
- $this->bitfield = (0 << self::BITFIELD_DISABLED) |
- ($reason << self::BITFIELD_REASON) |
- (255 << self::BITFIELD_TYPE);
- }
- public function getHash()
- {
- $data = unpack('ihash', md5(implode(',', $this->literals), true));
- return $data['hash'];
- }
- public function getJob()
- {
- return isset($this->job) ? $this->job : null;
- }
- public function getReason()
- {
- return ($this->bitfield & (255 << self::BITFIELD_REASON)) >> self::BITFIELD_REASON;
- }
- public function getReasonData()
- {
- return $this->reasonData;
- }
- public function getRequiredPackage()
- {
- if ($this->getReason() === self::RULE_JOB_INSTALL) {
- return $this->reasonData;
- }
- if ($this->getReason() === self::RULE_PACKAGE_REQUIRES) {
- return $this->reasonData->getTarget();
- }
- }
- /**
- * Checks if this rule is equal to another one
- *
- * Ignores whether either of the rules is disabled.
- *
- * @param Rule $rule The rule to check against
- * @return bool Whether the rules are equal
- */
- public function equals(Rule $rule)
- {
- if (count($this->literals) != count($rule->literals)) {
- return false;
- }
- for ($i = 0, $n = count($this->literals); $i < $n; $i++) {
- if ($this->literals[$i] !== $rule->literals[$i]) {
- return false;
- }
- }
- return true;
- }
- public function setType($type)
- {
- $this->bitfield = ($this->bitfield & ~(255 << self::BITFIELD_TYPE)) | ((255 & $type) << self::BITFIELD_TYPE);
- }
- public function getType()
- {
- return ($this->bitfield & (255 << self::BITFIELD_TYPE)) >> self::BITFIELD_TYPE;
- }
- public function disable()
- {
- $this->bitfield = ($this->bitfield & ~(255 << self::BITFIELD_DISABLED)) | (1 << self::BITFIELD_DISABLED);
- }
- public function enable()
- {
- $this->bitfield = $this->bitfield & ~(255 << self::BITFIELD_DISABLED);
- }
- public function isDisabled()
- {
- return (bool) (($this->bitfield & (255 << self::BITFIELD_DISABLED)) >> self::BITFIELD_DISABLED);
- }
- public function isEnabled()
- {
- return !(($this->bitfield & (255 << self::BITFIELD_DISABLED)) >> self::BITFIELD_DISABLED);
- }
- public function isAssertion()
- {
- return 1 === count($this->literals);
- }
- public function getPrettyString(Pool $pool, array $installedMap = array())
- {
- $ruleText = '';
- foreach ($this->literals as $i => $literal) {
- if ($i != 0) {
- $ruleText .= '|';
- }
- $ruleText .= $pool->literalToPrettyString($literal, $installedMap);
- }
- switch ($this->getReason()) {
- case self::RULE_INTERNAL_ALLOW_UPDATE:
- return $ruleText;
- case self::RULE_JOB_INSTALL:
- return "Install command rule ($ruleText)";
- case self::RULE_JOB_REMOVE:
- return "Remove command rule ($ruleText)";
- case self::RULE_PACKAGE_CONFLICT:
- $package1 = $pool->literalToPackage($this->literals[0]);
- $package2 = $pool->literalToPackage($this->literals[1]);
- return $package1->getPrettyString().' conflicts with '.$this->formatPackagesUnique($pool, array($package2)).'.';
- case self::RULE_PACKAGE_REQUIRES:
- $literals = $this->literals;
- $sourceLiteral = array_shift($literals);
- $sourcePackage = $pool->literalToPackage($sourceLiteral);
- $requires = array();
- foreach ($literals as $literal) {
- $requires[] = $pool->literalToPackage($literal);
- }
- $text = $this->reasonData->getPrettyString($sourcePackage);
- if ($requires) {
- $text .= ' -> satisfiable by ' . $this->formatPackagesUnique($pool, $requires) . '.';
- } else {
- $targetName = $this->reasonData->getTarget();
- if ($targetName === 'php' || $targetName === 'php-64bit' || $targetName === 'hhvm') {
- // handle php/hhvm
- if (defined('HHVM_VERSION')) {
- return $text . ' -> your HHVM version does not satisfy that requirement.';
- } elseif ($targetName === 'hhvm') {
- return $text . ' -> you are running this with PHP and not HHVM.';
- } else {
- $packages = $pool->whatProvides($targetName);
- $package = count($packages) ? current($packages) : phpversion();
- if (!($package instanceof CompletePackage)) {
- return $text . ' -> your PHP version ('.phpversion().') does not satisfy that requirement.';
- }
- $extra = $package->getExtra();
- if (!empty($extra['config.platform'])) {
- $text .= ' -> your PHP version ('.phpversion().') overridden by "config.platform.php" version ('.$package->getPrettyVersion().') does not satisfy that requirement.';
- } else {
- $text .= ' -> your PHP version ('.$package->getPrettyVersion().') does not satisfy that requirement.';
- }
- return $text;
- }
- } elseif (0 === strpos($targetName, 'ext-')) {
- // handle php extensions
- $ext = substr($targetName, 4);
- $error = extension_loaded($ext) ? 'has the wrong version ('.(phpversion($ext) ?: '0').') installed' : 'is missing from your system';
- return $text . ' -> the requested PHP extension '.$ext.' '.$error.'.';
- } elseif (0 === strpos($targetName, 'lib-')) {
- // handle linked libs
- $lib = substr($targetName, 4);
- return $text . ' -> the requested linked library '.$lib.' has the wrong version installed or is missing from your system, make sure to have the extension providing it.';
- } else {
- if ($providers = $pool->whatProvides($targetName, $this->reasonData->getConstraint(), true, true)) {
- return $text . ' -> satisfiable by ' . $this->formatPackagesUnique($pool, $providers) .' but these conflict with your requirements or minimum-stability.';
- }
- return $text . ' -> no matching package found.';
- }
- }
- return $text;
- case self::RULE_PACKAGE_OBSOLETES:
- return $ruleText;
- case self::RULE_INSTALLED_PACKAGE_OBSOLETES:
- return $ruleText;
- case self::RULE_PACKAGE_SAME_NAME:
- return 'Can only install one of: ' . $this->formatPackagesUnique($pool, $this->literals) . '.';
- case self::RULE_PACKAGE_IMPLICIT_OBSOLETES:
- return $ruleText;
- case self::RULE_LEARNED:
- return 'Conclusion: '.$ruleText;
- case self::RULE_PACKAGE_ALIAS:
- return $ruleText;
- default:
- return '('.$ruleText.')';
- }
- }
- protected function formatPackagesUnique($pool, array $packages)
- {
- $prepared = array();
- foreach ($packages as $package) {
- if (!is_object($package)) {
- $package = $pool->literalToPackage($package);
- }
- $prepared[$package->getName()]['name'] = $package->getPrettyName();
- $prepared[$package->getName()]['versions'][$package->getVersion()] = $package->getPrettyVersion();
- }
- foreach ($prepared as $name => $package) {
- $prepared[$name] = $package['name'].'['.implode(', ', $package['versions']).']';
- }
- return implode(', ', $prepared);
- }
- /**
- * Formats a rule as a string of the format (Literal1|Literal2|...)
- *
- * @return string
- */
- public function __toString()
- {
- $result = ($this->isDisabled()) ? 'disabled(' : '(';
- foreach ($this->literals as $i => $literal) {
- if ($i != 0) {
- $result .= '|';
- }
- $result .= $literal;
- }
- $result .= ')';
- return $result;
- }
- }
|