123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- <?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\Package\Loader;
- use Composer\Package;
- use Composer\Package\Version\VersionParser;
- /**
- * @author Konstantin Kudryashiv <ever.zet@gmail.com>
- * @author Jordi Boggiano <j.boggiano@seld.be>
- */
- class ArrayLoader implements LoaderInterface
- {
- protected $versionParser;
- public function __construct(VersionParser $parser = null)
- {
- if (!$parser) {
- $parser = new VersionParser;
- }
- $this->versionParser = $parser;
- }
- public function load(array $config, $class = 'Composer\Package\CompletePackage')
- {
- 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 $class($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['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);
- }
- if ($aliasNormalized = $this->getBranchAlias($config)) {
- $package->setAlias($aliasNormalized);
- $package->setPrettyAlias(preg_replace('{(\.9{7})+}', '.x', $aliasNormalized));
- }
- 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 (!empty($config['time'])) {
- try {
- $date = new \DateTime($config['time']);
- $date->setTimezone(new \DateTimeZone('UTC'));
- $package->setReleaseDate($date);
- } catch (\Exception $e) {
- }
- }
- if ($package instanceof Package\CompletePackageInterface) {
- 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['authors']) && is_array($config['authors'])) {
- $package->setAuthors($config['authors']);
- }
- if (isset($config['support'])) {
- $package->setSupport($config['support']);
- }
- }
- return $package;
- }
- /**
- * Retrieves a branch alias (dev-master => 1.0.x-dev for example) if it exists
- *
- * @param array $config the entire package config
- * @return string|null normalized version of the branch alias or null if there is none
- */
- public function getBranchAlias(array $config)
- {
- if ('dev-' !== substr($config['version'], 0, 4)
- || !isset($config['extra']['branch-alias'])
- || !is_array($config['extra']['branch-alias'])
- ) {
- return;
- }
- 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($config['version']) !== strtolower($sourceBranch)) {
- continue;
- }
- return $validatedTargetBranch;
- }
- }
- private function loadLinksFromConfig($package, $description, array $linksSpecs)
- {
- $links = array();
- foreach ($linksSpecs as $packageName => $constraint) {
- if ('self.version' === $constraint) {
- $parsedConstraint = $this->versionParser->parseConstraints($package->getPrettyVersion());
- } else {
- $parsedConstraint = $this->versionParser->parseConstraints($constraint);
- }
- $links[] = new Package\Link($package->getName(), $packageName, $parsedConstraint, $description, $constraint);
- }
- return $links;
- }
- }
|