2011-09-25 18:00:05 +00:00
< ? 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 ;
2012-01-16 13:14:15 +00:00
use Composer\IO\IOInterface ;
2012-04-09 14:36:06 +00:00
use Composer\Config ;
2013-11-19 16:45:28 +00:00
use Composer\EventDispatcher\EventDispatcher ;
2015-06-29 03:37:04 +00:00
use Composer\Package\PackageInterface ;
2018-09-12 16:58:54 +00:00
use Composer\Util\HttpDownloader ;
2012-01-10 17:50:16 +00:00
2011-09-25 18:00:05 +00:00
/**
* Repositories manager .
*
2011-10-22 18:49:19 +00:00
* @ author Jordi Boggiano < j . boggiano @ seld . be >
2011-09-25 18:00:05 +00:00
* @ author Konstantin Kudryashov < ever . zet @ gmail . com >
2012-01-10 17:50:16 +00:00
* @ author François Pluchino < francois . pluchino @ opendisplay . com >
2011-09-25 18:00:05 +00:00
*/
class RepositoryManager
{
private $localRepository ;
private $repositories = array ();
2011-10-22 18:49:19 +00:00
private $repositoryClasses = array ();
2012-01-16 13:14:15 +00:00
private $io ;
2012-04-09 14:36:06 +00:00
private $config ;
2013-11-19 16:45:28 +00:00
private $eventDispatcher ;
2016-01-10 20:06:10 +00:00
private $rfs ;
2012-01-10 17:50:16 +00:00
2018-09-12 16:58:54 +00:00
public function __construct ( IOInterface $io , Config $config , EventDispatcher $eventDispatcher = null , HttpDownloader $rfs = null )
2012-01-10 17:50:16 +00:00
{
2012-01-16 13:14:15 +00:00
$this -> io = $io ;
2012-04-09 14:36:06 +00:00
$this -> config = $config ;
2013-11-19 16:45:28 +00:00
$this -> eventDispatcher = $eventDispatcher ;
2016-01-10 20:06:10 +00:00
$this -> rfs = $rfs ;
2012-01-10 17:50:16 +00:00
}
2011-09-25 18:00:05 +00:00
2011-10-01 12:33:25 +00:00
/**
* Searches for a package by it ' s name and version in managed repositories .
*
2015-09-24 14:32:36 +00:00
* @ param string $name package name
* @ param string | \Composer\Semver\Constraint\ConstraintInterface $constraint package version or version constraint to match against
2011-10-01 12:33:25 +00:00
*
2012-05-22 10:07:08 +00:00
* @ return PackageInterface | null
2011-10-01 12:33:25 +00:00
*/
2015-06-18 16:44:58 +00:00
public function findPackage ( $name , $constraint )
2011-10-01 12:33:25 +00:00
{
foreach ( $this -> repositories as $repository ) {
2018-05-21 22:39:25 +00:00
/** @var RepositoryInterface $repository */
2015-06-18 16:44:58 +00:00
if ( $package = $repository -> findPackage ( $name , $constraint )) {
2011-10-01 12:33:25 +00:00
return $package ;
}
}
2016-04-11 14:06:57 +00:00
2016-04-06 22:52:50 +00:00
return null ;
2011-10-01 12:33:25 +00:00
}
2012-02-21 13:02:08 +00:00
/**
* Searches for all packages matching a name and optionally a version in managed repositories .
*
2015-09-24 14:32:36 +00:00
* @ param string $name package name
* @ param string | \Composer\Semver\Constraint\ConstraintInterface $constraint package version or version constraint to match against
2012-02-21 13:02:08 +00:00
*
2018-05-21 22:39:25 +00:00
* @ return PackageInterface []
2012-02-21 13:02:08 +00:00
*/
2015-06-18 16:44:58 +00:00
public function findPackages ( $name , $constraint )
2012-02-21 13:02:08 +00:00
{
$packages = array ();
2018-05-21 22:39:25 +00:00
foreach ( $this -> getRepositories () as $repository ) {
2015-06-18 16:44:58 +00:00
$packages = array_merge ( $packages , $repository -> findPackages ( $name , $constraint ));
2012-02-21 13:02:08 +00:00
}
return $packages ;
}
2011-09-25 18:00:05 +00:00
/**
2011-10-22 18:49:19 +00:00
* Adds repository
2011-09-25 18:00:05 +00:00
*
2012-05-22 10:07:08 +00:00
* @ param RepositoryInterface $repository repository instance
2011-09-25 18:00:05 +00:00
*/
2011-10-22 18:49:19 +00:00
public function addRepository ( RepositoryInterface $repository )
2011-09-25 18:00:05 +00:00
{
2011-10-22 18:49:19 +00:00
$this -> repositories [] = $repository ;
2011-09-25 18:00:05 +00:00
}
2016-02-23 14:06:21 +00:00
/**
* Adds a repository to the beginning of the chain
*
* This is useful when injecting additional repositories that should trump Packagist , e . g . from a plugin .
*
* @ param RepositoryInterface $repository repository instance
*/
public function prependRepository ( RepositoryInterface $repository )
{
array_unshift ( $this -> repositories , $repository );
}
2011-09-25 18:00:05 +00:00
/**
2011-10-22 20:20:30 +00:00
* Returns a new repository for a specific installation type .
2011-09-25 18:00:05 +00:00
*
2013-06-13 11:28:24 +00:00
* @ param string $type repository type
2014-07-23 18:19:29 +00:00
* @ param array $config repository configuration
2016-06-11 14:48:44 +00:00
* @ param string $name repository name
2013-06-13 00:05:44 +00:00
* @ throws \InvalidArgumentException if repository for provided type is not registered
2015-09-28 09:51:14 +00:00
* @ return RepositoryInterface
2011-09-25 18:00:05 +00:00
*/
2016-06-11 14:48:44 +00:00
public function createRepository ( $type , $config , $name = null )
2011-09-25 18:00:05 +00:00
{
2011-10-22 18:49:19 +00:00
if ( ! isset ( $this -> repositoryClasses [ $type ])) {
throw new \InvalidArgumentException ( 'Repository type is not registered: ' . $type );
2011-09-25 18:00:05 +00:00
}
2016-06-11 14:48:44 +00:00
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>' );
}
2011-10-22 20:20:30 +00:00
$class = $this -> repositoryClasses [ $type ];
2012-05-22 10:07:08 +00:00
2016-01-16 17:45:40 +00:00
$reflMethod = new \ReflectionMethod ( $class , '__construct' );
$params = $reflMethod -> getParameters ();
2018-09-12 16:58:54 +00:00
if ( isset ( $params [ 4 ]) && $params [ 4 ] -> getClass () && $params [ 4 ] -> getClass () -> getName () === 'Composer\Util\HttpDownloader' ) {
2016-01-16 17:45:40 +00:00
return new $class ( $config , $this -> io , $this -> config , $this -> eventDispatcher , $this -> rfs );
}
return new $class ( $config , $this -> io , $this -> config , $this -> eventDispatcher );
2011-10-22 18:49:19 +00:00
}
/**
* Stores repository class for a specific installation type .
*
2012-05-22 10:07:08 +00:00
* @ param string $type installation type
* @ param string $class class name of the repo implementation
2011-10-22 18:49:19 +00:00
*/
public function setRepositoryClass ( $type , $class )
{
$this -> repositoryClasses [ $type ] = $class ;
2011-09-25 18:00:05 +00:00
}
/**
* Returns all repositories , except local one .
*
2018-05-21 22:39:25 +00:00
* @ return RepositoryInterface []
2011-09-25 18:00:05 +00:00
*/
public function getRepositories ()
{
return $this -> repositories ;
}
/**
* Sets local repository for the project .
*
2013-04-28 20:32:46 +00:00
* @ param WritableRepositoryInterface $repository repository instance
2011-09-25 18:00:05 +00:00
*/
2013-04-28 20:32:46 +00:00
public function setLocalRepository ( WritableRepositoryInterface $repository )
2011-09-25 18:00:05 +00:00
{
$this -> localRepository = $repository ;
}
/**
* Returns local repository for the project .
*
2013-04-28 20:32:46 +00:00
* @ return WritableRepositoryInterface
2011-09-25 18:00:05 +00:00
*/
public function getLocalRepository ()
{
return $this -> localRepository ;
}
}