Exemple #1
0
 protected function selectPackage(IOInterface $io, $packageName, $version = null)
 {
     $io->writeError('<info>Searching for the specified package.</info>');
     if ($composer = $this->getComposer(false)) {
         $localRepo = $composer->getRepositoryManager()->getLocalRepository();
         $repos = new CompositeRepository(array_merge(array($localRepo), $composer->getRepositoryManager()->getRepositories()));
     } else {
         $defaultRepos = Factory::createDefaultRepositories($this->getIO());
         $io->writeError('No composer.json found in the current directory, searching packages from ' . implode(', ', array_keys($defaultRepos)));
         $repos = new CompositeRepository($defaultRepos);
     }
     $pool = new Pool();
     $pool->addRepository($repos);
     $parser = new VersionParser();
     $constraint = $version ? $parser->parseConstraints($version) : null;
     $packages = $pool->whatProvides($packageName, $constraint, true);
     if (count($packages) > 1) {
         $package = reset($packages);
         $io->writeError('<info>Found multiple matches, selected ' . $package->getPrettyString() . '.</info>');
         $io->writeError('Alternatives were ' . implode(', ', array_map(function ($p) {
             return $p->getPrettyString();
         }, $packages)) . '.');
         $io->writeError('<comment>Please use a more specific constraint to pick a different package.</comment>');
     } elseif ($packages) {
         $package = reset($packages);
         $io->writeError('<info>Found an exact match ' . $package->getPrettyString() . '.</info>');
     } else {
         $io->writeError('<error>Could not find a package matching ' . $packageName . '.</error>');
         return false;
     }
     return $package;
 }
Exemple #2
0
 /**
  * Search for packages.
  *
  * @param array   $packages Indexed array of package names to search for
  * @param boolean $onlyname True for name only search, false for full text
  *
  * @throws PackageManagerException
  *
  * @return array List of matching packages
  */
 public function execute($packages, $onlyname = true)
 {
     /** @var $composer \Composer\Composer */
     $composer = $this->getComposer();
     $io = $this->getIO();
     $platformRepo = new PlatformRepository();
     if ($composer) {
         $localRepo = $composer->getRepositoryManager()->getLocalRepository();
         $installedRepo = new CompositeRepository([$localRepo, $platformRepo]);
         $repos = new CompositeRepository(array_merge([$installedRepo], $composer->getRepositoryManager()->getRepositories()));
     } else {
         $defaultRepos = Factory::createDefaultRepositories($io);
         //No composer.json found in the current directory, showing packages from local repo
         $installedRepo = $platformRepo;
         $repos = new CompositeRepository(array_merge([$installedRepo], $defaultRepos));
     }
     $flags = $onlyname ? RepositoryInterface::SEARCH_NAME : RepositoryInterface::SEARCH_FULLTEXT;
     try {
         return $repos->search(implode(' ', $packages), $flags);
     } catch (\Exception $e) {
         $msg = __CLASS__ . '::' . __FUNCTION__ . ' recieved an error from Composer: ' . $e->getMessage() . ' in ' . $e->getFile() . '::' . $e->getLine();
         $this->app['logger.system']->critical($msg, ['event' => 'exception', 'exception' => $e]);
         throw new PackageManagerException($e->getMessage(), $e->getCode(), $e);
     }
 }
Exemple #3
0
 /**
  * 
  * @return ComposerRepository
  */
 private function getPackagistRepository()
 {
     if (!$this->repos) {
         $this->repos = Factory::createDefaultRepositories($this->getIO());
     }
     return $this->repos['packagist'];
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     // init repos
     $platformRepo = new PlatformRepository();
     if ($composer = $this->getComposer(false)) {
         $localRepo = $composer->getRepositoryManager()->getLocalRepository();
         $installedRepo = new CompositeRepository(array($localRepo, $platformRepo));
         $repos = new CompositeRepository(array_merge(array($installedRepo), $composer->getRepositoryManager()->getRepositories()));
     } else {
         $defaultRepos = Factory::createDefaultRepositories($this->getIO());
         $output->writeln('No composer.json found in the current directory, showing packages from ' . implode(', ', array_keys($defaultRepos)));
         $installedRepo = $platformRepo;
         $repos = new CompositeRepository(array_merge(array($installedRepo), $defaultRepos));
     }
     if ($composer) {
         $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'search', $input, $output);
         $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);
     }
     $onlyName = $input->getOption('only-name');
     $flags = $onlyName ? RepositoryInterface::SEARCH_NAME : RepositoryInterface::SEARCH_FULLTEXT;
     $results = $repos->search(implode(' ', $input->getArgument('tokens')), $flags);
     foreach ($results as $result) {
         $output->writeln($result['name'] . (isset($result['description']) ? ' ' . $result['description'] : ''));
     }
 }
 public function installProject(IOInterface $io, $packageName, $directory = null, $version = null, $preferSource = false, $installDevPackages = false, $repositoryUrl = null, $disableCustomInstallers = false, $noScripts = false)
 {
     $dm = $this->createDownloadManager($io);
     if ($preferSource) {
         $dm->setPreferSource(true);
     }
     $config = Factory::createConfig();
     if (null === $repositoryUrl) {
         $sourceRepo = new CompositeRepository(Factory::createDefaultRepositories($io, $config));
     } elseif ("json" === pathinfo($repositoryUrl, PATHINFO_EXTENSION)) {
         $sourceRepo = new FilesystemRepository(new JsonFile($repositoryUrl, new RemoteFilesystem($io)));
     } elseif (0 === strpos($repositoryUrl, 'http')) {
         $sourceRepo = new ComposerRepository(array('url' => $repositoryUrl), $io, $config);
     } else {
         throw new \InvalidArgumentException("Invalid repository url given. Has to be a .json file or an http url.");
     }
     $candidates = $sourceRepo->findPackages($packageName, $version);
     if (!$candidates) {
         throw new \InvalidArgumentException("Could not find package {$packageName}" . ($version ? " with version {$version}." : ''));
     }
     if (null === $directory) {
         $parts = explode("/", $packageName, 2);
         $directory = getcwd() . DIRECTORY_SEPARATOR . array_pop($parts);
     }
     // select highest version if we have many
     $package = $candidates[0];
     foreach ($candidates as $candidate) {
         if (version_compare($package->getVersion(), $candidate->getVersion(), '<')) {
             $package = $candidate;
         }
     }
     $io->write('<info>Installing ' . $package->getName() . ' (' . VersionParser::formatVersion($package, false) . ')</info>', true);
     if ($disableCustomInstallers) {
         $io->write('<info>Custom installers have been disabled.</info>');
     }
     if (0 === strpos($package->getPrettyVersion(), 'dev-') && in_array($package->getSourceType(), array('git', 'hg'))) {
         $package->setSourceReference(substr($package->getPrettyVersion(), 4));
     }
     $projectInstaller = new ProjectInstaller($directory, $dm);
     $projectInstaller->install(new InstalledFilesystemRepository(new JsonFile('php://memory')), $package);
     if ($package->getRepository() instanceof NotifiableRepositoryInterface) {
         $package->getRepository()->notifyInstall($package);
     }
     $io->write('<info>Created project in ' . $directory . '</info>', true);
     chdir($directory);
     putenv('COMPOSER_ROOT_VERSION=' . $package->getPrettyVersion());
     $composer = Factory::create($io);
     $installer = Installer::create($io, $composer);
     $installer->setPreferSource($preferSource)->setDevMode($installDevPackages)->setRunScripts(!$noScripts);
     if ($disableCustomInstallers) {
         $installer->disableCustomInstallers();
     }
     $installer->run();
 }
 public function load(array $config, $class = 'Composer\\Package\\RootPackage')
 {
     if (!isset($config['name'])) {
         $config['name'] = '__root__';
     }
     if (!isset($config['version'])) {
         if (getenv('COMPOSER_ROOT_VERSION')) {
             $version = getenv('COMPOSER_ROOT_VERSION');
         } else {
             $version = $this->guessVersion($config);
         }
         if (!$version) {
             $version = '1.0.0';
         }
         $config['version'] = $version;
     }
     $realPackage = $package = parent::load($config, $class);
     if ($realPackage instanceof AliasPackage) {
         $realPackage = $package->getAliasOf();
     }
     if (isset($config['minimum-stability'])) {
         $realPackage->setMinimumStability(VersionParser::normalizeStability($config['minimum-stability']));
     }
     $aliases = array();
     $stabilityFlags = array();
     $references = array();
     foreach (array('require', 'require-dev') as $linkType) {
         if (isset($config[$linkType])) {
             $linkInfo = BasePackage::$supportedLinkTypes[$linkType];
             $method = 'get' . ucfirst($linkInfo['method']);
             $links = array();
             foreach ($realPackage->{$method}() as $link) {
                 $links[$link->getTarget()] = $link->getConstraint()->getPrettyString();
             }
             $aliases = $this->extractAliases($links, $aliases);
             $stabilityFlags = $this->extractStabilityFlags($links, $stabilityFlags, $realPackage->getMinimumStability());
             $references = $this->extractReferences($links, $references);
         }
     }
     $realPackage->setAliases($aliases);
     $realPackage->setStabilityFlags($stabilityFlags);
     $realPackage->setReferences($references);
     if (isset($config['prefer-stable'])) {
         $realPackage->setPreferStable((bool) $config['prefer-stable']);
     }
     $repos = Factory::createDefaultRepositories(null, $this->config, $this->manager);
     foreach ($repos as $repo) {
         $this->manager->addRepository($repo);
     }
     $realPackage->setRepositories($this->config->getRepositories());
     return $package;
 }
Exemple #7
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     // init repos
     $platformRepo = new PlatformRepository();
     if ($composer = $this->getComposer(false)) {
         $localRepo = $composer->getRepositoryManager()->getLocalRepository();
         $installedRepo = new CompositeRepository(array($localRepo, $platformRepo));
         $repos = new CompositeRepository(array_merge(array($installedRepo), $composer->getRepositoryManager()->getRepositories()));
     } else {
         $defaultRepos = Factory::createDefaultRepositories($this->getIO());
         $output->writeln('No composer.json found in the current directory, showing packages from ' . implode(', ', array_keys($defaultRepos)));
         $installedRepo = $platformRepo;
         $repos = new CompositeRepository(array_merge(array($installedRepo), $defaultRepos));
     }
     $tokens = $input->getArgument('tokens');
     $packages = array();
     $maxPackageLength = 0;
     foreach ($repos->getPackages() as $package) {
         if ($package instanceof AliasPackage || isset($packages[$package->getName()])) {
             continue;
         }
         foreach ($tokens as $token) {
             if (!($score = $this->matchPackage($package, $token))) {
                 continue;
             }
             if (false !== ($pos = stripos($package->getName(), $token))) {
                 $name = substr($package->getPrettyName(), 0, $pos) . '<highlight>' . substr($package->getPrettyName(), $pos, strlen($token)) . '</highlight>' . substr($package->getPrettyName(), $pos + strlen($token));
             } else {
                 $name = $package->getPrettyName();
             }
             $description = strtok($package->getDescription(), "\r\n");
             if (false !== ($pos = stripos($description, $token))) {
                 $description = substr($description, 0, $pos) . '<highlight>' . substr($description, $pos, strlen($token)) . '</highlight>' . substr($description, $pos + strlen($token));
             }
             $packages[$package->getName()] = array('name' => $name, 'description' => $description, 'length' => $length = strlen($package->getPrettyName()), 'score' => $score);
             $maxPackageLength = max($maxPackageLength, $length);
             continue 2;
         }
     }
     usort($packages, function ($a, $b) {
         if ($a['score'] === $b['score']) {
             return 0;
         }
         return $a['score'] > $b['score'] ? -1 : 1;
     });
     foreach ($packages as $details) {
         $extraSpaces = $maxPackageLength - $details['length'];
         $output->writeln($details['name'] . str_repeat(' ', $extraSpaces) . ' <comment>:</comment> ' . $details['description']);
     }
 }
Exemple #8
0
 public function load($config)
 {
     if (!isset($config['name'])) {
         $config['name'] = '__root__';
     }
     if (!isset($config['version'])) {
         // override with env var if available
         if (getenv('COMPOSER_ROOT_VERSION')) {
             $version = getenv('COMPOSER_ROOT_VERSION');
         } else {
             $version = $this->guessVersion($config);
         }
         if (!$version) {
             $version = '1.0.0';
         }
         $config['version'] = $version;
     } else {
         $version = $config['version'];
     }
     $package = parent::load($config);
     $aliases = array();
     $stabilityFlags = array();
     $references = array();
     foreach (array('require', 'require-dev') as $linkType) {
         if (isset($config[$linkType])) {
             $aliases = $this->extractAliases($config[$linkType], $aliases);
             $stabilityFlags = $this->extractStabilityFlags($config[$linkType], $stabilityFlags);
             $references = $this->extractReferences($config[$linkType], $references);
         }
     }
     $package->setAliases($aliases);
     $package->setStabilityFlags($stabilityFlags);
     $package->setReferences($references);
     if (isset($config['minimum-stability'])) {
         $package->setMinimumStability(VersionParser::normalizeStability($config['minimum-stability']));
     }
     $repos = Factory::createDefaultRepositories(null, $this->config, $this->manager);
     foreach ($repos as $repo) {
         $this->manager->addRepository($repo);
     }
     $package->setRepositories($this->config->getRepositories());
     return $package;
 }
Exemple #9
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     // init repos
     $platformRepo = new PlatformRepository();
     if ($composer = $this->getComposer(false)) {
         $localRepo = $composer->getRepositoryManager()->getLocalRepository();
         $installedRepo = new CompositeRepository(array($localRepo, $platformRepo));
         $repos = new CompositeRepository(array_merge(array($installedRepo), $composer->getRepositoryManager()->getRepositories()));
     } else {
         $defaultRepos = Factory::createDefaultRepositories($this->getIO());
         $output->writeln('No composer.json found in the current directory, showing packages from ' . implode(', ', array_keys($defaultRepos)));
         $installedRepo = $platformRepo;
         $repos = new CompositeRepository(array_merge(array($installedRepo), $defaultRepos));
     }
     $this->tokens = $input->getArgument('tokens');
     $this->output = $output;
     $repos->filterPackages(array($this, 'processPackage'), 'Composer\\Package\\CompletePackage');
     foreach ($this->lowMatches as $details) {
         $output->writeln($details['name'] . '<comment>:</comment> ' . $details['description']);
     }
 }
 protected function installRootPackage(IOInterface $io, $config, $packageName, $directory = null, $packageVersion = null, $stability = 'stable', $preferSource = false, $preferDist = false, $installDevPackages = false, $repositoryUrl = null, $disablePlugins = false, $noScripts = false, $keepVcs = false, $noProgress = false)
 {
     if (null === $repositoryUrl) {
         $sourceRepo = new CompositeRepository(Factory::createDefaultRepositories($io, $config));
     } elseif ("json" === pathinfo($repositoryUrl, PATHINFO_EXTENSION)) {
         $sourceRepo = new FilesystemRepository(new JsonFile($repositoryUrl, new RemoteFilesystem($io)));
     } elseif (0 === strpos($repositoryUrl, 'http')) {
         $sourceRepo = new ComposerRepository(array('url' => $repositoryUrl), $io, $config);
     } else {
         throw new \InvalidArgumentException("Invalid repository url given. Has to be a .json file or an http url.");
     }
     $parser = new VersionParser();
     $candidates = array();
     $requirements = $parser->parseNameVersionPairs(array($packageName));
     $name = strtolower($requirements[0]['name']);
     if (!$packageVersion && isset($requirements[0]['version'])) {
         $packageVersion = $requirements[0]['version'];
     }
     if (null === $stability) {
         if (preg_match('{^[^,\\s]*?@(' . implode('|', array_keys(BasePackage::$stabilities)) . ')$}i', $packageVersion, $match)) {
             $stability = $match[1];
         } else {
             $stability = VersionParser::parseStability($packageVersion);
         }
     }
     $stability = VersionParser::normalizeStability($stability);
     if (!isset(BasePackage::$stabilities[$stability])) {
         throw new \InvalidArgumentException('Invalid stability provided (' . $stability . '), must be one of: ' . implode(', ', array_keys(BasePackage::$stabilities)));
     }
     $pool = new Pool($stability);
     $pool->addRepository($sourceRepo);
     $constraint = $packageVersion ? $parser->parseConstraints($packageVersion) : null;
     $candidates = $pool->whatProvides($name, $constraint);
     foreach ($candidates as $key => $candidate) {
         if ($candidate->getName() !== $name) {
             unset($candidates[$key]);
         }
     }
     if (!$candidates) {
         throw new \InvalidArgumentException("Could not find package {$name}" . ($packageVersion ? " with version {$packageVersion}." : " with stability {$stability}."));
     }
     if (null === $directory) {
         $parts = explode("/", $name, 2);
         $directory = getcwd() . DIRECTORY_SEPARATOR . array_pop($parts);
     }
     $package = reset($candidates);
     foreach ($candidates as $candidate) {
         if (version_compare($package->getVersion(), $candidate->getVersion(), '<')) {
             $package = $candidate;
         }
     }
     unset($candidates);
     $io->write('<info>Installing ' . $package->getName() . ' (' . VersionParser::formatVersion($package, false) . ')</info>');
     if ($disablePlugins) {
         $io->write('<info>Plugins have been disabled.</info>');
     }
     if (0 === strpos($package->getPrettyVersion(), 'dev-') && in_array($package->getSourceType(), array('git', 'hg'))) {
         $package->setSourceReference(substr($package->getPrettyVersion(), 4));
     }
     $dm = $this->createDownloadManager($io, $config);
     $dm->setPreferSource($preferSource)->setPreferDist($preferDist)->setOutputProgress(!$noProgress);
     $projectInstaller = new ProjectInstaller($directory, $dm);
     $im = $this->createInstallationManager();
     $im->addInstaller($projectInstaller);
     $im->install(new InstalledFilesystemRepository(new JsonFile('php://memory')), new InstallOperation($package));
     $im->notifyInstalls();
     $installedFromVcs = 'source' === $package->getInstallationSource();
     $io->write('<info>Created project in ' . $directory . '</info>');
     chdir($directory);
     putenv('COMPOSER_ROOT_VERSION=' . $package->getPrettyVersion());
     return $installedFromVcs;
 }
Exemple #11
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $this->versionParser = new VersionParser();
     // init repos
     $platformRepo = new PlatformRepository();
     $composer = $this->getComposer(false);
     if ($input->getOption('self')) {
         $package = $this->getComposer()->getPackage();
         $repos = $installedRepo = new ArrayRepository(array($package));
     } elseif ($input->getOption('platform')) {
         $repos = $installedRepo = $platformRepo;
     } elseif ($input->getOption('installed')) {
         $repos = $installedRepo = $this->getComposer()->getRepositoryManager()->getLocalRepository();
     } elseif ($input->getOption('available')) {
         $installedRepo = $platformRepo;
         if ($composer) {
             $repos = new CompositeRepository($composer->getRepositoryManager()->getRepositories());
         } else {
             $defaultRepos = Factory::createDefaultRepositories($this->getIO());
             $repos = new CompositeRepository($defaultRepos);
             $output->writeln('No composer.json found in the current directory, showing available packages from ' . implode(', ', array_keys($defaultRepos)));
         }
     } elseif ($composer) {
         $localRepo = $composer->getRepositoryManager()->getLocalRepository();
         $installedRepo = new CompositeRepository(array($localRepo, $platformRepo));
         $repos = new CompositeRepository(array_merge(array($installedRepo), $composer->getRepositoryManager()->getRepositories()));
     } else {
         $defaultRepos = Factory::createDefaultRepositories($this->getIO());
         $output->writeln('No composer.json found in the current directory, showing available packages from ' . implode(', ', array_keys($defaultRepos)));
         $installedRepo = $platformRepo;
         $repos = new CompositeRepository(array_merge(array($installedRepo), $defaultRepos));
     }
     if ($composer) {
         $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'show', $input, $output);
         $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);
     }
     // show single package or single version
     if ($input->getArgument('package') || !empty($package)) {
         $versions = array();
         if (empty($package)) {
             list($package, $versions) = $this->getPackage($installedRepo, $repos, $input->getArgument('package'), $input->getArgument('version'));
             if (!$package) {
                 throw new \InvalidArgumentException('Package ' . $input->getArgument('package') . ' not found');
             }
         } else {
             $versions = array($package->getPrettyVersion() => $package->getVersion());
         }
         $this->printMeta($input, $output, $package, $versions, $installedRepo, $repos);
         $this->printLinks($input, $output, $package, 'requires');
         $this->printLinks($input, $output, $package, 'devRequires', 'requires (dev)');
         if ($package->getSuggests()) {
             $output->writeln("\n<info>suggests</info>");
             foreach ($package->getSuggests() as $suggested => $reason) {
                 $output->writeln($suggested . ' <comment>' . $reason . '</comment>');
             }
         }
         $this->printLinks($input, $output, $package, 'provides');
         $this->printLinks($input, $output, $package, 'conflicts');
         $this->printLinks($input, $output, $package, 'replaces');
         return;
     }
     // list packages
     $packages = array();
     if ($repos instanceof CompositeRepository) {
         $repos = $repos->getRepositories();
     } elseif (!is_array($repos)) {
         $repos = array($repos);
     }
     foreach ($repos as $repo) {
         if ($repo === $platformRepo) {
             $type = '<info>platform</info>:';
         } elseif ($repo === $installedRepo || $installedRepo instanceof CompositeRepository && in_array($repo, $installedRepo->getRepositories(), true)) {
             $type = '<info>installed</info>:';
         } else {
             $type = '<comment>available</comment>:';
         }
         if ($repo instanceof ComposerRepository && $repo->hasProviders()) {
             foreach ($repo->getProviderNames() as $name) {
                 $packages[$type][$name] = $name;
             }
         } else {
             foreach ($repo->getPackages() as $package) {
                 if (!isset($packages[$type][$package->getName()]) || !is_object($packages[$type][$package->getName()]) || version_compare($packages[$type][$package->getName()]->getVersion(), $package->getVersion(), '<')) {
                     $packages[$type][$package->getName()] = $package;
                 }
             }
         }
     }
     $tree = !$input->getOption('platform') && !$input->getOption('installed') && !$input->getOption('available');
     $indent = $tree ? '  ' : '';
     foreach (array('<info>platform</info>:' => true, '<comment>available</comment>:' => false, '<info>installed</info>:' => true) as $type => $showVersion) {
         if (isset($packages[$type])) {
             if ($tree) {
                 $output->writeln($type);
             }
             ksort($packages[$type]);
             $nameLength = $versionLength = 0;
             foreach ($packages[$type] as $package) {
                 if (is_object($package)) {
                     $nameLength = max($nameLength, strlen($package->getPrettyName()));
                     $versionLength = max($versionLength, strlen($this->versionParser->formatVersion($package)));
                 } else {
                     $nameLength = max($nameLength, $package);
                 }
             }
             list($width) = $this->getApplication()->getTerminalDimensions();
             if (null === $width) {
                 // In case the width is not detected, we're probably running the command
                 // outside of a real terminal, use space without a limit
                 $width = PHP_INT_MAX;
             }
             if (defined('PHP_WINDOWS_VERSION_BUILD')) {
                 $width--;
             }
             $writePath = !$input->getOption('name-only') && $input->getOption('path');
             $writeVersion = !$input->getOption('name-only') && !$input->getOption('path') && $showVersion && $nameLength + $versionLength + 3 <= $width;
             $writeDescription = !$input->getOption('name-only') && !$input->getOption('path') && $nameLength + ($showVersion ? $versionLength : 0) + 24 <= $width;
             foreach ($packages[$type] as $package) {
                 if (is_object($package)) {
                     $output->write($indent . str_pad($package->getPrettyName(), $nameLength, ' '), false);
                     if ($writeVersion) {
                         $output->write(' ' . str_pad($this->versionParser->formatVersion($package), $versionLength, ' '), false);
                     }
                     if ($writeDescription) {
                         $description = strtok($package->getDescription(), "\r\n");
                         $remaining = $width - $nameLength - $versionLength - 4;
                         if (strlen($description) > $remaining) {
                             $description = substr($description, 0, $remaining - 3) . '...';
                         }
                         $output->write(' ' . $description);
                     }
                     if ($writePath) {
                         $path = strtok(realpath($composer->getInstallationManager()->getInstallPath($package)), "\r\n");
                         $output->write(' ' . $path);
                     }
                 } else {
                     $output->write($indent . $package);
                 }
                 $output->writeln('');
             }
             if ($tree) {
                 $output->writeln('');
             }
         }
     }
 }
Exemple #12
0
 protected function findPackages($name)
 {
     $packages = array();
     // init repos
     if (!$this->repos) {
         $this->repos = new CompositeRepository(array_merge(array(new PlatformRepository()), Factory::createDefaultRepositories($this->getIO())));
     }
     $token = strtolower($name);
     $this->repos->filterPackages(function ($package) use($token, &$packages) {
         if (false !== strpos($package->getName(), $token)) {
             $packages[] = $package;
         }
     });
     return $packages;
 }
Exemple #13
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     // init repos
     $platformRepo = new PlatformRepository();
     $getRepositories = function (Composer $composer, $dev) {
         $manager = $composer->getRepositoryManager();
         $repos = new CompositeRepository(array($manager->getLocalRepository()));
         if ($dev) {
             $repos->addRepository($manager->getLocalDevRepository());
         }
         return $repos;
     };
     if ($input->getOption('self')) {
         $package = $this->getComposer(false)->getPackage();
         $repos = $installedRepo = new ArrayRepository(array($package));
     } elseif ($input->getOption('platform')) {
         $repos = $installedRepo = $platformRepo;
     } elseif ($input->getOption('installed')) {
         $repos = $installedRepo = $getRepositories($this->getComposer(), $input->getOption('dev'));
     } elseif ($composer = $this->getComposer(false)) {
         $localRepo = $getRepositories($composer, $input->getOption('dev'));
         $installedRepo = new CompositeRepository(array($localRepo, $platformRepo));
         $repos = new CompositeRepository(array_merge(array($installedRepo), $composer->getRepositoryManager()->getRepositories()));
     } else {
         $defaultRepos = Factory::createDefaultRepositories($this->getIO());
         $output->writeln('No composer.json found in the current directory, showing packages from ' . implode(', ', array_keys($defaultRepos)));
         $installedRepo = $platformRepo;
         $repos = new CompositeRepository(array_merge(array($installedRepo), $defaultRepos));
     }
     // show single package or single version
     if ($input->getArgument('package') || !empty($package)) {
         $versions = array();
         if (empty($package)) {
             list($package, $versions) = $this->getPackage($installedRepo, $repos, $input->getArgument('package'), $input->getArgument('version'));
             if (!$package) {
                 throw new \InvalidArgumentException('Package ' . $input->getArgument('package') . ' not found');
             }
         } else {
             $versions = array($package->getPrettyVersion() => $package->getVersion());
         }
         $this->printMeta($input, $output, $package, $versions, $installedRepo, $repos);
         $this->printLinks($input, $output, $package, 'requires');
         $this->printLinks($input, $output, $package, 'devRequires', 'requires (dev)');
         if ($package->getSuggests()) {
             $output->writeln("\n<info>suggests</info>");
             foreach ($package->getSuggests() as $suggested => $reason) {
                 $output->writeln($suggested . ' <comment>' . $reason . '</comment>');
             }
         }
         $this->printLinks($input, $output, $package, 'provides');
         $this->printLinks($input, $output, $package, 'conflicts');
         $this->printLinks($input, $output, $package, 'replaces');
         return;
     }
     // list packages
     $packages = array();
     $repos->filterPackages(function ($package) use(&$packages, $platformRepo, $installedRepo) {
         if ($platformRepo->hasPackage($package)) {
             $type = '<info>platform</info>:';
         } elseif ($installedRepo->hasPackage($package)) {
             $type = '<info>installed</info>:';
         } else {
             $type = '<comment>available</comment>:';
         }
         if (!isset($packages[$type][$package->getName()]) || version_compare($packages[$type][$package->getName()]->getVersion(), $package->getVersion(), '<')) {
             $packages[$type][$package->getName()] = $package;
         }
     }, 'Composer\\Package\\CompletePackage');
     foreach (array('<info>platform</info>:' => true, '<comment>available</comment>:' => false, '<info>installed</info>:' => true) as $type => $showVersion) {
         if (isset($packages[$type])) {
             $output->writeln($type);
             ksort($packages[$type]);
             foreach ($packages[$type] as $package) {
                 $output->writeln('  ' . $package->getPrettyName() . ' ' . ($showVersion ? '[' . $package->getPrettyVersion() . ']' : '') . ' <comment>:</comment> ' . strtok($package->getDescription(), "\r\n"));
             }
             $output->writeln('');
         }
     }
 }
Exemple #14
0
 /**
  * Initializes repositories
  *
  * Returns an array of repos in order they should be checked in
  *
  * @return RepositoryInterface[]
  */
 private function initializeRepos()
 {
     $composer = $this->getComposer(false);
     if ($composer) {
         return array($composer->getRepositoryManager()->getLocalRepository(), new CompositeRepository($composer->getRepositoryManager()->getRepositories()));
     }
     $defaultRepos = Factory::createDefaultRepositories($this->getIO());
     return array(new CompositeRepository($defaultRepos));
 }
 public function installProject(IOInterface $io, $packageName, $directory = null, $packageVersion = null, $stability = 'stable', $preferSource = false, $preferDist = false, $installDevPackages = false, $repositoryUrl = null, $disableCustomInstallers = false, $noScripts = false, $keepVcs = false)
 {
     $config = Factory::createConfig();
     $stability = strtolower($stability);
     if ($stability === 'rc') {
         $stability = 'RC';
     }
     if (!isset(BasePackage::$stabilities[$stability])) {
         throw new \InvalidArgumentException('Invalid stability provided (' . $stability . '), must be one of: ' . implode(', ', array_keys(BasePackage::$stabilities)));
     }
     if (null === $repositoryUrl) {
         $sourceRepo = new CompositeRepository(Factory::createDefaultRepositories($io, $config));
     } elseif ("json" === pathinfo($repositoryUrl, PATHINFO_EXTENSION)) {
         $sourceRepo = new FilesystemRepository(new JsonFile($repositoryUrl, new RemoteFilesystem($io)));
     } elseif (0 === strpos($repositoryUrl, 'http')) {
         $sourceRepo = new ComposerRepository(array('url' => $repositoryUrl), $io, $config);
     } else {
         throw new \InvalidArgumentException("Invalid repository url given. Has to be a .json file or an http url.");
     }
     $parser = new VersionParser();
     $candidates = array();
     $requirements = $parser->parseNameVersionPairs(array($packageName));
     $name = strtolower($requirements[0]['name']);
     if (!$packageVersion && isset($requirements[0]['version'])) {
         $packageVersion = $requirements[0]['version'];
     }
     $pool = new Pool($packageVersion ? 'dev' : $stability);
     $pool->addRepository($sourceRepo);
     $constraint = $packageVersion ? new VersionConstraint('=', $parser->normalize($packageVersion)) : null;
     $candidates = $pool->whatProvides($name, $constraint);
     foreach ($candidates as $key => $candidate) {
         if ($candidate->getName() !== $name) {
             unset($candidates[$key]);
         }
     }
     if (!$candidates) {
         throw new \InvalidArgumentException("Could not find package {$name}" . ($packageVersion ? " with version {$packageVersion}." : " with stability {$stability}."));
     }
     if (null === $directory) {
         $parts = explode("/", $name, 2);
         $directory = getcwd() . DIRECTORY_SEPARATOR . array_pop($parts);
     }
     // select highest version if we have many
     $package = $candidates[0];
     foreach ($candidates as $candidate) {
         if (version_compare($package->getVersion(), $candidate->getVersion(), '<')) {
             $package = $candidate;
         }
     }
     unset($candidates);
     $io->write('<info>Installing ' . $package->getName() . ' (' . VersionParser::formatVersion($package, false) . ')</info>');
     if ($disableCustomInstallers) {
         $io->write('<info>Custom installers have been disabled.</info>');
     }
     if (0 === strpos($package->getPrettyVersion(), 'dev-') && in_array($package->getSourceType(), array('git', 'hg'))) {
         $package->setSourceReference(substr($package->getPrettyVersion(), 4));
     }
     $dm = $this->createDownloadManager($io, $config);
     $dm->setPreferSource($preferSource)->setPreferDist($preferDist);
     $projectInstaller = new ProjectInstaller($directory, $dm);
     $im = $this->createInstallationManager();
     $im->addInstaller($projectInstaller);
     $im->install(new InstalledFilesystemRepository(new JsonFile('php://memory')), new InstallOperation($package));
     $im->notifyInstalls();
     $installedFromVcs = 'source' === $package->getInstallationSource();
     $io->write('<info>Created project in ' . $directory . '</info>');
     chdir($directory);
     putenv('COMPOSER_ROOT_VERSION=' . $package->getPrettyVersion());
     // clean up memory
     unset($dm, $im, $config, $projectInstaller, $sourceRepo, $package);
     // install dependencies of the created project
     $composer = Factory::create($io);
     $installer = Installer::create($io, $composer);
     $installer->setPreferSource($preferSource)->setPreferDist($preferDist)->setDevMode($installDevPackages)->setRunScripts(!$noScripts);
     if ($disableCustomInstallers) {
         $installer->disableCustomInstallers();
     }
     if (!$installer->run()) {
         return 1;
     }
     if (!$keepVcs && $installedFromVcs && (!$io->isInteractive() || $io->askConfirmation('<info>Do you want to remove the existing VCS (.git, .svn..) history?</info> [<comment>Y,n</comment>]? ', true))) {
         $finder = new Finder();
         $finder->depth(0)->directories()->in(getcwd())->ignoreVCS(false)->ignoreDotFiles(false);
         foreach (array('.svn', '_svn', 'CVS', '_darcs', '.arch-params', '.monotone', '.bzr', '.git', '.hg') as $vcsName) {
             $finder->name($vcsName);
         }
         try {
             $fs = new Filesystem();
             $dirs = iterator_to_array($finder);
             unset($finder);
             foreach ($dirs as $dir) {
                 if (!$fs->removeDirectory($dir)) {
                     throw new \RuntimeException('Could not remove ' . $dir);
                 }
             }
         } catch (\Exception $e) {
             $io->write('<error>An error occurred while removing the VCS metadata: ' . $e->getMessage() . '</error>');
         }
     }
     return 0;
 }
 /**
  * @param  array            $config package data
  * @param  string           $class  FQCN to be instantiated
  * @param  string           $cwd    cwd of the root package to be used to guess the version if it is not provided
  * @return PackageInterface
  */
 public function load(array $config, $class = 'Composer\\Package\\RootPackage', $cwd = null)
 {
     if (!isset($config['name'])) {
         $config['name'] = '__root__';
     }
     $autoVersioned = false;
     if (!isset($config['version'])) {
         // override with env var if available
         if (getenv('COMPOSER_ROOT_VERSION')) {
             $version = getenv('COMPOSER_ROOT_VERSION');
         } else {
             $version = $this->versionGuesser->guessVersion($config, $cwd ?: getcwd());
         }
         if (!$version) {
             $version = '1.0.0';
             $autoVersioned = true;
         }
         $config['version'] = $version;
     }
     $realPackage = $package = parent::load($config, $class);
     if ($realPackage instanceof AliasPackage) {
         $realPackage = $package->getAliasOf();
     }
     if ($autoVersioned) {
         $realPackage->replaceVersion($realPackage->getVersion(), 'No version set (parsed as 1.0.0)');
     }
     if (isset($config['minimum-stability'])) {
         $realPackage->setMinimumStability(VersionParser::normalizeStability($config['minimum-stability']));
     }
     $aliases = array();
     $stabilityFlags = array();
     $references = array();
     foreach (array('require', 'require-dev') as $linkType) {
         if (isset($config[$linkType])) {
             $linkInfo = BasePackage::$supportedLinkTypes[$linkType];
             $method = 'get' . ucfirst($linkInfo['method']);
             $links = array();
             foreach ($realPackage->{$method}() as $link) {
                 $links[$link->getTarget()] = $link->getConstraint()->getPrettyString();
             }
             $aliases = $this->extractAliases($links, $aliases);
             $stabilityFlags = $this->extractStabilityFlags($links, $stabilityFlags, $realPackage->getMinimumStability());
             $references = $this->extractReferences($links, $references);
         }
     }
     if (isset($links[$config['name']])) {
         throw new \InvalidArgumentException(sprintf('Root package \'%s\' cannot require itself in its composer.json' . PHP_EOL . 'Did you accidentally name your root package after an external package?', $config['name']));
     }
     $realPackage->setAliases($aliases);
     $realPackage->setStabilityFlags($stabilityFlags);
     $realPackage->setReferences($references);
     if (isset($config['prefer-stable'])) {
         $realPackage->setPreferStable((bool) $config['prefer-stable']);
     }
     $repos = Factory::createDefaultRepositories(null, $this->config, $this->manager);
     foreach ($repos as $repo) {
         $this->manager->addRepository($repo);
     }
     $realPackage->setRepositories($this->config->getRepositories());
     return $package;
 }
Exemple #17
0
 /**
  * Initializes repositories
  *
  * Returns an array of repos in order they should be checked in
  *
  * @return RepositoryInterface[]
  */
 private function initializeRepos()
 {
     $composer = $this->getComposer(false);
     if ($composer) {
         return array_merge(array(new ArrayRepository(array($composer->getPackage()))), array($composer->getRepositoryManager()->getLocalRepository()), $composer->getRepositoryManager()->getRepositories());
     }
     $defaultRepos = Factory::createDefaultRepositories($this->getIO());
     return $defaultRepos;
 }
Exemple #18
0
 /**
  * Retrieves detailed information about a package, or lists all packages available.
  *
  * @param string  $type    Repository type, either: 'self', 'platform', 'installed' or 'available'
  * @param string  $package Package name to show
  * @param string  $version Package version to show
  * @param boolean $root    Query the Bolt parent composer install
  *
  * @return array Array of Composer packages
  */
 public function execute($type, $package = '', $version = '', $root = false)
 {
     $io = $this->getIO();
     if ($root) {
         $composerjson = $this->app['resources']->getPath('root/composer.json');
         $composer = Factory::create($io, $composerjson, true);
     } else {
         $composer = $this->getComposer();
     }
     $this->versionParser = new VersionParser();
     // Initialize repos.
     $platformRepo = new PlatformRepository();
     switch ($type) {
         case 'self':
             $package = $composer->getPackage();
             $repos = $installedRepo = new ArrayRepository([$package]);
             break;
         case 'platform':
             $repos = $platformRepo;
             $installedRepo = $platformRepo;
             break;
         case 'installed':
             $repos = $composer->getRepositoryManager()->getLocalRepository();
             $installedRepo = $repos;
             break;
         case 'available':
             $installedRepo = $platformRepo;
             if ($composer) {
                 $repos = new CompositeRepository($composer->getRepositoryManager()->getRepositories());
             } else {
                 // No composer.json found in the current directory, showing available packages from default repos.
                 $defaultRepos = Factory::createDefaultRepositories($io);
                 $repos = new CompositeRepository($defaultRepos);
             }
             break;
         default:
             if ($composer) {
                 $localRepo = $composer->getRepositoryManager()->getLocalRepository();
                 $installedRepo = new CompositeRepository([$localRepo, $platformRepo]);
                 $merged = array_merge([$installedRepo], $composer->getRepositoryManager()->getRepositories());
                 $repos = new CompositeRepository($merged);
             } else {
                 // No composer.json found in the current directory, showing available packages from default repos.
                 $defaultRepos = Factory::createDefaultRepositories($io);
                 $installedRepo = $platformRepo;
                 $repos = new CompositeRepository(array_merge([$installedRepo], $defaultRepos));
             }
     }
     // Single package or single version.
     if (!empty($package)) {
         if (is_object($package)) {
             return [$package->getName() => ['package' => $package, 'versions' => $package->getVersion()]];
         } else {
             return $this->getPackage($installedRepo, $repos, $package, $version);
         }
     }
     $packages = [];
     if ($repos instanceof CompositeRepository) {
         $repos = $repos->getRepositories();
     } elseif (!is_array($repos)) {
         $repos = [$repos];
     }
     foreach ($repos as $repo) {
         if ($repo instanceof ComposerRepository && $repo->hasProviders()) {
             foreach ($repo->getProviderNames() as $name) {
                 $packages[$name] = $name;
             }
         } else {
             foreach ($repo->getPackages() as $package) {
                 /** @var $package \Composer\Package\PackageInterface */
                 if (!isset($packages[$type][$package->getName()]) || !is_object($packages[$type][$package->getName()]) || version_compare($packages[$type][$package->getName()]->getVersion(), $package->getVersion(), '<')) {
                     $packages[$package->getName()] = ['package' => $package, 'versions' => $package->getVersion()];
                 }
             }
         }
     }
     ksort($packages);
     return $packages;
 }
 /**
  * initializes the repo
  *
  * @param InputInterface  $input
  * @param OutputInterface $output
  * @return CompositeRepository
  */
 private function initializeRepo(InputInterface $input, OutputInterface $output)
 {
     $composer = $this->getComposer(false);
     if ($composer) {
         $repo = new CompositeRepository($composer->getRepositoryManager()->getRepositories());
     } else {
         $defaultRepos = Factory::createDefaultRepositories($this->getIO());
         $repo = new CompositeRepository($defaultRepos);
     }
     return $repo;
 }
 /**
  * Returns a package tag list.
  *
  * @param CompletePackageInterface $package
  *
  * @return array
  */
 public function getPackageTags(CompletePackageInterface $package)
 {
     $ds = DIRECTORY_SEPARATOR;
     putenv("COMPOSER_HOME={$this->vendorDir}{$ds}composer");
     $repos = Factory::createDefaultRepositories(new NullIO());
     $compositeRepo = new CompositeRepository($repos);
     $pkgs = $compositeRepo->findPackages($package->getPrettyName());
     $tags = array();
     foreach ($pkgs as $pkg) {
         $tags[] = $pkg->getPrettyVersion();
     }
     return $tags;
 }
 protected function installRootPackage(IOInterface $io, Config $config, $packageName, $directory = null, $packageVersion = null, $stability = 'stable', $preferSource = false, $preferDist = false, $installDevPackages = false, $repositoryUrl = null, $disablePlugins = false, $noScripts = false, $keepVcs = false, $noProgress = false)
 {
     if (null === $repositoryUrl) {
         $sourceRepo = new CompositeRepository(Factory::createDefaultRepositories($io, $config));
     } elseif ("json" === pathinfo($repositoryUrl, PATHINFO_EXTENSION) && file_exists($repositoryUrl)) {
         $json = new JsonFile($repositoryUrl, new RemoteFilesystem($io, $config));
         $data = $json->read();
         if (!empty($data['packages']) || !empty($data['includes']) || !empty($data['provider-includes'])) {
             $sourceRepo = new ComposerRepository(array('url' => 'file://' . strtr(realpath($repositoryUrl), '\\', '/')), $io, $config);
         } else {
             $sourceRepo = new FilesystemRepository($json);
         }
     } elseif (0 === strpos($repositoryUrl, 'http')) {
         $sourceRepo = new ComposerRepository(array('url' => $repositoryUrl), $io, $config);
     } else {
         throw new \InvalidArgumentException("Invalid repository url given. Has to be a .json file or an http url.");
     }
     $parser = new VersionParser();
     $requirements = $parser->parseNameVersionPairs(array($packageName));
     $name = strtolower($requirements[0]['name']);
     if (!$packageVersion && isset($requirements[0]['version'])) {
         $packageVersion = $requirements[0]['version'];
     }
     if (null === $stability) {
         if (preg_match('{^[^,\\s]*?@(' . implode('|', array_keys(BasePackage::$stabilities)) . ')$}i', $packageVersion, $match)) {
             $stability = $match[1];
         } else {
             $stability = VersionParser::parseStability($packageVersion);
         }
     }
     $stability = VersionParser::normalizeStability($stability);
     if (!isset(BasePackage::$stabilities[$stability])) {
         throw new \InvalidArgumentException('Invalid stability provided (' . $stability . '), must be one of: ' . implode(', ', array_keys(BasePackage::$stabilities)));
     }
     $pool = new Pool($stability);
     $pool->addRepository($sourceRepo);
     // find the latest version if there are multiple
     $versionSelector = new VersionSelector($pool);
     $package = $versionSelector->findBestCandidate($name, $packageVersion);
     if (!$package) {
         throw new \InvalidArgumentException("Could not find package {$name}" . ($packageVersion ? " with version {$packageVersion}." : " with stability {$stability}."));
     }
     if (null === $directory) {
         $parts = explode("/", $name, 2);
         $directory = getcwd() . DIRECTORY_SEPARATOR . array_pop($parts);
     }
     // handler Ctrl+C for unix-like systems
     if (function_exists('pcntl_signal')) {
         declare (ticks=100);
         pcntl_signal(SIGINT, function () use($directory) {
             $fs = new Filesystem();
             $fs->removeDirectory($directory);
             exit(130);
         });
     }
     $io->writeError('<info>Installing ' . $package->getName() . ' (' . VersionParser::formatVersion($package, false) . ')</info>');
     if ($disablePlugins) {
         $io->writeError('<info>Plugins have been disabled.</info>');
     }
     if (0 === strpos($package->getPrettyVersion(), 'dev-') && in_array($package->getSourceType(), array('git', 'hg'))) {
         $package->setSourceReference(substr($package->getPrettyVersion(), 4));
     }
     $dm = $this->createDownloadManager($io, $config);
     $dm->setPreferSource($preferSource)->setPreferDist($preferDist)->setOutputProgress(!$noProgress);
     $projectInstaller = new ProjectInstaller($directory, $dm);
     $im = $this->createInstallationManager();
     $im->addInstaller($projectInstaller);
     $im->install(new InstalledFilesystemRepository(new JsonFile('php://memory')), new InstallOperation($package));
     $im->notifyInstalls();
     $installedFromVcs = 'source' === $package->getInstallationSource();
     $io->writeError('<info>Created project in ' . $directory . '</info>');
     chdir($directory);
     $_SERVER['COMPOSER_ROOT_VERSION'] = $package->getPrettyVersion();
     putenv('COMPOSER_ROOT_VERSION=' . $_SERVER['COMPOSER_ROOT_VERSION']);
     return $installedFromVcs;
 }
Exemple #22
0
 protected function findPackages($name)
 {
     $packages = array();
     // init repos
     if (!$this->repos) {
         $this->repos = new CompositeRepository(array_merge(array(new PlatformRepository()), Factory::createDefaultRepositories($this->getIO())));
     }
     $token = strtolower($name);
     foreach ($this->repos->getPackages() as $package) {
         if (false === ($pos = strpos($package->getName(), $token))) {
             continue;
         }
         $packages[] = $package;
     }
     return $packages;
 }
Exemple #23
0
 protected function getRepos()
 {
     if (!$this->repos) {
         $this->repos = new CompositeRepository(array_merge(array(new PlatformRepository()), Factory::createDefaultRepositories($this->getIO())));
     }
     return $this->repos;
 }
Exemple #24
0
 protected function findPackages($name)
 {
     $packages = array();
     if (!$this->repos) {
         $this->repos = new CompositeRepository(array_merge(array(new PlatformRepository()), Factory::createDefaultRepositories($this->getIO())));
     }
     return $this->repos->search($name);
 }