Pārlūkot izejas kodu

Use Composer's json parser to report parse errors to users

Jordi Boggiano 13 gadi atpakaļ
vecāks
revīzija
c10efea40f

+ 10 - 9
src/Packagist/WebBundle/Entity/Package.php

@@ -130,17 +130,18 @@ class Package
         }
         try {
             $information = $repo->getComposerInformation($repo->getRootIdentifier());
-        } catch (\UnexpectedValueException $e) {}
-        // TODO use more specialized exception for repos
 
-        if (!isset($information['name']) || !$information['name']) {
-            $context->addViolation('The package name was not found, your composer.json file must be invalid or missing in your master branch/trunk. Maybe the URL you entered has a typo.', array(), null);
-            return;
-        }
+            if (!isset($information['name']) || !$information['name']) {
+                $context->addViolation('The package name was not found in the composer.json, make sure there is a name present.', array(), null);
+                return;
+            }
 
-        if (!preg_match('{^[a-z0-9_-]+/[a-z0-9_-]+$}i', $information['name'])) {
-            $context->addViolation('The package name '.$information['name'].' is invalid, it should have a vendor name, a forward slash, and a package name, matching <em>[a-z0-9_-]+/[a-z0-9_-]+</em>.', array(), null);
-            return;
+            if (!preg_match('{^[a-z0-9_-]+/[a-z0-9_-]+$}i', $information['name'])) {
+                $context->addViolation('The package name '.$information['name'].' is invalid, it should have a vendor name, a forward slash, and a package name, matching <em>[a-z0-9_-]+/[a-z0-9_-]+</em>.', array(), null);
+                return;
+            }
+        } catch (\UnexpectedValueException $e) {
+            $context->addViolation('We had problems parsing your composer.json file, the parser reports: '.$e->getMessage(), array(), null);
         }
     }
 

+ 5 - 1
src/Packagist/WebBundle/Repository/Repository/GitHubRepository.php

@@ -2,6 +2,8 @@
 
 namespace Packagist\WebBundle\Repository\Repository;
 
+use Composer\Json\JsonFile;
+
 class GitHubRepository implements RepositoryInterface
 {
     protected $owner;
@@ -79,11 +81,13 @@ class GitHubRepository implements RepositoryInterface
     public function getComposerInformation($identifier)
     {
         if (!isset($this->infoCache[$identifier])) {
-            $composer = json_decode(@file_get_contents('https://raw.github.com/'.$this->owner.'/'.$this->repository.'/'.$identifier.'/composer.json'), true);
+            $composer = @file_get_contents('https://raw.github.com/'.$this->owner.'/'.$this->repository.'/'.$identifier.'/composer.json');
             if (!$composer) {
                 throw new \UnexpectedValueException('Failed to retrieve composer information for identifier '.$identifier.' in '.$this->getUrl());
             }
 
+            $composer = JsonFile::parseJson($composer);
+
             if (!isset($composer['time'])) {
                 $commit = json_decode(file_get_contents('http://github.com/api/v2/json/commits/show/'.$this->owner.'/'.$this->repository.'/'.$identifier), true);
                 $composer['time'] = $commit['commit']['committed_date'];