1
0
Fork 0

Add InvalidRepositoryException

pull/1303/head
Jordi Boggiano 2012-11-08 14:35:01 +01:00
parent 150b7867e3
commit 4f5d08e2ad
6 changed files with 47 additions and 9 deletions

View File

@ -26,11 +26,13 @@ class ValidatingArrayLoader implements LoaderInterface
private $errors; private $errors;
private $warnings; private $warnings;
private $config; private $config;
private $strictName;
public function __construct(LoaderInterface $loader, VersionParser $parser = null) public function __construct(LoaderInterface $loader, $strictName = true, VersionParser $parser = null)
{ {
$this->loader = $loader; $this->loader = $loader;
$this->versionParser = $parser ?: new VersionParser(); $this->versionParser = $parser ?: new VersionParser();
$this->strictName = $strictName;
} }
public function load(array $config, $class = 'Composer\Package\CompletePackage') public function load(array $config, $class = 'Composer\Package\CompletePackage')
@ -39,7 +41,11 @@ class ValidatingArrayLoader implements LoaderInterface
$this->warnings = array(); $this->warnings = array();
$this->config = $config; $this->config = $config;
$this->validateRegex('name', '[A-Za-z0-9][A-Za-z0-9_.-]*/[A-Za-z0-9][A-Za-z0-9_.-]*', true); if ($this->strictName) {
$this->validateRegex('name', '[A-Za-z0-9][A-Za-z0-9_.-]*/[A-Za-z0-9][A-Za-z0-9_.-]*', true);
} else {
$this->validateString('name', true);
}
if (!empty($this->config['version'])) { if (!empty($this->config['version'])) {
try { try {

View File

@ -48,10 +48,14 @@ class FilesystemRepository extends ArrayRepository implements WritableRepository
return; return;
} }
$packages = $this->file->read(); try {
$packages = $this->file->read();
if (!is_array($packages)) { if (!is_array($packages)) {
throw new \UnexpectedValueException('Could not parse package list from the '.$this->file->getPath().' repository'); throw new \UnexpectedValueException('Could not parse package list from the repository');
}
} catch (\Exception $e) {
throw new InvalidRepositoryException('Invalid repository data in '.$this->file->getPath().', packages could not be loaded: ['.get_class($e).'] '.$e->getMessage());
} }
$loader = new ArrayLoader(); $loader = new ArrayLoader();

View File

@ -0,0 +1,22 @@
<?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;
/**
* Exception thrown when a package repository is utterly broken
*
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
class InvalidRepositoryException extends \Exception
{
}

View File

@ -13,6 +13,7 @@
namespace Composer\Repository; namespace Composer\Repository;
use Composer\Package\Loader\ArrayLoader; use Composer\Package\Loader\ArrayLoader;
use Composer\Package\Loader\ValidatingArrayLoader;
/** /**
* Package repository. * Package repository.
@ -45,9 +46,14 @@ class PackageRepository extends ArrayRepository
{ {
parent::initialize(); parent::initialize();
$loader = new ArrayLoader(); $loader = new ValidatingArrayLoader(new ArrayLoader, false);
foreach ($this->config as $package) { foreach ($this->config as $package) {
$package = $loader->load($package); try {
$package = $loader->load($package);
} catch (\Exception $e) {
throw new InvalidRepositoryException('A repository of type "package" contains an invalid package definition: '.$e->getMessage()."\n\nInvalid package definition:\n".json_encode($package));
}
$this->addPackage($package); $this->addPackage($package);
} }
} }

View File

@ -252,7 +252,7 @@ class VcsRepository extends ArrayRepository
} }
if (!$this->getPackages()) { if (!$this->getPackages()) {
throw new \RuntimeException('No valid composer.json was found in any branch or tag of '.$this->url.', could not load a package from it.'); throw new InvalidRepositoryException('No valid composer.json was found in any branch or tag of '.$this->url.', could not load a package from it.');
} }
} }

View File

@ -43,7 +43,7 @@ class FilesystemRepositoryTest extends TestCase
} }
/** /**
* @expectedException \UnexpectedValueException * @expectedException Composer\Repository\InvalidRepositoryException
*/ */
public function testCorruptedRepositoryFile() public function testCorruptedRepositoryFile()
{ {