| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377 |
- <?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\AliasPackage;
- use Composer\Package\Link;
- use Composer\Package\RootAliasPackage;
- use Composer\Package\RootPackageInterface;
- 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;
- protected $loadOptions;
- public function __construct(VersionParser $parser = null, $loadOptions = false)
- {
- if (!$parser) {
- $parser = new VersionParser;
- }
- $this->versionParser = $parser;
- $this->loadOptions = $loadOptions;
- }
- public function load(array $config, $class = 'Composer\Package\CompletePackage')
- {
- $package = $this->createObject($config, $class);
- foreach (Package\BasePackage::$supportedLinkTypes as $type => $opts) {
- if (isset($config[$type])) {
- $method = 'set'.ucfirst($opts['method']);
- $package->{$method}(
- $this->parseLinks(
- $package->getName(),
- $package->getPrettyVersion(),
- $opts['description'],
- $config[$type]
- )
- );
- }
- }
- $package = $this->configureObject($package, $config);
- return $package;
- }
- public function loadPackages(array $versions, $class)
- {
- static $uniqKeys = array('version', 'version_normalized', 'source', 'dist', 'time');
- $packages = array();
- $linkCache = array();
- foreach ($versions as $version) {
- if (isset($version['versions'])) {
- $baseVersion = $version;
- foreach ($uniqKeys as $key) {
- unset($baseVersion[$key.'s']);
- }
- foreach ($version['versions'] as $index => $dummy) {
- $unpackedVersion = $baseVersion;
- foreach ($uniqKeys as $key) {
- $unpackedVersion[$key] = $version[$key.'s'][$index];
- }
- $package = $this->createObject($unpackedVersion, $class);
- $this->configureCachedLinks($linkCache, $package, $unpackedVersion);
- $package = $this->configureObject($package, $unpackedVersion);
- $packages[] = $package;
- }
- } else {
- $package = $this->createObject($version, $class);
- $this->configureCachedLinks($linkCache, $package, $version);
- $package = $this->configureObject($package, $version);
- $packages[] = $package;
- }
- }
- return $packages;
- }
- private function createObject(array $config, $class)
- {
- 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']);
- }
- return new $class($config['name'], $version, $config['version']);
- }
- private function configureObject($package, array $config)
- {
- $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'])) {
- foreach ((array) $config['bin'] as $key => $bin) {
- $config['bin'][$key] = ltrim($bin, '/');
- }
- $package->setBinaries((array) $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']) || !isset($config['source']['reference'])) {
- throw new \UnexpectedValueException(sprintf(
- "Package %s's source key should be specified as {\"type\": ..., \"url\": ..., \"reference\": ...},\n%s given.",
- $config['name'],
- json_encode($config['source'])
- ));
- }
- $package->setSourceType($config['source']['type']);
- $package->setSourceUrl($config['source']['url']);
- $package->setSourceReference(isset($config['source']['reference']) ? $config['source']['reference'] : null);
- if (isset($config['source']['mirrors'])) {
- $package->setSourceMirrors($config['source']['mirrors']);
- }
- }
- if (isset($config['dist'])) {
- if (!isset($config['dist']['type'])
- || !isset($config['dist']['url'])) {
- throw new \UnexpectedValueException(sprintf(
- "Package %s's dist key should be specified as ".
- "{\"type\": ..., \"url\": ..., \"reference\": ..., \"shasum\": ...},\n%s given.",
- $config['name'],
- 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 (isset($config['dist']['mirrors'])) {
- $package->setDistMirrors($config['dist']['mirrors']);
- }
- }
- 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['autoload-dev'])) {
- $package->setDevAutoload($config['autoload-dev']);
- }
- if (isset($config['include-path'])) {
- $package->setIncludePaths($config['include-path']);
- }
- if (!empty($config['time'])) {
- $time = preg_match('/^\d++$/D', $config['time']) ? '@'.$config['time'] : $config['time'];
- try {
- $date = new \DateTime($time, new \DateTimeZone('UTC'));
- $package->setReleaseDate($date);
- } catch (\Exception $e) {
- }
- }
- if (!empty($config['notification-url'])) {
- $package->setNotificationUrl($config['notification-url']);
- }
- if (!empty($config['archive']['exclude'])) {
- $package->setArchiveExcludes($config['archive']['exclude']);
- }
- if ($package instanceof Package\CompletePackageInterface) {
- if (isset($config['scripts']) && is_array($config['scripts'])) {
- foreach ($config['scripts'] as $event => $listeners) {
- $config['scripts'][$event] = (array) $listeners;
- }
- if (isset($config['scripts']['composer'])) {
- trigger_error('The `composer` script name is reserved for internal use, please avoid defining it', E_USER_DEPRECATED);
- }
- $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']);
- }
- if (isset($config['abandoned'])) {
- $package->setAbandoned($config['abandoned']);
- }
- }
- if ($this->loadOptions && isset($config['transport-options'])) {
- $package->setTransportOptions($config['transport-options']);
- }
- if ($aliasNormalized = $this->getBranchAlias($config)) {
- if ($package instanceof RootPackageInterface) {
- return new RootAliasPackage($package, $aliasNormalized, preg_replace('{(\.9{7})+}', '.x', $aliasNormalized));
- }
- return new AliasPackage($package, $aliasNormalized, preg_replace('{(\.9{7})+}', '.x', $aliasNormalized));
- }
- return $package;
- }
- private function configureCachedLinks(&$linkCache, $package, array $config)
- {
- $name = $package->getName();
- $prettyVersion = $package->getPrettyVersion();
- foreach (Package\BasePackage::$supportedLinkTypes as $type => $opts) {
- if (isset($config[$type])) {
- $method = 'set'.ucfirst($opts['method']);
- $links = array();
- foreach ($config[$type] as $prettyTarget => $constraint) {
- $target = strtolower($prettyTarget);
- if ($constraint === 'self.version') {
- $links[$target] = $this->createLink($name, $prettyVersion, $opts['description'], $target, $constraint);
- } else {
- if (!isset($linkCache[$name][$type][$target][$constraint])) {
- $linkCache[$name][$type][$target][$constraint] = array($target, $this->createLink($name, $prettyVersion, $opts['description'], $target, $constraint));
- }
- list($target, $link) = $linkCache[$name][$type][$target][$constraint];
- $links[$target] = $link;
- }
- }
- $package->{$method}($links);
- }
- }
- }
- /**
- * @param string $source source package name
- * @param string $sourceVersion source package version (pretty version ideally)
- * @param string $description link description (e.g. requires, replaces, ..)
- * @param array $links array of package name => constraint mappings
- * @return Link[]
- */
- public function parseLinks($source, $sourceVersion, $description, $links)
- {
- $res = array();
- foreach ($links as $target => $constraint) {
- $res[strtolower($target)] = $this->createLink($source, $sourceVersion, $description, $target, $constraint);
- }
- return $res;
- }
- private function createLink($source, $sourceVersion, $description, $target, $prettyConstraint)
- {
- if (!is_string($prettyConstraint)) {
- throw new \UnexpectedValueException('Link constraint in '.$source.' '.$description.' > '.$target.' should be a string, got '.gettype($prettyConstraint) . ' (' . var_export($prettyConstraint, true) . ')');
- }
- if ('self.version' === $prettyConstraint) {
- $parsedConstraint = $this->versionParser->parseConstraints($sourceVersion);
- } else {
- $parsedConstraint = $this->versionParser->parseConstraints($prettyConstraint);
- }
- return new Link($source, $target, $parsedConstraint, $description, $prettyConstraint);
- }
- /**
- * 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) && '-dev' !== substr($config['version'], -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;
- }
- // If using numeric aliases ensure the alias is a valid subversion
- if (($sourcePrefix = $this->versionParser->parseNumericAliasPrefix($sourceBranch))
- && ($targetPrefix = $this->versionParser->parseNumericAliasPrefix($targetBranch))
- && (stripos($targetPrefix, $sourcePrefix) !== 0)
- ) {
- continue;
- }
- return $validatedTargetBranch;
- }
- }
- }
|