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 21:32:53 +00:00
|
|
|
namespace Composer\Repository;
|
2011-04-05 15:37:19 +00:00
|
|
|
|
2012-02-23 22:21:33 +00:00
|
|
|
use Composer\Package\AliasPackage;
|
2011-04-17 20:59:23 +00:00
|
|
|
use Composer\Package\PackageInterface;
|
2011-11-20 14:42:12 +00:00
|
|
|
use Composer\Package\Version\VersionParser;
|
2011-04-17 20:59:23 +00:00
|
|
|
|
2011-04-05 15:37:19 +00:00
|
|
|
/**
|
|
|
|
* A repository implementation that simply stores packages in an array
|
|
|
|
*
|
|
|
|
* @author Nils Adermann <naderman@naderman.de>
|
|
|
|
*/
|
|
|
|
class ArrayRepository implements RepositoryInterface
|
|
|
|
{
|
2011-04-17 22:12:40 +00:00
|
|
|
protected $packages;
|
2011-04-05 15:37:19 +00:00
|
|
|
|
2011-10-01 12:33:25 +00:00
|
|
|
/**
|
2011-10-22 20:20:30 +00:00
|
|
|
* {@inheritDoc}
|
2011-10-01 12:33:25 +00:00
|
|
|
*/
|
|
|
|
public function findPackage($name, $version)
|
|
|
|
{
|
2011-11-20 14:42:12 +00:00
|
|
|
// normalize version & name
|
|
|
|
$versionParser = new VersionParser();
|
|
|
|
$version = $versionParser->normalize($version);
|
|
|
|
$name = strtolower($name);
|
|
|
|
|
2011-10-01 12:33:25 +00:00
|
|
|
foreach ($this->getPackages() as $package) {
|
|
|
|
if ($name === $package->getName() && $version === $package->getVersion()) {
|
|
|
|
return $package;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-16 11:44:47 +00:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2012-02-21 13:02:08 +00:00
|
|
|
public function findPackages($name, $version = null)
|
2012-01-16 11:44:47 +00:00
|
|
|
{
|
|
|
|
// normalize name
|
|
|
|
$name = strtolower($name);
|
2012-02-21 13:02:08 +00:00
|
|
|
|
|
|
|
// normalize version
|
|
|
|
if (null !== $version) {
|
|
|
|
$versionParser = new VersionParser();
|
|
|
|
$version = $versionParser->normalize($version);
|
|
|
|
}
|
|
|
|
|
2012-01-16 21:40:14 +00:00
|
|
|
$packages = array();
|
2012-01-16 11:44:47 +00:00
|
|
|
|
2012-01-16 21:40:14 +00:00
|
|
|
foreach ($this->getPackages() as $package) {
|
2012-02-21 13:02:08 +00:00
|
|
|
if ($package->getName() === $name && (null === $version || $version === $package->getVersion())) {
|
2012-01-16 21:40:14 +00:00
|
|
|
$packages[] = $package;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $packages;
|
2012-01-16 11:44:47 +00:00
|
|
|
}
|
|
|
|
|
2011-10-22 20:20:30 +00:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
2011-09-25 17:57:58 +00:00
|
|
|
*/
|
|
|
|
public function hasPackage(PackageInterface $package)
|
|
|
|
{
|
|
|
|
$packageId = $package->getUniqueName();
|
|
|
|
|
|
|
|
foreach ($this->getPackages() as $repoPackage) {
|
|
|
|
if ($packageId === $repoPackage->getUniqueName()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-04-05 15:37:19 +00:00
|
|
|
/**
|
|
|
|
* Adds a new package to the repository
|
|
|
|
*
|
2011-04-17 20:59:23 +00:00
|
|
|
* @param PackageInterface $package
|
2011-04-05 15:37:19 +00:00
|
|
|
*/
|
2011-04-17 20:59:23 +00:00
|
|
|
public function addPackage(PackageInterface $package)
|
2011-04-05 15:37:19 +00:00
|
|
|
{
|
2011-05-22 07:10:03 +00:00
|
|
|
if (null === $this->packages) {
|
|
|
|
$this->initialize();
|
|
|
|
}
|
2011-04-17 20:59:23 +00:00
|
|
|
$package->setRepository($this);
|
|
|
|
$this->packages[] = $package;
|
2012-02-25 01:52:19 +00:00
|
|
|
|
|
|
|
// create alias package on the fly if needed (installed repos manage aliases themselves)
|
|
|
|
if ($package->getAlias() && !$this instanceof InstalledRepositoryInterface) {
|
|
|
|
$this->addPackage($this->createAliasPackage($package));
|
|
|
|
}
|
2011-04-05 15:37:19 +00:00
|
|
|
}
|
|
|
|
|
2012-02-23 22:21:33 +00:00
|
|
|
protected function createAliasPackage(PackageInterface $package)
|
|
|
|
{
|
|
|
|
return new AliasPackage($package, $package->getAlias(), $package->getPrettyAlias());
|
|
|
|
}
|
|
|
|
|
2011-09-25 17:57:58 +00:00
|
|
|
/**
|
|
|
|
* Removes package from repository.
|
|
|
|
*
|
|
|
|
* @param PackageInterface $package package instance
|
|
|
|
*/
|
|
|
|
public function removePackage(PackageInterface $package)
|
|
|
|
{
|
|
|
|
$packageId = $package->getUniqueName();
|
|
|
|
|
|
|
|
foreach ($this->getPackages() as $key => $repoPackage) {
|
|
|
|
if ($packageId === $repoPackage->getUniqueName()) {
|
|
|
|
array_splice($this->packages, $key, 1);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-05 15:37:19 +00:00
|
|
|
/**
|
2011-10-22 20:20:30 +00:00
|
|
|
* {@inheritDoc}
|
2011-04-05 15:37:19 +00:00
|
|
|
*/
|
|
|
|
public function getPackages()
|
|
|
|
{
|
2011-04-17 22:12:40 +00:00
|
|
|
if (null === $this->packages) {
|
|
|
|
$this->initialize();
|
|
|
|
}
|
2011-04-05 15:37:19 +00:00
|
|
|
return $this->packages;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the number of packages in this repository
|
|
|
|
*
|
|
|
|
* @return int Number of packages
|
|
|
|
*/
|
|
|
|
public function count()
|
|
|
|
{
|
|
|
|
return count($this->packages);
|
|
|
|
}
|
2011-04-17 22:12:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes the packages array. Mostly meant as an extension point.
|
|
|
|
*/
|
|
|
|
protected function initialize()
|
|
|
|
{
|
|
|
|
$this->packages = array();
|
|
|
|
}
|
2011-04-05 15:37:19 +00:00
|
|
|
}
|