From 4f5d08e2ad352e128063f5bc0f0691167d5f78e0 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Thu, 8 Nov 2012 14:35:01 +0100 Subject: [PATCH] Add InvalidRepositoryException --- .../Package/Loader/ValidatingArrayLoader.php | 10 +++++++-- .../Repository/FilesystemRepository.php | 10 ++++++--- .../Repository/InvalidRepositoryException.php | 22 +++++++++++++++++++ src/Composer/Repository/PackageRepository.php | 10 +++++++-- src/Composer/Repository/VcsRepository.php | 2 +- .../Repository/FilesystemRepositoryTest.php | 2 +- 6 files changed, 47 insertions(+), 9 deletions(-) create mode 100644 src/Composer/Repository/InvalidRepositoryException.php diff --git a/src/Composer/Package/Loader/ValidatingArrayLoader.php b/src/Composer/Package/Loader/ValidatingArrayLoader.php index 17fb9089f..6c1ff1bb3 100644 --- a/src/Composer/Package/Loader/ValidatingArrayLoader.php +++ b/src/Composer/Package/Loader/ValidatingArrayLoader.php @@ -26,11 +26,13 @@ class ValidatingArrayLoader implements LoaderInterface private $errors; private $warnings; 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->versionParser = $parser ?: new VersionParser(); + $this->strictName = $strictName; } public function load(array $config, $class = 'Composer\Package\CompletePackage') @@ -39,7 +41,11 @@ class ValidatingArrayLoader implements LoaderInterface $this->warnings = array(); $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'])) { try { diff --git a/src/Composer/Repository/FilesystemRepository.php b/src/Composer/Repository/FilesystemRepository.php index 314e45997..1c205cf54 100644 --- a/src/Composer/Repository/FilesystemRepository.php +++ b/src/Composer/Repository/FilesystemRepository.php @@ -48,10 +48,14 @@ class FilesystemRepository extends ArrayRepository implements WritableRepository return; } - $packages = $this->file->read(); + try { + $packages = $this->file->read(); - if (!is_array($packages)) { - throw new \UnexpectedValueException('Could not parse package list from the '.$this->file->getPath().' repository'); + if (!is_array($packages)) { + 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(); diff --git a/src/Composer/Repository/InvalidRepositoryException.php b/src/Composer/Repository/InvalidRepositoryException.php new file mode 100644 index 000000000..061a5048b --- /dev/null +++ b/src/Composer/Repository/InvalidRepositoryException.php @@ -0,0 +1,22 @@ + + * Jordi Boggiano + * + * 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 + */ +class InvalidRepositoryException extends \Exception +{ +} diff --git a/src/Composer/Repository/PackageRepository.php b/src/Composer/Repository/PackageRepository.php index 323cc805a..ca3334204 100644 --- a/src/Composer/Repository/PackageRepository.php +++ b/src/Composer/Repository/PackageRepository.php @@ -13,6 +13,7 @@ namespace Composer\Repository; use Composer\Package\Loader\ArrayLoader; +use Composer\Package\Loader\ValidatingArrayLoader; /** * Package repository. @@ -45,9 +46,14 @@ class PackageRepository extends ArrayRepository { parent::initialize(); - $loader = new ArrayLoader(); + $loader = new ValidatingArrayLoader(new ArrayLoader, false); 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); } } diff --git a/src/Composer/Repository/VcsRepository.php b/src/Composer/Repository/VcsRepository.php index c9c3b3d1f..e2a10a9c8 100644 --- a/src/Composer/Repository/VcsRepository.php +++ b/src/Composer/Repository/VcsRepository.php @@ -252,7 +252,7 @@ class VcsRepository extends ArrayRepository } 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.'); } } diff --git a/tests/Composer/Test/Repository/FilesystemRepositoryTest.php b/tests/Composer/Test/Repository/FilesystemRepositoryTest.php index fee07dfc8..f80a91889 100644 --- a/tests/Composer/Test/Repository/FilesystemRepositoryTest.php +++ b/tests/Composer/Test/Repository/FilesystemRepositoryTest.php @@ -43,7 +43,7 @@ class FilesystemRepositoryTest extends TestCase } /** - * @expectedException \UnexpectedValueException + * @expectedException Composer\Repository\InvalidRepositoryException */ public function testCorruptedRepositoryFile() {