Add parsing and on-the-fly loading of repositories defined in packages
parent
17286e993c
commit
9b24734c9d
|
@ -40,6 +40,7 @@ class ArrayDumper
|
||||||
'recommends',
|
'recommends',
|
||||||
'suggests',
|
'suggests',
|
||||||
'autoload',
|
'autoload',
|
||||||
|
'repositories',
|
||||||
);
|
);
|
||||||
|
|
||||||
$data = array();
|
$data = array();
|
||||||
|
|
|
@ -13,6 +13,7 @@
|
||||||
namespace Composer\Package\Loader;
|
namespace Composer\Package\Loader;
|
||||||
|
|
||||||
use Composer\Package;
|
use Composer\Package;
|
||||||
|
use Composer\Repository\RepositoryManager;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Konstantin Kudryashiv <ever.zet@gmail.com>
|
* @author Konstantin Kudryashiv <ever.zet@gmail.com>
|
||||||
|
@ -30,9 +31,11 @@ class ArrayLoader
|
||||||
);
|
);
|
||||||
|
|
||||||
protected $versionParser;
|
protected $versionParser;
|
||||||
|
private $manager;
|
||||||
|
|
||||||
public function __construct($parser = null)
|
public function __construct(RepositoryManager $manager, $parser = null)
|
||||||
{
|
{
|
||||||
|
$this->manager = $manager;
|
||||||
$this->versionParser = $parser;
|
$this->versionParser = $parser;
|
||||||
if (!$parser) {
|
if (!$parser) {
|
||||||
$this->versionParser = new Package\Version\VersionParser;
|
$this->versionParser = new Package\Version\VersionParser;
|
||||||
|
@ -50,6 +53,18 @@ class ArrayLoader
|
||||||
$package->setTargetDir($config['target-dir']);
|
$package->setTargetDir($config['target-dir']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isset($config['repositories'])) {
|
||||||
|
$repositories = array();
|
||||||
|
foreach ($config['repositories'] as $repo) {
|
||||||
|
if (!$repo) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$repository = $this->manager->createRepository(key($repo), current($repo));
|
||||||
|
$this->manager->addRepository($repository);
|
||||||
|
}
|
||||||
|
$package->setRepositories($config['repositories']);
|
||||||
|
}
|
||||||
|
|
||||||
if (isset($config['extra'])) {
|
if (isset($config['extra'])) {
|
||||||
$package->setExtra($config['extra']);
|
$package->setExtra($config['extra']);
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,6 +31,7 @@ class MemoryPackage extends BasePackage
|
||||||
protected $distSha1Checksum;
|
protected $distSha1Checksum;
|
||||||
protected $releaseType;
|
protected $releaseType;
|
||||||
protected $version;
|
protected $version;
|
||||||
|
protected $repositories;
|
||||||
protected $license;
|
protected $license;
|
||||||
protected $extra = array();
|
protected $extra = array();
|
||||||
|
|
||||||
|
@ -233,6 +234,24 @@ class MemoryPackage extends BasePackage
|
||||||
return $this->distSha1Checksum;
|
return $this->distSha1Checksum;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the repositories
|
||||||
|
*
|
||||||
|
* @param string $repositories
|
||||||
|
*/
|
||||||
|
public function setRepositories($repositories)
|
||||||
|
{
|
||||||
|
$this->repositories = $repositories;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function getRepositories()
|
||||||
|
{
|
||||||
|
return $this->repositories;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the release type
|
* Set the release type
|
||||||
*
|
*
|
||||||
|
|
|
@ -228,6 +228,15 @@ interface PackageInterface
|
||||||
*/
|
*/
|
||||||
function getAutoload();
|
function getAutoload();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns an array of repositories
|
||||||
|
*
|
||||||
|
* {"<type>": {<config key/values>}}
|
||||||
|
*
|
||||||
|
* @return array Repositories
|
||||||
|
*/
|
||||||
|
function getRepositories();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stores a reference to the repository that owns the package
|
* Stores a reference to the repository that owns the package
|
||||||
*
|
*
|
||||||
|
|
|
@ -22,14 +22,10 @@ use Composer\Package\PackageInterface;
|
||||||
class ArrayRepository implements RepositoryInterface
|
class ArrayRepository implements RepositoryInterface
|
||||||
{
|
{
|
||||||
protected $packages;
|
protected $packages;
|
||||||
|
protected $repositoryManager;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Searches for a package by it's name and version (if has one).
|
* {@inheritDoc}
|
||||||
*
|
|
||||||
* @param string $name package name
|
|
||||||
* @param string $version package version
|
|
||||||
*
|
|
||||||
* @return PackageInterface|null
|
|
||||||
*/
|
*/
|
||||||
public function findPackage($name, $version)
|
public function findPackage($name, $version)
|
||||||
{
|
{
|
||||||
|
@ -41,11 +37,15 @@ class ArrayRepository implements RepositoryInterface
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if specified package in this repository.
|
* {@inheritDoc}
|
||||||
*
|
*/
|
||||||
* @param PackageInterface $package package instance
|
public function setRepositoryManager(RepositoryManager $manager)
|
||||||
*
|
{
|
||||||
* @return Boolean
|
$this->repositoryManager = $manager;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
public function hasPackage(PackageInterface $package)
|
public function hasPackage(PackageInterface $package)
|
||||||
{
|
{
|
||||||
|
@ -93,9 +93,7 @@ class ArrayRepository implements RepositoryInterface
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns all contained packages
|
* {@inheritDoc}
|
||||||
*
|
|
||||||
* @return array All packages
|
|
||||||
*/
|
*/
|
||||||
public function getPackages()
|
public function getPackages()
|
||||||
{
|
{
|
||||||
|
|
|
@ -51,7 +51,7 @@ class ComposerRepository extends ArrayRepository
|
||||||
private function createPackages($data)
|
private function createPackages($data)
|
||||||
{
|
{
|
||||||
foreach ($data['versions'] as $rev) {
|
foreach ($data['versions'] as $rev) {
|
||||||
$loader = new ArrayLoader();
|
$loader = new ArrayLoader($this->repositoryManager);
|
||||||
$this->addPackage($loader->load($rev));
|
$this->addPackage($loader->load($rev));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,4 +47,11 @@ interface RepositoryInterface extends \Countable
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
function getPackages();
|
function getPackages();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stores the RepositoryManager owning this repository
|
||||||
|
*
|
||||||
|
* @param RepositoryManager $manager
|
||||||
|
*/
|
||||||
|
function setRepositoryManager(RepositoryManager $manager);
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,6 +24,15 @@ class RepositoryManager
|
||||||
private $repositories = array();
|
private $repositories = array();
|
||||||
private $repositoryClasses = array();
|
private $repositoryClasses = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used for lazy loading of packages and their contained repositories
|
||||||
|
*
|
||||||
|
* This is a performance optimization to avoid loading all packages unless they are needed
|
||||||
|
*
|
||||||
|
* @var Boolean
|
||||||
|
*/
|
||||||
|
private $initialized;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Searches for a package by it's name and version in managed repositories.
|
* Searches for a package by it's name and version in managed repositories.
|
||||||
*
|
*
|
||||||
|
@ -49,24 +58,31 @@ class RepositoryManager
|
||||||
public function addRepository(RepositoryInterface $repository)
|
public function addRepository(RepositoryInterface $repository)
|
||||||
{
|
{
|
||||||
$this->repositories[] = $repository;
|
$this->repositories[] = $repository;
|
||||||
|
|
||||||
|
$repository->setRepositoryManager($this);
|
||||||
|
|
||||||
|
// already initialized, so initialize new repos on the fly
|
||||||
|
if ($this->initialized) {
|
||||||
|
$repository->getPackages();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns repository class for a specific installation type.
|
* Returns a new repository for a specific installation type.
|
||||||
*
|
|
||||||
* @param string $type installation type
|
|
||||||
*
|
*
|
||||||
|
* @param string $type repository type
|
||||||
|
* @param string $config repository configuration
|
||||||
* @return RepositoryInterface
|
* @return RepositoryInterface
|
||||||
*
|
|
||||||
* @throws InvalidArgumentException if repository for provided type is not registeterd
|
* @throws InvalidArgumentException if repository for provided type is not registeterd
|
||||||
*/
|
*/
|
||||||
public function getRepositoryClass($type)
|
public function createRepository($type, $config)
|
||||||
{
|
{
|
||||||
if (!isset($this->repositoryClasses[$type])) {
|
if (!isset($this->repositoryClasses[$type])) {
|
||||||
throw new \InvalidArgumentException('Repository type is not registered: '.$type);
|
throw new \InvalidArgumentException('Repository type is not registered: '.$type);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->repositoryClasses[$type];
|
$class = $this->repositoryClasses[$type];
|
||||||
|
return new $class($config);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -87,6 +103,13 @@ class RepositoryManager
|
||||||
*/
|
*/
|
||||||
public function getRepositories()
|
public function getRepositories()
|
||||||
{
|
{
|
||||||
|
if (!$this->initialized) {
|
||||||
|
$this->initialized = true;
|
||||||
|
// warm up repos to be sure all sub-repos are added before we return
|
||||||
|
foreach ($this->repositories as $repository) {
|
||||||
|
$repository->getPackages();
|
||||||
|
}
|
||||||
|
}
|
||||||
return $this->repositories;
|
return $this->repositories;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue