Browse Source

Add InvalidRepositoryException

Jordi Boggiano 12 years ago
parent
commit
4f5d08e2ad

+ 8 - 2
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 {

+ 7 - 3
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();

+ 22 - 0
src/Composer/Repository/InvalidRepositoryException.php

@@ -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
+{
+}

+ 8 - 2
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);
         }
     }

+ 1 - 1
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.');
         }
     }
 

+ 1 - 1
tests/Composer/Test/Repository/FilesystemRepositoryTest.php

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