<?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\Repository;
use Composer\IO\IOInterface;
use Composer\Config;
use Composer\EventDispatcher\EventDispatcher;
use Composer\Package\PackageInterface;
use Composer\Util\RemoteFilesystem;
/**
* Repositories manager.
*
* @author Jordi Boggiano <j.boggiano@seld.be>
* @author Konstantin Kudryashov <ever.zet@gmail.com>
* @author François Pluchino <francois.pluchino@opendisplay.com>
*/
class RepositoryManager
{
private $localRepository;
private $repositories = array();
private $repositoryClasses = array();
private $io;
private $config;
private $eventDispatcher;
private $rfs;
public function __construct(IOInterface $io, Config $config, EventDispatcher $eventDispatcher = null, RemoteFilesystem $rfs = null)
{
$this->io = $io;
$this->config = $config;
$this->eventDispatcher = $eventDispatcher;
$this->rfs = $rfs;
}
public function findPackage($name, $constraint)
{
foreach ($this->repositories as $repository) {
if ($package = $repository->findPackage($name, $constraint)) {
return $package;
}
}
return null;
}
public function findPackages($name, $constraint)
{
$packages = array();
foreach ($this->getRepositories() as $repository) {
$packages = array_merge($packages, $repository->findPackages($name, $constraint));
}
return $packages;
}
public function addRepository(RepositoryInterface $repository)
{
$this->repositories[] = $repository;
}
public function prependRepository(RepositoryInterface $repository)
{
array_unshift($this->repositories, $repository);
}
public function createRepository($type, $config, $name = null)
{
if (!isset($this->repositoryClasses[$type])) {
throw new \InvalidArgumentException('Repository type is not registered: '.$type);
}
if (isset($config['packagist']) && false === $config['packagist']) {
$this->io->writeError('<warning>Repository "'.$name.'" ('.json_encode($config).') has a packagist key which should be in its own repository definition</warning>');
}
$class = $this->repositoryClasses[$type];
$reflMethod = new \ReflectionMethod($class, '__construct');
$params = $reflMethod->getParameters();
if (isset($params[4]) && $params[4]->getClass() && $params[4]->getClass()->getName() === 'Composer\Util\RemoteFilesystem') {
return new $class($config, $this->io, $this->config, $this->eventDispatcher, $this->rfs);
}
return new $class($config, $this->io, $this->config, $this->eventDispatcher);
}
public function setRepositoryClass($type, $class)
{
$this->repositoryClasses[$type] = $class;
}
public function getRepositories()
{
return $this->repositories;
}
public function setLocalRepository(WritableRepositoryInterface $repository)
{
$this->localRepository = $repository;
}
public function getLocalRepository()
{
return $this->localRepository;
}
}