Composer\Satis\PackageSelection\PackageSelection::select PHP Method

select() public method

Sets the list of packages to build.
public select ( Composer\Composer $composer, boolean $verbose ) : Composer\Package\PackageInterface[]
$composer Composer\Composer The Composer instance
$verbose boolean Output infos if true
return Composer\Package\PackageInterface[]
    public function select(Composer $composer, $verbose)
    {
        // run over all packages and store matching ones
        $this->output->writeln('<info>Scanning packages</info>');
        $repos = $composer->getRepositoryManager()->getRepositories();
        $pool = new Pool($this->minimumStability);
        if ($this->hasRepositoryFilter()) {
            $repos = $this->filterRepositories($repos);
        }
        foreach ($repos as $repo) {
            try {
                $pool->addRepository($repo);
            } catch (\Exception $exception) {
                if (!$this->skipErrors) {
                    throw $exception;
                }
                $this->output->writeln(sprintf("<error>Skipping Exception '%s'.</error>", $exception->getMessage()));
            }
        }
        if ($this->hasRepositoryFilter()) {
            if (count($repos) === 0) {
                throw new \InvalidArgumentException(sprintf('Specified repository url "%s" does not exist.', $this->repositoryFilter));
            } elseif (count($repos) > 1) {
                throw new \InvalidArgumentException(sprintf('Found more than one repository for url "%s".', $this->repositoryFilter));
            }
        }
        $links = $this->requireAll ? $this->getAllLinks($repos, $this->minimumStability, $verbose) : $this->getFilteredLinks($composer);
        // process links if any
        $depsLinks = [];
        $i = 0;
        while (isset($links[$i])) {
            $link = $links[$i];
            ++$i;
            $name = $link->getTarget();
            $matches = $pool->whatProvides($name, $link->getConstraint(), true);
            foreach ($matches as $index => $package) {
                // skip aliases
                if ($package instanceof AliasPackage) {
                    $package = $package->getAliasOf();
                }
                // add matching package if not yet selected
                if (!isset($this->selected[$package->getUniqueName()])) {
                    if ($verbose) {
                        $this->output->writeln('Selected ' . $package->getPrettyName() . ' (' . $package->getPrettyVersion() . ')');
                    }
                    $this->selected[$package->getUniqueName()] = $package;
                    if (!$this->requireAll) {
                        $required = $this->getRequired($package);
                        // append non-platform dependencies
                        foreach ($required as $dependencyLink) {
                            $target = $dependencyLink->getTarget();
                            if (!preg_match(PlatformRepository::PLATFORM_PACKAGE_REGEX, $target)) {
                                $linkId = $target . ' ' . $dependencyLink->getConstraint();
                                // prevent loading multiple times the same link
                                if (!isset($depsLinks[$linkId])) {
                                    $links[] = $dependencyLink;
                                    $depsLinks[$linkId] = true;
                                }
                            }
                        }
                    }
                }
            }
            if (!$matches) {
                $this->output->writeln('<error>The ' . $name . ' ' . $link->getPrettyConstraint() . ' requirement did not match any package</error>');
            }
        }
        $this->setSelectedAsAbandoned();
        ksort($this->selected, SORT_STRING);
        return $this->selected;
    }

Usage Example

Exemplo n.º 1
0
 /**
  * @param InputInterface $input The input instance
  * @param OutputInterface $output The output instance
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $verbose = $input->getOption('verbose');
     $configFile = $input->getArgument('file');
     $packagesFilter = $input->getArgument('packages');
     $repositoryUrl = $input->getOption('repository-url');
     $skipErrors = (bool) $input->getOption('skip-errors');
     if ($repositoryUrl !== null && count($packagesFilter) > 0) {
         throw new \InvalidArgumentException('The arguments "package" and "repository-url" can not be used together.');
     }
     // load auth.json authentication information and pass it to the io interface
     $io = $this->getIO();
     $io->loadConfiguration($this->getConfiguration());
     if (preg_match('{^https?://}i', $configFile)) {
         $rfs = new RemoteFilesystem($io);
         $contents = $rfs->getContents(parse_url($configFile, PHP_URL_HOST), $configFile, false);
         $config = JsonFile::parseJson($contents, $configFile);
     } else {
         $file = new JsonFile($configFile);
         if (!$file->exists()) {
             $output->writeln('<error>File not found: ' . $configFile . '</error>');
             return 1;
         }
         $config = $file->read();
     }
     // disable packagist by default
     unset(Config::$defaultRepositories['packagist']);
     if (!($outputDir = $input->getArgument('output-dir'))) {
         $outputDir = isset($config['output-dir']) ? $config['output-dir'] : null;
     }
     if (null === $outputDir) {
         throw new \InvalidArgumentException('The output dir must be specified as second argument or be configured inside ' . $input->getArgument('file'));
     }
     $composer = $this->getApplication()->getComposer(true, $config);
     $packageSelection = new PackageSelection($output, $outputDir, $config, $skipErrors);
     if ($repositoryUrl !== null) {
         $packageSelection->setRepositoryFilter($repositoryUrl);
     } else {
         $packageSelection->setPackagesFilter($packagesFilter);
     }
     $packages = $packageSelection->select($composer, $verbose);
     if (isset($config['archive']['directory'])) {
         $downloads = new ArchiveBuilder($output, $outputDir, $config, $skipErrors);
         $downloads->setComposer($composer);
         $downloads->dump($packages);
     }
     if ($packageSelection->hasFilterForPackages()) {
         // in case of an active package filter we need to load the dumped packages.json and merge the
         // updated packages in
         $oldPackages = $packageSelection->load();
         $packages += $oldPackages;
         ksort($packages);
     }
     $packagesBuilder = new PackagesBuilder($output, $outputDir, $config, $skipErrors);
     $packagesBuilder->dump($packages);
     if ($htmlView = !$input->getOption('no-html-output')) {
         $htmlView = !isset($config['output-html']) || $config['output-html'];
     }
     if ($htmlView) {
         $web = new WebBuilder($output, $outputDir, $config, $skipErrors);
         $web->setRootPackage($composer->getPackage());
         $web->dump($packages);
     }
 }