1
0
Fork 0

Move root package handling to a dedicated loader and make ArrayLoader more strict

pull/150/merge
Jordi Boggiano 2011-12-12 21:46:21 +01:00 committed by Nils Adermann
parent a2caa7c5e6
commit f6b9488bf0
14 changed files with 86 additions and 62 deletions

View File

@ -142,7 +142,7 @@ class Application extends BaseApplication
$im->addInstaller(new Installer\InstallerInstaller($vendorDir, $binDir, $dm, $rm->getLocalRepository(), $im));
// load package
$loader = new Package\Loader\ArrayLoader($rm);
$loader = new Package\Loader\RootPackageLoader($rm);
$package = $loader->load($packageConfig);
// load default repository unless it's explicitly disabled

View File

@ -13,6 +13,7 @@
namespace Composer\Package\Loader;
use Composer\Package;
use Composer\Package\Version\VersionParser;
use Composer\Repository\RepositoryManager;
/**
@ -31,49 +32,37 @@ class ArrayLoader
);
protected $versionParser;
private $manager;
public function __construct(RepositoryManager $manager, $parser = null)
public function __construct(VersionParser $parser = null)
{
$this->manager = $manager;
$this->versionParser = $parser;
if (!$parser) {
$this->versionParser = new Package\Version\VersionParser;
$parser = new VersionParser;
}
$this->versionParser = $parser;
}
public function load($config)
{
$prettyVersion = isset($config['version']) ? $config['version'] : '0.0.0';
if (!isset($config['name'])) {
throw new \UnexpectedValueException('Unknown package has no name defined ('.json_encode($config).').');
}
if (!isset($config['version'])) {
throw new \UnexpectedValueException('Package '.$config['name'].' has no version defined.');
}
// handle already normalized versions
if (isset($config['version_normalized'])) {
$version = $config['version_normalized'];
} else {
$version = $this->versionParser->normalize($prettyVersion);
$version = $this->versionParser->normalize($config['version']);
}
$package = new Package\MemoryPackage(isset($config['name']) ? $config['name'] : '__app__', $version, $prettyVersion);
$package->setType(isset($config['type']) ? $config['type'] : 'library');
$package = new Package\MemoryPackage($config['name'], $version, $config['version']);
$package->setType(isset($config['type']) ? strtolower($config['type']) : 'library');
if (isset($config['target-dir'])) {
$package->setTargetDir($config['target-dir']);
}
if (isset($config['repositories'])) {
$repositories = array();
foreach ($config['repositories'] as $repoName => $repo) {
if (false === $repo && 'packagist' === $repoName) {
continue;
}
if (!is_array($repo)) {
throw new \UnexpectedValueException('Repository '.$repoName.' in '.$package->getPrettyName().' '.$package->getVersion().' should be an array, '.gettype($repo).' given');
}
$repository = $this->manager->createRepository(key($repo), current($repo));
$this->manager->addRepository($repository);
}
$package->setRepositories($config['repositories']);
}
if (isset($config['extra']) && is_array($config['extra'])) {
$package->setExtra($config['extra']);
}
@ -167,7 +156,7 @@ class ArrayLoader
$constraint = $package->getPrettyVersion();
}
$parsedConstraint = $this->versionParser->parseConstraints($constraint);
$links[] = new Package\Link($package->getName(), $packageName, $parsedConstraint, $description, $constraint);
$links[] = new Package\Link($package->getName(), $packageName, $parsedConstraint, $description, $constraint);
}
return $links;

View File

@ -0,0 +1,63 @@
<?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\Package\Loader;
use Composer\Package\Version\VersionParser;
use Composer\Repository\RepositoryManager;
/**
* ArrayLoader built for the sole purpose of loading the root package
*
* Sets additional defaults and loads repositories
*
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
class RootPackageLoader extends ArrayLoader
{
private $manager;
public function __construct(RepositoryManager $manager, VersionParser $parser = null)
{
$this->manager = $manager;
parent::__construct($parser);
}
public function load($config)
{
if (!isset($config['name'])) {
$config['name'] = '__root__';
}
if (!isset($config['version'])) {
$config['version'] = '1.0.0-dev';
}
$package = parent::load($config);
if (isset($config['repositories'])) {
$repositories = array();
foreach ($config['repositories'] as $repoName => $repo) {
if (false === $repo && 'packagist' === $repoName) {
continue;
}
if (!is_array($repo)) {
throw new \UnexpectedValueException('Repository '.$repoName.' in '.$package->getPrettyName().' '.$package->getVersion().' should be an array, '.gettype($repo).' given');
}
$repository = $this->manager->createRepository(key($repo), current($repo));
$this->manager->addRepository($repository);
}
$package->setRepositories($config['repositories']);
}
return $package;
}
}

View File

@ -23,7 +23,6 @@ use Composer\Package\Version\VersionParser;
class ArrayRepository implements RepositoryInterface
{
protected $packages;
protected $repositoryManager;
/**
* {@inheritDoc}
@ -42,14 +41,6 @@ class ArrayRepository implements RepositoryInterface
}
}
/**
* {@inheritDoc}
*/
public function setRepositoryManager(RepositoryManager $manager)
{
$this->repositoryManager = $manager;
}
/**
* {@inheritDoc}
*/

View File

@ -47,7 +47,7 @@ class ComposerRepository extends ArrayRepository
throw new \UnexpectedValueException('Could not parse package list from the '.$this->url.' repository');
}
$loader = new ArrayLoader($this->repositoryManager);
$loader = new ArrayLoader();
foreach ($packages as $data) {
foreach ($data['versions'] as $rev) {
$this->addPackage($loader->load($rev));

View File

@ -53,7 +53,7 @@ class FilesystemRepository extends ArrayRepository implements WritableRepository
throw new \UnexpectedValueException('Could not parse package list from the '.$this->file->getPath().' repository');
}
$loader = new ArrayLoader($this->repositoryManager);
$loader = new ArrayLoader();
foreach ($packages as $package) {
$this->addPackage($loader->load($package));
}

View File

@ -47,7 +47,7 @@ class PackageRepository extends ArrayRepository
$this->config = array($this->config);
}
$loader = new ArrayLoader($this->repositoryManager);
$loader = new ArrayLoader();
foreach ($this->config as $package) {
$package = $loader->load($package);
$this->addPackage($package);

View File

@ -56,7 +56,7 @@ class PearRepository extends ArrayRepository
$packagesXML = $this->requestXml($this->url . $categoryLink);
$packages = $packagesXML->getElementsByTagName('p');
$loader = new ArrayLoader($this->repositoryManager);
$loader = new ArrayLoader();
foreach ($packages as $package) {
$packageName = $package->nodeValue;

View File

@ -47,11 +47,4 @@ interface RepositoryInterface extends \Countable
* @return array
*/
function getPackages();
/**
* Stores the RepositoryManager owning this repository
*
* @param RepositoryManager $manager
*/
function setRepositoryManager(RepositoryManager $manager);
}

View File

@ -59,8 +59,6 @@ class RepositoryManager
{
$this->repositories[] = $repository;
$repository->setRepositoryManager($this);
// already initialized, so initialize new repos on the fly
if ($this->initialized) {
$repository->getPackages();
@ -120,7 +118,6 @@ class RepositoryManager
*/
public function setLocalRepository(RepositoryInterface $repository)
{
$repository->setRepositoryManager($this);
$this->localRepository = $repository;
}

View File

@ -69,7 +69,7 @@ class VcsRepository extends ArrayRepository
}
$versionParser = new VersionParser;
$loader = new ArrayLoader($this->repositoryManager);
$loader = new ArrayLoader();
$versions = array();
if ($driver->hasComposerFile($driver->getRootIdentifier())) {

View File

@ -20,11 +20,7 @@ class InstallerInstallerTest extends \PHPUnit_Framework_TestCase
{
protected function setUp()
{
$repositoryManager = $this->getMockBuilder('Composer\Repository\RepositoryManager')
->disableOriginalConstructor()
->getMock();
$loader = new JsonLoader($repositoryManager);
$loader = new JsonLoader();
$this->packages = array();
for ($i = 1; $i <= 3; $i++) {
$this->packages[] = $loader->load(__DIR__.'/Fixtures/installer-v'.$i.'/composer.json');

View File

@ -18,8 +18,7 @@ class ArrayLoaderTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
$this->manager = $this->getMock('Composer\Repository\RepositoryManager');
$this->loader = new ArrayLoader($this->manager);
$this->loader = new ArrayLoader();
}
public function testSelfVersion()

View File

@ -22,7 +22,6 @@ class FilesystemRepositoryTest extends TestCase
$json = $this->createJsonFileMock();
$repository = new FilesystemRepository($json);
$repository->setRepositoryManager($this->getMock('Composer\Repository\RepositoryManager'));
$json
->expects($this->once())
@ -51,7 +50,6 @@ class FilesystemRepositoryTest extends TestCase
$json = $this->createJsonFileMock();
$repository = new FilesystemRepository($json);
$repository->setRepositoryManager($this->getMock('Composer\Repository\RepositoryManager'));
$json
->expects($this->once())
@ -70,7 +68,6 @@ class FilesystemRepositoryTest extends TestCase
$json = $this->createJsonFileMock();
$repository = new FilesystemRepository($json);
$repository->setRepositoryManager($this->getMock('Composer\Repository\RepositoryManager'));
$json
->expects($this->once())
@ -85,7 +82,6 @@ class FilesystemRepositoryTest extends TestCase
$json = $this->createJsonFileMock();
$repository = new FilesystemRepository($json);
$repository->setRepositoryManager($this->getMock('Composer\Repository\RepositoryManager'));
$json
->expects($this->once())