Exemple #1
0
 public function testShouldCreatePackages()
 {
     $reader = $this->getMockBuilder('\\Composer\\Repository\\PearRepository')->disableOriginalConstructor()->getMock();
     $ref = new \ReflectionMethod($reader, 'buildComposerPackages');
     $ref->setAccessible(true);
     $channelInfo = new ChannelInfo('test.loc', 'test', array(new PackageInfo('test.loc', 'sample', 'license', 'shortDescription', 'description', array('1.0.0.1' => new ReleaseInfo('stable', new DependencyInfo(array(new DependencyConstraint('required', '> 5.2.0.0', 'php', ''), new DependencyConstraint('conflicts', '== 2.5.6.0', 'pear.php.net', 'broken')), array('*' => array(new DependencyConstraint('optional', '*', 'ext', 'xml')))))))));
     $packages = $ref->invoke($reader, $channelInfo, new VersionParser());
     $expectedPackage = new MemoryPackage('pear-test.loc/sample', '1.0.0.1', '1.0.0.1');
     $expectedPackage->setType('pear-library');
     $expectedPackage->setDistType('file');
     $expectedPackage->setDescription('description');
     $expectedPackage->setDistUrl("http://test.loc/get/sample-1.0.0.1.tgz");
     $expectedPackage->setAutoload(array('classmap' => array('')));
     $expectedPackage->setIncludePaths(array('/'));
     $expectedPackage->setRequires(array(new Link('pear-test.loc/sample', 'php', $this->createConstraint('>', '5.2.0.0'), 'required', '> 5.2.0.0')));
     $expectedPackage->setConflicts(array(new Link('pear-test.loc/sample', 'pear-pear.php.net/broken', $this->createConstraint('==', '2.5.6.0'), 'conflicts', '== 2.5.6.0')));
     $expectedPackage->setSuggests(array('*-ext-xml' => '*'));
     $expectedPackage->setReplaces(array(new Link('pear-test.loc/sample', 'pear-test/sample', new VersionConstraint('==', '1.0.0.1'), 'replaces', '== 1.0.0.1')));
     $this->assertCount(1, $packages);
     $this->assertEquals($expectedPackage, $packages[0], 0, 1);
 }
Exemple #2
0
 public function load(array $config)
 {
     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($config['version']);
     }
     $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['extra']) && is_array($config['extra'])) {
         $package->setExtra($config['extra']);
     }
     if (isset($config['bin'])) {
         if (!is_array($config['bin'])) {
             throw new \UnexpectedValueException('Package ' . $config['name'] . '\'s bin key should be an array, ' . gettype($config['bin']) . ' given.');
         }
         foreach ($config['bin'] as $key => $bin) {
             $config['bin'][$key] = ltrim($bin, '/');
         }
         $package->setBinaries($config['bin']);
     }
     if (isset($config['scripts']) && is_array($config['scripts'])) {
         foreach ($config['scripts'] as $event => $listeners) {
             $config['scripts'][$event] = (array) $listeners;
         }
         $package->setScripts($config['scripts']);
     }
     if (!empty($config['description']) && is_string($config['description'])) {
         $package->setDescription($config['description']);
     }
     if (!empty($config['homepage']) && is_string($config['homepage'])) {
         $package->setHomepage($config['homepage']);
     }
     if (!empty($config['keywords']) && is_array($config['keywords'])) {
         $package->setKeywords($config['keywords']);
     }
     if (!empty($config['license'])) {
         $package->setLicense(is_array($config['license']) ? $config['license'] : array($config['license']));
     }
     if (!empty($config['time'])) {
         try {
             $date = new \DateTime($config['time']);
             $date->setTimezone(new \DateTimeZone('UTC'));
             $package->setReleaseDate($date);
         } catch (\Exception $e) {
         }
     }
     if (!empty($config['authors']) && is_array($config['authors'])) {
         $package->setAuthors($config['authors']);
     }
     if (isset($config['installation-source'])) {
         $package->setInstallationSource($config['installation-source']);
     }
     if (isset($config['source'])) {
         if (!isset($config['source']['type']) || !isset($config['source']['url'])) {
             throw new \UnexpectedValueException(sprintf("package source should be specified as {\"type\": ..., \"url\": ...},\n%s given", json_encode($config['source'])));
         }
         $package->setSourceType($config['source']['type']);
         $package->setSourceUrl($config['source']['url']);
         $package->setSourceReference($config['source']['reference']);
     }
     if (isset($config['dist'])) {
         if (!isset($config['dist']['type']) || !isset($config['dist']['url'])) {
             throw new \UnexpectedValueException(sprintf("package dist should be specified as " . "{\"type\": ..., \"url\": ..., \"reference\": ..., \"shasum\": ...},\n%s given", json_encode($config['dist'])));
         }
         $package->setDistType($config['dist']['type']);
         $package->setDistUrl($config['dist']['url']);
         $package->setDistReference(isset($config['dist']['reference']) ? $config['dist']['reference'] : null);
         $package->setDistSha1Checksum(isset($config['dist']['shasum']) ? $config['dist']['shasum'] : null);
     }
     // check for a branch alias (dev-master => 1.0.x-dev for example) if this is a named branch
     if ('dev-' === substr($package->getPrettyVersion(), 0, 4) && isset($config['extra']['branch-alias']) && is_array($config['extra']['branch-alias'])) {
         foreach ($config['extra']['branch-alias'] as $sourceBranch => $targetBranch) {
             // ensure it is an alias to a -dev package
             if ('-dev' !== substr($targetBranch, -4)) {
                 continue;
             }
             // normalize without -dev and ensure it's a numeric branch that is parseable
             $validatedTargetBranch = $this->versionParser->normalizeBranch(substr($targetBranch, 0, -4));
             if ('-dev' !== substr($validatedTargetBranch, -4)) {
                 continue;
             }
             // ensure that it is the current branch aliasing itself
             if (strtolower($package->getPrettyVersion()) !== strtolower($sourceBranch)) {
                 continue;
             }
             $package->setAlias($validatedTargetBranch);
             $package->setPrettyAlias(preg_replace('{(\\.9{7})+}', '.x', $validatedTargetBranch));
             break;
         }
     }
     foreach (Package\BasePackage::$supportedLinkTypes as $type => $opts) {
         if (isset($config[$type])) {
             $method = 'set' . ucfirst($opts['method']);
             $package->{$method}($this->loadLinksFromConfig($package, $opts['description'], $config[$type]));
         }
     }
     if (isset($config['suggest']) && is_array($config['suggest'])) {
         foreach ($config['suggest'] as $target => $reason) {
             if ('self.version' === trim($reason)) {
                 $config['suggest'][$target] = $package->getPrettyVersion();
             }
         }
         $package->setSuggests($config['suggest']);
     }
     if (isset($config['autoload'])) {
         $package->setAutoload($config['autoload']);
     }
     if (isset($config['include-path'])) {
         $package->setIncludePaths($config['include-path']);
     }
     if (isset($config['support'])) {
         $package->setSupport($config['support']);
     }
     return $package;
 }
Exemple #3
0
 public function load($config)
 {
     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($config['version']);
     }
     $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['extra']) && is_array($config['extra'])) {
         $package->setExtra($config['extra']);
     }
     if (isset($config['bin']) && is_array($config['bin'])) {
         foreach ($config['bin'] as $key => $bin) {
             $config['bin'][$key] = ltrim($bin, '/');
         }
         $package->setBinaries($config['bin']);
     }
     if (isset($config['scripts']) && is_array($config['scripts'])) {
         foreach ($config['scripts'] as $event => $listeners) {
             $config['scripts'][$event] = (array) $listeners;
         }
         $package->setScripts($config['scripts']);
     }
     if (!empty($config['description']) && is_string($config['description'])) {
         $package->setDescription($config['description']);
     }
     if (!empty($config['homepage']) && is_string($config['homepage'])) {
         $package->setHomepage($config['homepage']);
     }
     if (!empty($config['keywords'])) {
         $package->setKeywords(is_array($config['keywords']) ? $config['keywords'] : array($config['keywords']));
     }
     if (!empty($config['license'])) {
         $package->setLicense(is_array($config['license']) ? $config['license'] : array($config['license']));
     }
     if (!empty($config['time'])) {
         try {
             $package->setReleaseDate(new \DateTime($config['time']));
         } catch (\Exception $e) {
         }
     }
     if (!empty($config['authors']) && is_array($config['authors'])) {
         $package->setAuthors($config['authors']);
     }
     if (isset($config['installation-source'])) {
         $package->setInstallationSource($config['installation-source']);
     }
     if (isset($config['source'])) {
         if (!isset($config['source']['type']) || !isset($config['source']['url'])) {
             throw new \UnexpectedValueException(sprintf("package source should be specified as {\"type\": ..., \"url\": ...},\n%s given", json_encode($config['source'])));
         }
         $package->setSourceType($config['source']['type']);
         $package->setSourceUrl($config['source']['url']);
         $package->setSourceReference($config['source']['reference']);
     } elseif ($package->isDev()) {
         throw new \UnexpectedValueException('Dev package ' . $package . ' must have a source specified');
     }
     if (isset($config['dist'])) {
         if (!isset($config['dist']['type']) || !isset($config['dist']['url'])) {
             throw new \UnexpectedValueException(sprintf("package dist should be specified as " . "{\"type\": ..., \"url\": ..., \"reference\": ..., \"shasum\": ...},\n%s given", json_encode($config['dist'])));
         }
         $package->setDistType($config['dist']['type']);
         $package->setDistUrl($config['dist']['url']);
         $package->setDistReference(isset($config['dist']['reference']) ? $config['dist']['reference'] : null);
         $package->setDistSha1Checksum(isset($config['dist']['shasum']) ? $config['dist']['shasum'] : null);
     }
     foreach ($this->supportedLinkTypes as $type => $description) {
         if (isset($config[$type])) {
             $method = 'set' . ucfirst($description);
             $package->{$method}($this->loadLinksFromConfig($package, $description, $config[$type]));
         }
     }
     if (isset($config['autoload'])) {
         $package->setAutoload($config['autoload']);
     }
     return $package;
 }
Exemple #4
0
 /**
  * Builds MemoryPackages from PEAR package definition data.
  *
  * @param  ChannelInfo   $channelInfo
  * @return MemoryPackage
  */
 private function buildComposerPackages(ChannelInfo $channelInfo, VersionParser $versionParser)
 {
     $result = array();
     foreach ($channelInfo->getPackages() as $packageDefinition) {
         foreach ($packageDefinition->getReleases() as $version => $releaseInfo) {
             try {
                 $normalizedVersion = $versionParser->normalize($version);
             } catch (\UnexpectedValueException $e) {
                 if ($this->io->isVerbose()) {
                     $this->io->write('Could not load ' . $packageDefinition->getPackageName() . ' ' . $version . ': ' . $e->getMessage());
                 }
                 continue;
             }
             $composerPackageName = $this->buildComposerPackageName($packageDefinition->getChannelName(), $packageDefinition->getPackageName());
             // distribution url must be read from /r/{packageName}/{version}.xml::/r/g:text()
             // but this location is 'de-facto' standard
             $distUrl = "http://{$packageDefinition->getChannelName()}/get/{$packageDefinition->getPackageName()}-{$version}.tgz";
             $requires = array();
             $suggests = array();
             $conflicts = array();
             $replaces = array();
             // alias package only when its channel matches repository channel,
             // cause we've know only repository channel alias
             if ($channelInfo->getName() == $packageDefinition->getChannelName()) {
                 $composerPackageAlias = $this->buildComposerPackageName($channelInfo->getAlias(), $packageDefinition->getPackageName());
                 $aliasConstraint = new VersionConstraint('==', $normalizedVersion);
                 $replaces[] = new Link($composerPackageName, $composerPackageAlias, $aliasConstraint, 'replaces', (string) $aliasConstraint);
             }
             // alias package with user-specified prefix. it makes private pear channels looks like composer's.
             if (!empty($this->vendorAlias)) {
                 $composerPackageAlias = "{$this->vendorAlias}/{$packageDefinition->getPackageName()}";
                 $aliasConstraint = new VersionConstraint('==', $normalizedVersion);
                 $replaces[] = new Link($composerPackageName, $composerPackageAlias, $aliasConstraint, 'replaces', (string) $aliasConstraint);
             }
             foreach ($releaseInfo->getDependencyInfo()->getRequires() as $dependencyConstraint) {
                 $dependencyPackageName = $this->buildComposerPackageName($dependencyConstraint->getChannelName(), $dependencyConstraint->getPackageName());
                 $constraint = $versionParser->parseConstraints($dependencyConstraint->getConstraint());
                 $link = new Link($composerPackageName, $dependencyPackageName, $constraint, $dependencyConstraint->getType(), $dependencyConstraint->getConstraint());
                 switch ($dependencyConstraint->getType()) {
                     case 'required':
                         $requires[] = $link;
                         break;
                     case 'conflicts':
                         $conflicts[] = $link;
                         break;
                     case 'replaces':
                         $replaces[] = $link;
                         break;
                 }
             }
             foreach ($releaseInfo->getDependencyInfo()->getOptionals() as $group => $dependencyConstraints) {
                 foreach ($dependencyConstraints as $dependencyConstraint) {
                     $dependencyPackageName = $this->buildComposerPackageName($dependencyConstraint->getChannelName(), $dependencyConstraint->getPackageName());
                     $suggests[$group . '-' . $dependencyPackageName] = $dependencyConstraint->getConstraint();
                 }
             }
             $package = new MemoryPackage($composerPackageName, $normalizedVersion, $version);
             $package->setType('pear-library');
             $package->setDescription($packageDefinition->getDescription());
             $package->setDistType('file');
             $package->setDistUrl($distUrl);
             $package->setAutoload(array('classmap' => array('')));
             $package->setIncludePaths(array('/'));
             $package->setRequires($requires);
             $package->setConflicts($conflicts);
             $package->setSuggests($suggests);
             $package->setReplaces($replaces);
             $result[] = $package;
         }
     }
     return $result;
 }