2011-04-05 15:37:19 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of Composer.
|
|
|
|
*
|
|
|
|
* (c) Nils Adermann <naderman@naderman.de>
|
2011-04-16 12:42:35 +00:00
|
|
|
* Jordi Boggiano <j.boggiano@seld.be>
|
2011-04-05 15:37:19 +00:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2011-04-17 19:36:05 +00:00
|
|
|
namespace Composer\Package;
|
2011-04-05 15:37:19 +00:00
|
|
|
|
2011-04-17 22:16:12 +00:00
|
|
|
use Composer\Package\LinkConstraint\LinkConstraintInterface;
|
2011-05-22 07:07:35 +00:00
|
|
|
use Composer\Package\LinkConstraint\VersionConstraint;
|
2011-04-17 21:45:37 +00:00
|
|
|
use Composer\Repository\RepositoryInterface;
|
2011-04-05 15:37:19 +00:00
|
|
|
|
|
|
|
/**
|
2011-04-17 19:36:05 +00:00
|
|
|
* Base class for packages providing name storage and default match implementation
|
|
|
|
*
|
2011-04-05 15:37:19 +00:00
|
|
|
* @author Nils Adermann <naderman@naderman.de>
|
|
|
|
*/
|
2011-04-17 19:49:05 +00:00
|
|
|
abstract class BasePackage implements PackageInterface
|
2011-04-05 15:37:19 +00:00
|
|
|
{
|
|
|
|
protected $name;
|
2011-04-17 20:59:23 +00:00
|
|
|
protected $repository;
|
2011-04-05 15:37:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* All descendents' constructors should call this parent constructor
|
|
|
|
*
|
|
|
|
* @param string $name The package's name
|
|
|
|
*/
|
2011-04-17 19:36:05 +00:00
|
|
|
public function __construct($name)
|
2011-04-05 15:37:19 +00:00
|
|
|
{
|
|
|
|
$this->name = $name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the package's name without version info, thus not a unique identifier
|
|
|
|
*
|
|
|
|
* @return string package name
|
|
|
|
*/
|
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return $this->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a set of names that could refer to this package
|
|
|
|
*
|
|
|
|
* No version or release type information should be included in any of the
|
|
|
|
* names. Provided or replaced package names need to be returned as well.
|
|
|
|
*
|
|
|
|
* @return array An array of strings refering to this package
|
|
|
|
*/
|
|
|
|
public function getNames()
|
|
|
|
{
|
|
|
|
$names = array(
|
|
|
|
$this->getName(),
|
|
|
|
);
|
|
|
|
|
2011-04-17 22:16:12 +00:00
|
|
|
foreach ($this->getProvides() as $link) {
|
|
|
|
$names[] = $link->getTarget();
|
2011-04-05 15:37:19 +00:00
|
|
|
}
|
|
|
|
|
2011-04-17 22:16:12 +00:00
|
|
|
foreach ($this->getReplaces() as $link) {
|
|
|
|
$names[] = $link->getTarget();
|
2011-04-05 15:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $names;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the package matches the given constraint directly or through
|
|
|
|
* provided or replaced packages
|
|
|
|
*
|
2011-04-17 22:16:12 +00:00
|
|
|
* @param string $name Name of the package to be matched
|
|
|
|
* @param LinkConstraintInterface $constraint The constraint to verify
|
|
|
|
* @return bool Whether this package matches the name and constraint
|
2011-04-05 15:37:19 +00:00
|
|
|
*/
|
2011-04-17 22:16:12 +00:00
|
|
|
public function matches($name, LinkConstraintInterface $constraint)
|
2011-04-05 15:37:19 +00:00
|
|
|
{
|
|
|
|
if ($this->name === $name) {
|
2011-05-22 07:07:35 +00:00
|
|
|
return $constraint->matches(new VersionConstraint('=', $this->getVersion(), $this->getReleaseType()));
|
2011-04-05 15:37:19 +00:00
|
|
|
}
|
|
|
|
|
2011-04-17 22:16:12 +00:00
|
|
|
foreach ($this->getProvides() as $link) {
|
|
|
|
if ($link->getTarget() === $name) {
|
|
|
|
return $constraint->matches($link->getConstraint());
|
2011-04-05 15:37:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-17 22:16:12 +00:00
|
|
|
foreach ($this->getReplaces() as $link) {
|
|
|
|
if ($link->getTarget() === $name) {
|
|
|
|
return $constraint->matches($link->getConstraint());
|
2011-04-05 15:37:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-04-17 20:59:23 +00:00
|
|
|
public function getRepository()
|
|
|
|
{
|
|
|
|
return $this->repository;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setRepository(RepositoryInterface $repository)
|
|
|
|
{
|
|
|
|
if ($this->repository) {
|
|
|
|
throw new \LogicException('A package can only be added to one repository');
|
|
|
|
}
|
|
|
|
$this->repository = $repository;
|
|
|
|
}
|
|
|
|
|
2011-04-05 15:37:19 +00:00
|
|
|
/**
|
|
|
|
* Converts the package into a readable and unique string
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function __toString()
|
|
|
|
{
|
2011-05-22 07:08:30 +00:00
|
|
|
return $this->getName().'-'.$this->getVersion().'-'.$this->getReleaseType();
|
2011-04-05 15:37:19 +00:00
|
|
|
}
|
2011-04-21 08:24:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses a version string and returns an array with the version and its type (dev, alpha, beta, RC, stable)
|
|
|
|
*
|
|
|
|
* @param string $version
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function parseVersion($version)
|
|
|
|
{
|
2011-04-23 20:25:10 +00:00
|
|
|
if (!preg_match('#^v?(\d+)(\.\d+)?(\.\d+)?-?((?:beta|RC\d+|alpha|dev)\d*)?$#i', $version, $matches)) {
|
2011-04-21 08:24:19 +00:00
|
|
|
throw new \UnexpectedValueException('Invalid version string '.$version);
|
|
|
|
}
|
|
|
|
|
|
|
|
return array(
|
|
|
|
'version' => $matches[1]
|
|
|
|
.(!empty($matches[2]) ? $matches[2] : '.0')
|
|
|
|
.(!empty($matches[3]) ? $matches[3] : '.0'),
|
|
|
|
'type' => strtolower(!empty($matches[4]) ? $matches[4] : 'stable'),
|
|
|
|
);
|
|
|
|
}
|
2011-04-05 15:37:19 +00:00
|
|
|
}
|