Ejemplo n.º 1
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));
     }
     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'] : ''));
     }
 }
 /**
  * Executes composer for the extension.
  */
 public function execute(InputInterface $input, OutputInterface $output)
 {
     $name = $this->argument('extension');
     $update = $this->option('update');
     if (!is_dir($path = $this->pagekit['path.extensions'] . "/{$name}") && file_exists("{$path}/extension.json")) {
         $this->error("Extension not exists '{$path}'");
         exit;
     }
     $package = json_decode(file_get_contents("{$path}/extension.json"), true);
     if (!isset($package['composer']) || empty($package['composer'])) {
         $this->error("Composer not defined in '{$path}/extension.json'");
         exit;
     }
     $this->loadComposer($path);
     $io = new ConsoleIO($input, $output, $this->getHelperSet());
     $composer = Factory::create($io, $package['composer']);
     $lockFile = new JsonFile("{$path}/extension.lock");
     $locker = new Locker($io, $lockFile, $composer->getRepositoryManager(), $composer->getInstallationManager(), md5(json_encode($package['composer'])));
     $composer->setLocker($locker);
     $installed = new JsonFile($this->pagekit['path'] . '/vendor/composer/installed.json');
     $internal = new CompositeRepository([]);
     $internal->addRepository(new InstalledFilesystemRepository($installed));
     $installer = Installer::create($io, $composer);
     $installer->setAdditionalInstalledRepository($internal);
     $installer->setUpdate($update);
     return $installer->run();
 }
Ejemplo n.º 3
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->app['extend.manager']->getComposer();
     $io = $this->app['extend.manager']->getIO();
     $platformRepo = new PlatformRepository();
     if ($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($io);
         //No composer.json found in the current directory, showing packages from local repo
         $installedRepo = $platformRepo;
         $repos = new CompositeRepository(array_merge(array($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, array('event' => 'exception', 'exception' => $e));
         throw new PackageManagerException($e->getMessage(), $e->getCode(), $e);
     }
 }
Ejemplo n.º 4
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 {
         $output->writeln('No composer.json found in the current directory, showing packages from packagist.org');
         $installedRepo = $platformRepo;
         $repos = new CompositeRepository(array($installedRepo, new ComposerRepository(array('url' => 'http://packagist.org'))));
     }
     $tokens = array_map('strtolower', $input->getArgument('tokens'));
     foreach ($repos->getPackages() as $package) {
         foreach ($tokens as $token) {
             if (false === ($pos = strpos($package->getName(), $token))) {
                 continue;
             }
             if ($platformRepo->hasPackage($package)) {
                 $type = '<info>platform: </info> ';
             } elseif ($installedRepo->hasPackage($package)) {
                 $type = '<info>installed:</info> ';
             } else {
                 $type = '<comment>available:</comment> ';
             }
             $name = substr($package->getPrettyName(), 0, $pos) . '<highlight>' . substr($package->getPrettyName(), $pos, strlen($token)) . '</highlight>' . substr($package->getPrettyName(), $pos + strlen($token));
             $output->writeln($type . ': ' . $name . ' <comment>' . $package->getPrettyVersion() . '</comment>');
             continue 2;
         }
     }
 }
Ejemplo n.º 5
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 = RepositoryFactory::defaultRepos($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 = sprintf('%s recieved an error from Composer: %s in %s::%s', __METHOD__, $e->getMessage(), $e->getFile(), $e->getLine());
         $this->app['logger.system']->critical($msg, ['event' => 'exception', 'exception' => $e]);
         throw new PackageManagerException($e->getMessage(), $e->getCode(), $e);
     }
 }
Ejemplo n.º 6
0
 /**
  * Execute the command.
  *
  * @param  InputInterface  $input
  * @param  OutputInterface $output
  * @param  bool            $inverted Whether to invert matching process (why-not vs why behaviour)
  * @return int|null        Exit code of the operation.
  */
 protected function doExecute(InputInterface $input, OutputInterface $output, $inverted = false)
 {
     // Emit command event on startup
     $composer = $this->getComposer();
     $commandEvent = new CommandEvent(PluginEvents::COMMAND, $this->getName(), $input, $output);
     $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);
     // Prepare repositories and set up a pool
     $platformOverrides = $composer->getConfig()->get('platform') ?: array();
     $repository = new CompositeRepository(array(new ArrayRepository(array($composer->getPackage())), $composer->getRepositoryManager()->getLocalRepository(), new PlatformRepository(array(), $platformOverrides)));
     $pool = new Pool();
     $pool->addRepository($repository);
     // Parse package name and constraint
     list($needle, $textConstraint) = array_pad(explode(':', $input->getArgument(self::ARGUMENT_PACKAGE)), 2, $input->getArgument(self::ARGUMENT_CONSTRAINT));
     // Find packages that are or provide the requested package first
     $packages = $pool->whatProvides($needle);
     if (empty($packages)) {
         throw new \InvalidArgumentException(sprintf('Could not find package "%s" in your project', $needle));
     }
     // If the version we ask for is not installed then we need to locate it in remote repos and add it.
     // This is needed for why-not to resolve conflicts from an uninstalled version against installed packages.
     if (!$repository->findPackage($needle, $textConstraint)) {
         $defaultRepos = new CompositeRepository(RepositoryFactory::defaultRepos($this->getIO()));
         if ($match = $defaultRepos->findPackage($needle, $textConstraint)) {
             $repository->addRepository(new ArrayRepository(array(clone $match)));
         }
     }
     // Include replaced packages for inverted lookups as they are then the actual starting point to consider
     $needles = array($needle);
     if ($inverted) {
         foreach ($packages as $package) {
             $needles = array_merge($needles, array_map(function (Link $link) {
                 return $link->getTarget();
             }, $package->getReplaces()));
         }
     }
     // Parse constraint if one was supplied
     if ('*' !== $textConstraint) {
         $versionParser = new VersionParser();
         $constraint = $versionParser->parseConstraints($textConstraint);
     } else {
         $constraint = null;
     }
     // Parse rendering options
     $renderTree = $input->getOption(self::OPTION_TREE);
     $recursive = $renderTree || $input->getOption(self::OPTION_RECURSIVE);
     // Resolve dependencies
     $results = $repository->getDependents($needles, $constraint, $inverted, $recursive);
     if (empty($results)) {
         $extra = null !== $constraint ? sprintf(' in versions %smatching %s', $inverted ? 'not ' : '', $textConstraint) : '';
         $this->getIO()->writeError(sprintf('<info>There is no installed package depending on "%s"%s</info>', $needle, $extra));
     } elseif ($renderTree) {
         $this->initStyles($output);
         $root = $packages[0];
         $this->getIO()->write(sprintf('<info>%s</info> %s %s', $root->getPrettyName(), $root->getPrettyVersion(), $root->getDescription()));
         $this->printTree($results);
     } else {
         $this->printTable($output, $results);
     }
     return 0;
 }
Ejemplo n.º 7
0
 /**
  * Runs Composer Update command.
  *
  * @param  array|bool $updates
  * @return bool
  */
 protected function composerUpdate($updates = false)
 {
     $installed = new JsonFile($this->paths['path.vendor'] . '/composer/installed.json');
     $internal = new CompositeRepository([]);
     $internal->addRepository(new InstalledFilesystemRepository($installed));
     $composer = $this->getComposer();
     $installer = Installer::create($this->getIO(), $composer)->setAdditionalInstalledRepository($internal)->setPreferDist(true)->setOptimizeAutoloader(true)->setUpdate(true);
     if ($updates) {
         $installer->setUpdateWhitelist($updates);
     }
     $installer->run();
 }
 /**
  * Search for packages.
  *
  * @param array $tokens
  * @param int   $searchIn
  *
  * @return CompletePackageInterface[]
  */
 protected function searchPackages(array $tokens, $searchIn)
 {
     $repositoryManager = $this->getRepositoryManager();
     $platformRepo = new PlatformRepository();
     $localRepository = $repositoryManager->getLocalRepository();
     $installedRepository = new CompositeRepository(array($localRepository, $platformRepo));
     $repositories = new CompositeRepository(array_merge(array($installedRepository), $repositoryManager->getRepositories()));
     $results = $repositories->search(implode(' ', $tokens), $searchIn);
     $contaoVersion = VERSION . (is_numeric(BUILD) ? '.' . BUILD : '-' . BUILD);
     $constraint = $this->createConstraint('=', $contaoVersion);
     $constraint->setPrettyString($contaoVersion);
     $packages = array();
     foreach ($results as $result) {
         if (!isset($packages[$result['name']])) {
             /** @var PackageInterface[] $versions */
             $versions = $repositories->findPackages($result['name']);
             /** @var PackageInterface|CompletePackageInterface $latestVersion */
             $latestVersion = false;
             $packages[$result['name']] = $result;
             if (count($versions)) {
                 $packages[$result['name']]['type'] = $versions[0]->getType();
                 $packages[$result['name']]['description'] = $versions[0] instanceof CompletePackageInterface ? $versions[0]->getDescription() : '';
                 $packages[$result['name']]['contao-compatible'] = null;
                 foreach ($versions as $version) {
                     $requires = $version->getRequires();
                     if (isset($requires['contao/core']) && $requires['contao/core'] instanceof Link) {
                         /** @var Link $link */
                         $link = $requires['contao/core'];
                         if ($link->getConstraint()->matches($constraint)) {
                             $packages[$result['name']]['contao-compatible'] = true;
                             if (!$latestVersion || $version->getReleaseDate() > $latestVersion->getReleaseDate()) {
                                 $latestVersion = $version;
                             }
                         }
                     }
                 }
             }
             if ($packages[$result['name']]['contao-compatible'] === null) {
                 $packages[$result['name']]['contao-compatible'] = true;
             }
             if ($latestVersion) {
                 $packages[$result['name']]['type'] = $latestVersion->getType();
                 if ($latestVersion instanceof CompletePackageInterface) {
                     $packages[$result['name']]['description'] = $latestVersion->getDescription();
                 }
             }
         }
     }
     return $packages;
 }
Ejemplo n.º 9
0
 public function testGetPackages()
 {
     $arrayRepoOne = new ArrayRepository();
     $arrayRepoOne->addPackage($this->getPackage('foo', '1'));
     $arrayRepoTwo = new ArrayRepository();
     $arrayRepoTwo->addPackage($this->getPackage('bar', '1'));
     $repo = new CompositeRepository(array($arrayRepoOne, $arrayRepoTwo));
     $packages = $repo->getPackages();
     $this->assertCount(2, $packages, "Should get two packages");
     $this->assertEquals("foo", $packages[0]->getName(), "First package should have name of 'foo'");
     $this->assertEquals("1", $packages[0]->getPrettyVersion(), "First package should have pretty version of '1'");
     $this->assertEquals("bar", $packages[1]->getName(), "Second package should have name of 'bar'");
     $this->assertEquals("1", $packages[1]->getPrettyVersion(), "Second package should have pretty version of '1'");
 }
Ejemplo n.º 10
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 {
         $output->writeln('No composer.json found in the current directory, showing packages from packagist.org');
         $installedRepo = $platformRepo;
         $packagist = new ComposerRepository(array('url' => 'http://packagist.org'), $this->getIO(), Factory::createConfig());
         $repos = new CompositeRepository(array($installedRepo, $packagist));
     }
     $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']);
     }
 }
Ejemplo n.º 11
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     // init repos
     $platformRepo = new PlatformRepository();
     $io = $this->getIO();
     if (!($composer = $this->getComposer(false))) {
         $composer = Factory::create($this->getIO(), array());
     }
     $localRepo = $composer->getRepositoryManager()->getLocalRepository();
     $installedRepo = new CompositeRepository(array($localRepo, $platformRepo));
     $repos = new CompositeRepository(array_merge(array($installedRepo), $composer->getRepositoryManager()->getRepositories()));
     $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'search', $input, $output);
     $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);
     $onlyName = $input->getOption('only-name');
     $type = $input->getOption('type') ?: null;
     $flags = $onlyName ? RepositoryInterface::SEARCH_NAME : RepositoryInterface::SEARCH_FULLTEXT;
     $results = $repos->search(implode(' ', $input->getArgument('tokens')), $flags, $type);
     foreach ($results as $result) {
         $io->write($result['name'] . (isset($result['description']) ? ' ' . $result['description'] : ''));
     }
 }
Ejemplo n.º 12
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']);
     }
 }
Ejemplo n.º 13
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 {
         $output->writeln('No composer.json found in the current directory, showing packages from packagist.org');
         $installedRepo = $platformRepo;
         $packagist = new ComposerRepository(array('url' => 'http://packagist.org'), $this->getIO());
         $repos = new CompositeRepository(array($installedRepo, $packagist));
     }
     $tokens = $input->getArgument('tokens');
     $packages = array();
     foreach ($repos->getPackages() as $package) {
         if ($package instanceof AliasPackage || isset($packages[$package->getName()])) {
             continue;
         }
         foreach ($tokens as $token) {
             if (!$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();
             }
             $packages[$package->getName()] = array('name' => $name, 'description' => strtok($package->getDescription(), "\r\n"));
             continue 2;
         }
     }
     foreach ($packages as $details) {
         $output->writeln($details['name'] . ' <comment>:</comment> ' . $details['description']);
     }
 }
Ejemplo n.º 14
0
 /**
  * Run installation (or update)
  */
 public function run()
 {
     if ($this->dryRun) {
         $this->verbose = true;
         $this->runScripts = false;
         $this->installationManager->addInstaller(new NoopInstaller());
         $this->mockLocalRepositories($this->repositoryManager);
     }
     if ($this->preferSource) {
         $this->downloadManager->setPreferSource(true);
     }
     if ($this->preferDist) {
         $this->downloadManager->setPreferDist(true);
     }
     // create installed repo, this contains all local packages + platform packages (php & extensions)
     $installedRootPackage = clone $this->package;
     $installedRootPackage->setRequires(array());
     $installedRootPackage->setDevRequires(array());
     $platformRepo = new PlatformRepository();
     $repos = array_merge($this->repositoryManager->getLocalRepositories(), array(new InstalledArrayRepository(array($installedRootPackage)), $platformRepo));
     $installedRepo = new CompositeRepository($repos);
     if ($this->additionalInstalledRepository) {
         $installedRepo->addRepository($this->additionalInstalledRepository);
     }
     $aliases = $this->getRootAliases();
     $this->aliasPlatformPackages($platformRepo, $aliases);
     if ($this->runScripts) {
         // dispatch pre event
         $eventName = $this->update ? ScriptEvents::PRE_UPDATE_CMD : ScriptEvents::PRE_INSTALL_CMD;
         $this->eventDispatcher->dispatchCommandEvent($eventName, $this->devMode);
     }
     $this->suggestedPackages = array();
     if (!$this->doInstall($this->repositoryManager->getLocalRepository(), $installedRepo, $aliases)) {
         return false;
     }
     if ($this->devMode) {
         if (!$this->doInstall($this->repositoryManager->getLocalDevRepository(), $installedRepo, $aliases, true)) {
             return false;
         }
     }
     // output suggestions
     foreach ($this->suggestedPackages as $suggestion) {
         $target = $suggestion['target'];
         if ($installedRepo->filterPackages(function (PackageInterface $package) use($target) {
             if (in_array($target, $package->getNames())) {
                 return false;
             }
         })) {
             $this->io->write($suggestion['source'] . ' suggests installing ' . $suggestion['target'] . ' (' . $suggestion['reason'] . ')');
         }
     }
     if (!$this->dryRun) {
         // write lock
         if ($this->update || !$this->locker->isLocked()) {
             $updatedLock = $this->locker->setLockData($this->repositoryManager->getLocalRepository()->getPackages(), $this->devMode ? $this->repositoryManager->getLocalDevRepository()->getPackages() : null, $aliases, $this->package->getMinimumStability(), $this->package->getStabilityFlags());
             if ($updatedLock) {
                 $this->io->write('<info>Writing lock file</info>');
             }
         }
         // write autoloader
         $this->io->write('<info>Generating autoload files</info>');
         $localRepos = new CompositeRepository($this->repositoryManager->getLocalRepositories());
         $this->autoloadGenerator->dump($this->config, $localRepos, $this->package, $this->installationManager, 'composer', $this->optimizeAutoloader);
         if ($this->runScripts) {
             // dispatch post event
             $eventName = $this->update ? ScriptEvents::POST_UPDATE_CMD : ScriptEvents::POST_INSTALL_CMD;
             $this->eventDispatcher->dispatchCommandEvent($eventName, $this->devMode);
         }
     }
     return true;
 }
Ejemplo n.º 15
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();
         $repo = 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)));
         $repo = new CompositeRepository($defaultRepos);
     }
     $packages = $repo->findPackages($packageName, $version);
     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;
 }
Ejemplo n.º 16
0
 /**
  * Run installation (or update)
  *
  * @return int 0 on success or a positive error code on failure
  *
  * @throws \Exception
  */
 public function run()
 {
     gc_collect_cycles();
     gc_disable();
     if ($this->dryRun) {
         $this->verbose = true;
         $this->runScripts = false;
         $this->installationManager->addInstaller(new NoopInstaller());
         $this->mockLocalRepositories($this->repositoryManager);
     }
     // TODO remove this BC feature at some point
     // purge old require-dev packages to avoid conflicts with the new way of handling dev requirements
     $devRepo = new InstalledFilesystemRepository(new JsonFile($this->config->get('vendor-dir') . '/composer/installed_dev.json'));
     if ($devRepo->getPackages()) {
         $this->io->writeError('<warning>BC Notice: Removing old dev packages to migrate to the new require-dev handling.</warning>');
         foreach ($devRepo->getPackages() as $package) {
             if ($this->installationManager->isPackageInstalled($devRepo, $package)) {
                 $this->installationManager->uninstall($devRepo, new UninstallOperation($package));
             }
         }
         unlink($this->config->get('vendor-dir') . '/composer/installed_dev.json');
     }
     unset($devRepo, $package);
     // end BC
     if ($this->runScripts) {
         // dispatch pre event
         $eventName = $this->update ? ScriptEvents::PRE_UPDATE_CMD : ScriptEvents::PRE_INSTALL_CMD;
         $this->eventDispatcher->dispatchScript($eventName, $this->devMode);
     }
     $this->downloadManager->setPreferSource($this->preferSource);
     $this->downloadManager->setPreferDist($this->preferDist);
     // clone root package to have one in the installed repo that does not require anything
     // we don't want it to be uninstallable, but its requirements should not conflict
     // with the lock file for example
     $installedRootPackage = clone $this->package;
     $installedRootPackage->setRequires(array());
     $installedRootPackage->setDevRequires(array());
     // create installed repo, this contains all local packages + platform packages (php & extensions)
     $localRepo = $this->repositoryManager->getLocalRepository();
     $platformRepo = new PlatformRepository();
     $repos = array($localRepo, new InstalledArrayRepository(array($installedRootPackage)), $platformRepo);
     $installedRepo = new CompositeRepository($repos);
     if ($this->additionalInstalledRepository) {
         $installedRepo->addRepository($this->additionalInstalledRepository);
     }
     $aliases = $this->getRootAliases();
     $this->aliasPlatformPackages($platformRepo, $aliases);
     try {
         $this->suggestedPackages = array();
         $res = $this->doInstall($localRepo, $installedRepo, $platformRepo, $aliases, $this->devMode);
         if ($res !== 0) {
             return $res;
         }
     } catch (\Exception $e) {
         $this->installationManager->notifyInstalls();
         throw $e;
     }
     $this->installationManager->notifyInstalls();
     // output suggestions if we're in dev mode
     if ($this->devMode) {
         foreach ($this->suggestedPackages as $suggestion) {
             $target = $suggestion['target'];
             foreach ($installedRepo->getPackages() as $package) {
                 if (in_array($target, $package->getNames())) {
                     continue 2;
                 }
             }
             $this->io->writeError($suggestion['source'] . ' suggests installing ' . $suggestion['target'] . ' (' . $suggestion['reason'] . ')');
         }
     }
     # Find abandoned packages and warn user
     foreach ($localRepo->getPackages() as $package) {
         if (!$package instanceof CompletePackage || !$package->isAbandoned()) {
             continue;
         }
         $replacement = is_string($package->getReplacementPackage()) ? 'Use ' . $package->getReplacementPackage() . ' instead' : 'No replacement was suggested';
         $this->io->writeError(sprintf("<error>Package %s is abandoned, you should avoid using it. %s.</error>", $package->getPrettyName(), $replacement));
     }
     if (!$this->dryRun) {
         // write lock
         if ($this->update || !$this->locker->isLocked()) {
             $localRepo->reload();
             // if this is not run in dev mode and the root has dev requires, the lock must
             // contain null to prevent dev installs from a non-dev lock
             $devPackages = $this->devMode || !$this->package->getDevRequires() ? array() : null;
             // split dev and non-dev requirements by checking what would be removed if we update without the dev requirements
             if ($this->devMode && $this->package->getDevRequires()) {
                 $policy = $this->createPolicy();
                 $pool = $this->createPool(true);
                 $pool->addRepository($installedRepo, $aliases);
                 // creating requirements request
                 $request = $this->createRequest($pool, $this->package, $platformRepo);
                 $request->updateAll();
                 foreach ($this->package->getRequires() as $link) {
                     $request->install($link->getTarget(), $link->getConstraint());
                 }
                 $this->eventDispatcher->dispatchInstallerEvent(InstallerEvents::PRE_DEPENDENCIES_SOLVING, false, $policy, $pool, $installedRepo, $request);
                 $solver = new Solver($policy, $pool, $installedRepo);
                 $ops = $solver->solve($request, $this->ignorePlatformReqs);
                 $this->eventDispatcher->dispatchInstallerEvent(InstallerEvents::POST_DEPENDENCIES_SOLVING, false, $policy, $pool, $installedRepo, $request, $ops);
                 foreach ($ops as $op) {
                     if ($op->getJobType() === 'uninstall') {
                         $devPackages[] = $op->getPackage();
                     }
                 }
             }
             $platformReqs = $this->extractPlatformRequirements($this->package->getRequires());
             $platformDevReqs = $this->devMode ? $this->extractPlatformRequirements($this->package->getDevRequires()) : array();
             $updatedLock = $this->locker->setLockData(array_diff($localRepo->getCanonicalPackages(), (array) $devPackages), $devPackages, $platformReqs, $platformDevReqs, $aliases, $this->package->getMinimumStability(), $this->package->getStabilityFlags(), $this->preferStable || $this->package->getPreferStable(), $this->preferLowest);
             if ($updatedLock) {
                 $this->io->writeError('<info>Writing lock file</info>');
             }
         }
         if ($this->dumpAutoloader) {
             // write autoloader
             if ($this->optimizeAutoloader) {
                 $this->io->writeError('<info>Generating optimized autoload files</info>');
             } else {
                 $this->io->writeError('<info>Generating autoload files</info>');
             }
             $this->autoloadGenerator->setDevMode($this->devMode);
             $this->autoloadGenerator->dump($this->config, $localRepo, $this->package, $this->installationManager, 'composer', $this->optimizeAutoloader);
         }
         if ($this->runScripts) {
             // dispatch post event
             $eventName = $this->update ? ScriptEvents::POST_UPDATE_CMD : ScriptEvents::POST_INSTALL_CMD;
             $this->eventDispatcher->dispatchScript($eventName, $this->devMode);
         }
         $vendorDir = $this->config->get('vendor-dir');
         if (is_dir($vendorDir)) {
             touch($vendorDir);
         }
     }
     return 0;
 }
Ejemplo n.º 17
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('');
         }
     }
 }
Ejemplo n.º 18
0
 /**
  * Run installation (or update)
  */
 public function run()
 {
     if ($this->dryRun) {
         $this->verbose = true;
         $this->runScripts = false;
         $this->installationManager->addInstaller(new NoopInstaller());
         $this->mockLocalRepositories($this->repositoryManager);
     }
     // TODO remove this BC feature at some point
     // purge old require-dev packages to avoid conflicts with the new way of handling dev requirements
     $devRepo = new InstalledFilesystemRepository(new JsonFile($this->config->get('vendor-dir') . '/composer/installed_dev.json'));
     if ($devRepo->getPackages()) {
         $this->io->write('<warning>BC Notice: Removing old dev packages to migrate to the new require-dev handling.</warning>');
         foreach ($devRepo->getPackages() as $package) {
             if ($this->installationManager->isPackageInstalled($devRepo, $package)) {
                 $this->installationManager->uninstall($devRepo, new UninstallOperation($package));
             }
         }
         unlink($this->config->get('vendor-dir') . '/composer/installed_dev.json');
     }
     unset($devRepo, $package);
     // end BC
     if ($this->preferSource) {
         $this->downloadManager->setPreferSource(true);
     }
     if ($this->preferDist) {
         $this->downloadManager->setPreferDist(true);
     }
     // create installed repo, this contains all local packages + platform packages (php & extensions)
     $installedRootPackage = clone $this->package;
     $installedRootPackage->setRequires(array());
     $installedRootPackage->setDevRequires(array());
     $localRepo = $this->repositoryManager->getLocalRepository();
     $platformRepo = new PlatformRepository();
     $repos = array($localRepo, new InstalledArrayRepository(array($installedRootPackage)), $platformRepo);
     $installedRepo = new CompositeRepository($repos);
     if ($this->additionalInstalledRepository) {
         $installedRepo->addRepository($this->additionalInstalledRepository);
     }
     $aliases = $this->getRootAliases();
     $this->aliasPlatformPackages($platformRepo, $aliases);
     if ($this->runScripts) {
         // dispatch pre event
         $eventName = $this->update ? ScriptEvents::PRE_UPDATE_CMD : ScriptEvents::PRE_INSTALL_CMD;
         $this->eventDispatcher->dispatchCommandEvent($eventName, $this->devMode);
     }
     try {
         $this->suggestedPackages = array();
         if (!$this->doInstall($localRepo, $installedRepo, $platformRepo, $aliases, $this->devMode)) {
             return false;
         }
     } catch (\Exception $e) {
         $this->installationManager->notifyInstalls();
         throw $e;
     }
     $this->installationManager->notifyInstalls();
     // output suggestions
     foreach ($this->suggestedPackages as $suggestion) {
         $target = $suggestion['target'];
         foreach ($installedRepo->getPackages() as $package) {
             if (in_array($target, $package->getNames())) {
                 continue 2;
             }
         }
         $this->io->write($suggestion['source'] . ' suggests installing ' . $suggestion['target'] . ' (' . $suggestion['reason'] . ')');
     }
     if (!$this->dryRun) {
         // write lock
         if ($this->update || !$this->locker->isLocked()) {
             $localRepo->reload();
             // if this is not run in dev mode and the root has dev requires, the lock must
             // contain null to prevent dev installs from a non-dev lock
             $devPackages = $this->devMode || !$this->package->getDevRequires() ? array() : null;
             // split dev and non-dev requirements by checking what would be removed if we update without the dev requirements
             if ($this->devMode && $this->package->getDevRequires()) {
                 $policy = $this->createPolicy();
                 $pool = $this->createPool();
                 $pool->addRepository($installedRepo, $aliases);
                 // creating requirements request
                 $request = $this->createRequest($pool, $this->package, $platformRepo);
                 $request->updateAll();
                 foreach ($this->package->getRequires() as $link) {
                     $request->install($link->getTarget(), $link->getConstraint());
                 }
                 $solver = new Solver($policy, $pool, $installedRepo);
                 $ops = $solver->solve($request);
                 foreach ($ops as $op) {
                     if ($op->getJobType() === 'uninstall') {
                         $devPackages[] = $op->getPackage();
                     }
                 }
             }
             $platformReqs = $this->extractPlatformRequirements($this->package->getRequires());
             $platformDevReqs = $this->devMode ? $this->extractPlatformRequirements($this->package->getDevRequires()) : array();
             $updatedLock = $this->locker->setLockData(array_diff($localRepo->getPackages(), (array) $devPackages), $devPackages, $platformReqs, $platformDevReqs, $aliases, $this->package->getMinimumStability(), $this->package->getStabilityFlags());
             if ($updatedLock) {
                 $this->io->write('<info>Writing lock file</info>');
             }
         }
         // write autoloader
         $this->io->write('<info>Generating autoload files</info>');
         $this->autoloadGenerator->dump($this->config, $localRepo, $this->package, $this->installationManager, 'composer', $this->optimizeAutoloader);
         if ($this->runScripts) {
             // dispatch post event
             $eventName = $this->update ? ScriptEvents::POST_UPDATE_CMD : ScriptEvents::POST_INSTALL_CMD;
             $this->eventDispatcher->dispatchCommandEvent($eventName, $this->devMode);
         }
     }
     return true;
 }
Ejemplo n.º 19
0
 /**
  * Run installation (or update)
  *
  * @throws \Exception
  * @return int        0 on success or a positive error code on failure
  */
 public function run()
 {
     // Disable GC to save CPU cycles, as the dependency solver can create hundreds of thousands
     // of PHP objects, the GC can spend quite some time walking the tree of references looking
     // for stuff to collect while there is nothing to collect. This slows things down dramatically
     // and turning it off results in much better performance. Do not try this at home however.
     gc_collect_cycles();
     gc_disable();
     if ($this->dryRun) {
         $this->verbose = true;
         $this->runScripts = false;
         $this->installationManager->addInstaller(new NoopInstaller());
         $this->mockLocalRepositories($this->repositoryManager);
     }
     if ($this->runScripts) {
         // dispatch pre event
         $eventName = $this->update ? ScriptEvents::PRE_UPDATE_CMD : ScriptEvents::PRE_INSTALL_CMD;
         $this->eventDispatcher->dispatchScript($eventName, $this->devMode);
     }
     $this->downloadManager->setPreferSource($this->preferSource);
     $this->downloadManager->setPreferDist($this->preferDist);
     // clone root package to have one in the installed repo that does not require anything
     // we don't want it to be uninstallable, but its requirements should not conflict
     // with the lock file for example
     $installedRootPackage = clone $this->package;
     $installedRootPackage->setRequires(array());
     $installedRootPackage->setDevRequires(array());
     // create installed repo, this contains all local packages + platform packages (php & extensions)
     $localRepo = $this->repositoryManager->getLocalRepository();
     if (!$this->update && $this->locker->isLocked()) {
         $platformOverrides = $this->locker->getPlatformOverrides();
     } else {
         $platformOverrides = $this->config->get('platform') ?: array();
     }
     $platformRepo = new PlatformRepository(array(), $platformOverrides);
     $repos = array($localRepo, new InstalledArrayRepository(array($installedRootPackage)), $platformRepo);
     $installedRepo = new CompositeRepository($repos);
     if ($this->additionalInstalledRepository) {
         $installedRepo->addRepository($this->additionalInstalledRepository);
     }
     $aliases = $this->getRootAliases();
     $this->aliasPlatformPackages($platformRepo, $aliases);
     try {
         $this->suggestedPackages = array();
         $res = $this->doInstall($localRepo, $installedRepo, $platformRepo, $aliases, $this->devMode);
         if ($res !== 0) {
             return $res;
         }
     } catch (\Exception $e) {
         if (!$this->dryRun) {
             $this->installationManager->notifyInstalls($this->io);
         }
         throw $e;
     }
     if (!$this->dryRun) {
         $this->installationManager->notifyInstalls($this->io);
     }
     // output suggestions if we're in dev mode
     if ($this->devMode) {
         foreach ($this->suggestedPackages as $suggestion) {
             $target = $suggestion['target'];
             foreach ($installedRepo->getPackages() as $package) {
                 if (in_array($target, $package->getNames())) {
                     continue 2;
                 }
             }
             $this->io->writeError($suggestion['source'] . ' suggests installing ' . $suggestion['target'] . ' (' . $suggestion['reason'] . ')');
         }
     }
     # Find abandoned packages and warn user
     foreach ($localRepo->getPackages() as $package) {
         if (!$package instanceof CompletePackage || !$package->isAbandoned()) {
             continue;
         }
         $replacement = is_string($package->getReplacementPackage()) ? 'Use ' . $package->getReplacementPackage() . ' instead' : 'No replacement was suggested';
         $this->io->writeError(sprintf("<warning>Package %s is abandoned, you should avoid using it. %s.</warning>", $package->getPrettyName(), $replacement));
     }
     if (!$this->dryRun) {
         // write lock
         if ($this->update || !$this->locker->isLocked()) {
             $localRepo->reload();
             // if this is not run in dev mode and the root has dev requires, the lock must
             // contain null to prevent dev installs from a non-dev lock
             $devPackages = $this->devMode || !$this->package->getDevRequires() ? array() : null;
             // split dev and non-dev requirements by checking what would be removed if we update without the dev requirements
             if ($this->devMode && $this->package->getDevRequires()) {
                 $policy = $this->createPolicy();
                 $pool = $this->createPool(true);
                 $pool->addRepository($installedRepo, $aliases);
                 // creating requirements request
                 $request = $this->createRequest($this->package, $platformRepo);
                 $request->updateAll();
                 foreach ($this->package->getRequires() as $link) {
                     $request->install($link->getTarget(), $link->getConstraint());
                 }
                 $this->eventDispatcher->dispatchInstallerEvent(InstallerEvents::PRE_DEPENDENCIES_SOLVING, false, $policy, $pool, $installedRepo, $request);
                 $solver = new Solver($policy, $pool, $installedRepo);
                 $ops = $solver->solve($request, $this->ignorePlatformReqs);
                 $this->eventDispatcher->dispatchInstallerEvent(InstallerEvents::POST_DEPENDENCIES_SOLVING, false, $policy, $pool, $installedRepo, $request, $ops);
                 foreach ($ops as $op) {
                     if ($op->getJobType() === 'uninstall') {
                         $devPackages[] = $op->getPackage();
                     }
                 }
             }
             $platformReqs = $this->extractPlatformRequirements($this->package->getRequires());
             $platformDevReqs = $this->devMode ? $this->extractPlatformRequirements($this->package->getDevRequires()) : array();
             $updatedLock = $this->locker->setLockData(array_diff($localRepo->getCanonicalPackages(), (array) $devPackages), $devPackages, $platformReqs, $platformDevReqs, $aliases, $this->package->getMinimumStability(), $this->package->getStabilityFlags(), $this->preferStable || $this->package->getPreferStable(), $this->preferLowest, $this->config->get('platform') ?: array());
             if ($updatedLock) {
                 $this->io->writeError('<info>Writing lock file</info>');
             }
         }
         if ($this->dumpAutoloader) {
             // write autoloader
             if ($this->optimizeAutoloader) {
                 $this->io->writeError('<info>Generating optimized autoload files</info>');
             } else {
                 $this->io->writeError('<info>Generating autoload files</info>');
             }
             $this->autoloadGenerator->setDevMode($this->devMode);
             $this->autoloadGenerator->setClassMapAuthoritative($this->classMapAuthoritative);
             $this->autoloadGenerator->setRunScripts($this->runScripts);
             $this->autoloadGenerator->dump($this->config, $localRepo, $this->package, $this->installationManager, 'composer', $this->optimizeAutoloader);
         }
         if ($this->runScripts) {
             // dispatch post event
             $eventName = $this->update ? ScriptEvents::POST_UPDATE_CMD : ScriptEvents::POST_INSTALL_CMD;
             $this->eventDispatcher->dispatchScript($eventName, $this->devMode);
         }
         $vendorDir = $this->config->get('vendor-dir');
         if (is_dir($vendorDir)) {
             // suppress errors as this fails sometimes on OSX for no apparent reason
             // see https://github.com/composer/composer/issues/4070#issuecomment-129792748
             @touch($vendorDir);
         }
     }
     // re-enable GC except on HHVM which triggers a warning here
     if (!defined('HHVM_VERSION')) {
         gc_enable();
     }
     return 0;
 }
Ejemplo n.º 20
0
 /**
  * @return RepositoryInterface
  */
 private function createInstalledRepo(RepositoryInterface $localRepo, PlatformRepository $platformRepo)
 {
     // clone root package to have one in the installed repo that does not require anything
     // we don't want it to be uninstallable, but its requirements should not conflict
     // with the lock file for example
     $installedRootPackage = clone $this->package;
     $installedRootPackage->setRequires(array());
     $installedRootPackage->setDevRequires(array());
     $repos = array($localRepo, new InstalledArrayRepository(array($installedRootPackage)), $platformRepo);
     $installedRepo = new CompositeRepository($repos);
     if ($this->additionalInstalledRepository) {
         $installedRepo->addRepository($this->additionalInstalledRepository);
     }
     return $installedRepo;
 }
Ejemplo n.º 21
0
 /**
  * If the version we ask for is not installed then we need to locate it in
  * remote repos and add it.
  *
  * This is needed for why-not to resolve conflicts from an uninstalled
  * version against installed packages.
  *
  * @param string $packageName    Package to inspect.
  * @param string $textConstraint Optional version constraint
  * @param bool   $onlyLocal
  *
  * @return Pool
  */
 private function getRequiredPool($packageName, $textConstraint, $onlyLocal)
 {
     if ($onlyLocal === false) {
         return $this->getPool();
     }
     $composer = $this->getComposer();
     $pool = new Pool();
     // Prepare repositories and set up a pool
     $platformOverrides = $composer->getConfig()->get('platform') ?: [];
     /** @var BasePackage $rootPackage */
     $rootPackage = $composer->getPackage();
     if ($rootPackage->getRepository() === null) {
         $packageRepo = new ArrayRepository([$composer->getPackage()]);
         $localRepo = $composer->getRepositoryManager()->getLocalRepository();
         $platformRepo = new PlatformRepository([], $platformOverrides);
         $compositeRepository = new CompositeRepository([$packageRepo, $localRepo, $platformRepo]);
         $pool->addRepository($compositeRepository);
         $defaultRepos = new CompositeRepository(RepositoryFactory::defaultRepos($this->getIO()));
         $match = $defaultRepos->findPackage($packageName, $textConstraint);
         if ($match) {
             $compositeRepository->addRepository(new ArrayRepository([clone $match]));
         }
     }
     return $pool;
 }
Ejemplo n.º 22
0
 /**
  * Create a repository search instance.
  *
  * @param string   $keywords  The search keywords.
  *
  * @param string   $type      The desired search type.
  *
  * @param Composer $composer  The composer instance.
  *
  * @param int      $threshold The threshold after which to stop searching.
  *
  * @return CompositeSearch
  */
 private function getRepositorySearch($keywords, $type, Composer $composer, $threshold)
 {
     $repositoryManager = $composer->getRepositoryManager();
     $localRepository = $repositoryManager->getLocalRepository();
     $repositories = new CompositeRepository([$localRepository]);
     // If we do not search locally, add the other repositories now.
     if ('installed' !== $type) {
         $repositories->addRepository(new CompositeRepository($repositoryManager->getRepositories()));
     }
     $repositorySearch = new RepositorySearch($repositories);
     $repositorySearch->setSatisfactionThreshold($threshold);
     if (false !== strpos($keywords, '/')) {
         $repositorySearch->disableSearchType(RepositoryInterface::SEARCH_FULLTEXT);
     } else {
         $repositorySearch->disableSearchType(RepositoryInterface::SEARCH_NAME);
     }
     $searcher = new CompositeSearch([$repositorySearch]);
     $searcher->setSatisfactionThreshold($threshold);
     return $searcher;
 }
Ejemplo n.º 23
0
 /**
  * Runs Composer Update command.
  *
  * @param  array|bool $updates
  * @param array $refresh
  * @param bool $packagist
  * @param bool $preferSource
  * @return bool
  * @throws \Exception
  */
 protected function composerUpdate($updates = false, $refresh = [], $packagist = false, $preferSource = false)
 {
     $installed = new JsonFile($this->paths['path.vendor'] . '/composer/installed.json');
     $internal = new CompositeRepository([]);
     $internal->addRepository(new InstalledFilesystemRepository($installed));
     $composer = $this->getComposer($packagist);
     $composer->getDownloadManager()->setOutputProgress(false);
     $local = $composer->getRepositoryManager()->getLocalRepository();
     foreach ($refresh as $package) {
         $local->removePackage($package);
     }
     $installer = Installer::create($this->getIO(), $composer)->setAdditionalInstalledRepository($internal)->setOptimizeAutoloader(true)->setUpdate(true);
     if ($preferSource) {
         $installer->setPreferSource(true);
     } else {
         $installer->setPreferDist(true);
     }
     if ($updates) {
         $installer->setUpdateWhitelist($updates)->setWhitelistDependencies();
     }
     $installer->run();
 }
Ejemplo n.º 24
0
 public function run()
 {
     if ($this->dryRun) {
         $this->verbose = true;
         $this->runScripts = false;
         $this->installationManager->addInstaller(new NoopInstaller());
         $this->mockLocalRepositories($this->repositoryManager);
     }
     $devRepo = new InstalledFilesystemRepository(new JsonFile($this->config->get('vendor-dir') . '/composer/installed_dev.json'));
     if ($devRepo->getPackages()) {
         $this->io->write('<warning>BC Notice: Removing old dev packages to migrate to the new require-dev handling.</warning>');
         foreach ($devRepo->getPackages() as $package) {
             if ($this->installationManager->isPackageInstalled($devRepo, $package)) {
                 $this->installationManager->uninstall($devRepo, new UninstallOperation($package));
             }
         }
         unlink($this->config->get('vendor-dir') . '/composer/installed_dev.json');
     }
     unset($devRepo, $package);
     if ($this->runScripts) {
         $eventName = $this->update ? ScriptEvents::PRE_UPDATE_CMD : ScriptEvents::PRE_INSTALL_CMD;
         $this->eventDispatcher->dispatchCommandEvent($eventName, $this->devMode);
     }
     $this->downloadManager->setPreferSource($this->preferSource);
     $this->downloadManager->setPreferDist($this->preferDist);
     $installedRootPackage = clone $this->package;
     $installedRootPackage->setRequires(array());
     $installedRootPackage->setDevRequires(array());
     $localRepo = $this->repositoryManager->getLocalRepository();
     $platformRepo = new PlatformRepository();
     $repos = array($localRepo, new InstalledArrayRepository(array($installedRootPackage)), $platformRepo);
     $installedRepo = new CompositeRepository($repos);
     if ($this->additionalInstalledRepository) {
         $installedRepo->addRepository($this->additionalInstalledRepository);
     }
     $aliases = $this->getRootAliases();
     $this->aliasPlatformPackages($platformRepo, $aliases);
     try {
         $this->suggestedPackages = array();
         $res = $this->doInstall($localRepo, $installedRepo, $platformRepo, $aliases, $this->devMode);
         if ($res !== 0) {
             return $res;
         }
     } catch (\Exception $e) {
         $this->installationManager->notifyInstalls();
         throw $e;
     }
     $this->installationManager->notifyInstalls();
     foreach ($this->suggestedPackages as $suggestion) {
         $target = $suggestion['target'];
         foreach ($installedRepo->getPackages() as $package) {
             if (in_array($target, $package->getNames())) {
                 continue 2;
             }
         }
         $this->io->write($suggestion['source'] . ' suggests installing ' . $suggestion['target'] . ' (' . $suggestion['reason'] . ')');
     }
     if (!$this->dryRun) {
         if ($this->update || !$this->locker->isLocked()) {
             $localRepo->reload();
             $devPackages = $this->devMode || !$this->package->getDevRequires() ? array() : null;
             if ($this->devMode && $this->package->getDevRequires()) {
                 $policy = $this->createPolicy();
                 $pool = $this->createPool(true);
                 $pool->addRepository($installedRepo, $aliases);
                 $request = $this->createRequest($pool, $this->package, $platformRepo);
                 $request->updateAll();
                 foreach ($this->package->getRequires() as $link) {
                     $request->install($link->getTarget(), $link->getConstraint());
                 }
                 $solver = new Solver($policy, $pool, $installedRepo);
                 $ops = $solver->solve($request);
                 foreach ($ops as $op) {
                     if ($op->getJobType() === 'uninstall') {
                         $devPackages[] = $op->getPackage();
                     }
                 }
             }
             $platformReqs = $this->extractPlatformRequirements($this->package->getRequires());
             $platformDevReqs = $this->devMode ? $this->extractPlatformRequirements($this->package->getDevRequires()) : array();
             $updatedLock = $this->locker->setLockData(array_diff($localRepo->getCanonicalPackages(), (array) $devPackages), $devPackages, $platformReqs, $platformDevReqs, $aliases, $this->package->getMinimumStability(), $this->package->getStabilityFlags());
             if ($updatedLock) {
                 $this->io->write('<info>Writing lock file</info>');
             }
         }
         if ($this->optimizeAutoloader) {
             $this->io->write('<info>Generating optimized autoload files</info>');
         } else {
             $this->io->write('<info>Generating autoload files</info>');
         }
         $this->autoloadGenerator->dump($this->config, $localRepo, $this->package, $this->installationManager, 'composer', $this->optimizeAutoloader);
         if ($this->runScripts) {
             $eventName = $this->update ? ScriptEvents::POST_UPDATE_CMD : ScriptEvents::POST_INSTALL_CMD;
             $this->eventDispatcher->dispatchCommandEvent($eventName, $this->devMode);
         }
     }
     return 0;
 }
Ejemplo n.º 25
0
 /**
  * Run installation (or update)
  */
 public function run()
 {
     if ($this->dryRun) {
         $this->verbose = true;
     }
     if ($this->preferSource) {
         $this->downloadManager->setPreferSource(true);
     }
     // create installed repo, this contains all local packages + platform packages (php & extensions)
     $repos = array_merge($this->repositoryManager->getLocalRepositories(), array(new PlatformRepository()));
     $installedRepo = new CompositeRepository($repos);
     if ($this->additionalInstalledRepository) {
         $installedRepo->addRepository($this->additionalInstalledRepository);
     }
     $aliases = $this->aliasPackages();
     if (!$this->dryRun) {
         // dispatch pre event
         $eventName = $this->update ? ScriptEvents::PRE_UPDATE_CMD : ScriptEvents::PRE_INSTALL_CMD;
         $this->eventDispatcher->dispatchCommandEvent($eventName);
     }
     $this->suggestedPackages = array();
     if (!$this->doInstall($this->repositoryManager->getLocalRepository(), $installedRepo, $aliases)) {
         return false;
     }
     if ($this->devMode) {
         if (!$this->doInstall($this->repositoryManager->getLocalDevRepository(), $installedRepo, $aliases, true)) {
             return false;
         }
     }
     // dump suggestions
     foreach ($this->suggestedPackages as $suggestion) {
         $this->io->write($suggestion['source'] . ' suggests installing ' . $suggestion['target'] . ' (' . $suggestion['reason'] . ')');
     }
     if (!$this->dryRun) {
         // write lock
         if ($this->update || !$this->locker->isLocked()) {
             $updatedLock = $this->locker->setLockData($this->repositoryManager->getLocalRepository()->getPackages(), $this->devMode ? $this->repositoryManager->getLocalDevRepository()->getPackages() : null, $aliases);
             if ($updatedLock) {
                 $this->io->write('<info>Writing lock file</info>');
             }
         }
         // write autoloader
         $this->io->write('<info>Generating autoload files</info>');
         $generator = new AutoloadGenerator();
         $localRepos = new CompositeRepository($this->repositoryManager->getLocalRepositories());
         $generator->dump($localRepos, $this->package, $this->installationManager, $this->installationManager->getVendorPath() . '/.composer');
         // dispatch post event
         $eventName = $this->update ? ScriptEvents::POST_UPDATE_CMD : ScriptEvents::POST_INSTALL_CMD;
         $this->eventDispatcher->dispatchCommandEvent($eventName);
     }
     return true;
 }
Ejemplo n.º 26
0
 public function install(IOInterface $io, Composer $composer, EventDispatcher $eventDispatcher, $preferSource = false, $dryRun = false, $verbose = false, $noInstallRecommends = false, $installSuggests = false, $update = false, RepositoryInterface $additionalInstalledRepository = null)
 {
     if ($dryRun) {
         $verbose = true;
     }
     if ($preferSource) {
         $composer->getDownloadManager()->setPreferSource(true);
     }
     // create local repo, this contains all packages that are installed in the local project
     $localRepo = $composer->getRepositoryManager()->getLocalRepository();
     // create installed repo, this contains all local packages + platform packages (php & extensions)
     $installedRepo = new CompositeRepository(array($localRepo, new PlatformRepository()));
     if ($additionalInstalledRepository) {
         $installedRepo->addRepository($additionalInstalledRepository);
     }
     // creating repository pool
     $pool = new Pool();
     $pool->addRepository($installedRepo);
     foreach ($composer->getRepositoryManager()->getRepositories() as $repository) {
         $pool->addRepository($repository);
     }
     // dispatch pre event
     if (!$dryRun) {
         $eventName = $update ? ScriptEvents::PRE_UPDATE_CMD : ScriptEvents::PRE_INSTALL_CMD;
         $eventDispatcher->dispatchCommandEvent($eventName);
     }
     // creating requirements request
     $installFromLock = false;
     $request = new Request($pool);
     if ($update) {
         $io->write('<info>Updating dependencies</info>');
         $installedPackages = $installedRepo->getPackages();
         $links = $this->collectLinks($composer->getPackage(), $noInstallRecommends, $installSuggests);
         $request->updateAll();
         foreach ($links as $link) {
             $request->install($link->getTarget(), $link->getConstraint());
         }
     } elseif ($composer->getLocker()->isLocked()) {
         $installFromLock = true;
         $io->write('<info>Installing from lock file</info>');
         if (!$composer->getLocker()->isFresh()) {
             $io->write('<warning>Your lock file is out of sync with your composer.json, run "composer.phar update" to update dependencies</warning>');
         }
         foreach ($composer->getLocker()->getLockedPackages() as $package) {
             $constraint = new VersionConstraint('=', $package->getVersion());
             $request->install($package->getName(), $constraint);
         }
     } else {
         $io->write('<info>Installing dependencies</info>');
         $links = $this->collectLinks($composer->getPackage(), $noInstallRecommends, $installSuggests);
         foreach ($links as $link) {
             $request->install($link->getTarget(), $link->getConstraint());
         }
     }
     // prepare solver
     $installationManager = $composer->getInstallationManager();
     $policy = new DependencyResolver\DefaultPolicy();
     $solver = new DependencyResolver\Solver($policy, $pool, $installedRepo);
     // solve dependencies
     $operations = $solver->solve($request);
     // execute operations
     if (!$operations) {
         $io->write('<info>Nothing to install/update</info>');
     }
     // force dev packages to be updated to latest reference on update
     if ($update) {
         foreach ($localRepo->getPackages() as $package) {
             // skip non-dev packages
             if (!$package->isDev()) {
                 continue;
             }
             // skip packages that will be updated/uninstalled
             foreach ($operations as $operation) {
                 if ('update' === $operation->getJobType() && $package === $operation->getInitialPackage() || 'uninstall' === $operation->getJobType() && $package === $operation->getPackage()) {
                     continue 2;
                 }
             }
             // force update
             $newPackage = $composer->getRepositoryManager()->findPackage($package->getName(), $package->getVersion());
             if ($newPackage && $newPackage->getSourceReference() !== $package->getSourceReference()) {
                 $operations[] = new UpdateOperation($package, $newPackage);
             }
         }
     }
     foreach ($operations as $operation) {
         if ($verbose) {
             $io->write((string) $operation);
         }
         if (!$dryRun) {
             $eventDispatcher->dispatchPackageEvent(constant('Composer\\Script\\ScriptEvents::PRE_PACKAGE_' . strtoupper($operation->getJobType())), $operation);
             // if installing from lock, restore dev packages' references to their locked state
             if ($installFromLock) {
                 $package = null;
                 if ('update' === $operation->getJobType()) {
                     $package = $operation->getTargetPackage();
                 } elseif ('install' === $operation->getJobType()) {
                     $package = $operation->getPackage();
                 }
                 if ($package && $package->isDev()) {
                     $lockData = $composer->getLocker()->getLockData();
                     foreach ($lockData['packages'] as $lockedPackage) {
                         if (!empty($lockedPackage['source-reference']) && strtolower($lockedPackage['package']) === $package->getName()) {
                             $package->setSourceReference($lockedPackage['source-reference']);
                             break;
                         }
                     }
                 }
             }
             $installationManager->execute($operation);
             $eventDispatcher->dispatchPackageEvent(constant('Composer\\Script\\ScriptEvents::POST_PACKAGE_' . strtoupper($operation->getJobType())), $operation);
         }
     }
     if (!$dryRun) {
         if ($update || !$composer->getLocker()->isLocked()) {
             $composer->getLocker()->lockPackages($localRepo->getPackages());
             $io->write('<info>Writing lock file</info>');
         }
         $localRepo->write();
         $io->write('<info>Generating autoload files</info>');
         $generator = new AutoloadGenerator();
         $generator->dump($localRepo, $composer->getPackage(), $installationManager, $installationManager->getVendorPath() . '/.composer');
         // dispatch post event
         $eventName = $update ? ScriptEvents::POST_UPDATE_CMD : ScriptEvents::POST_INSTALL_CMD;
         $eventDispatcher->dispatchCommandEvent($eventName);
     }
 }
Ejemplo n.º 27
0
 /**
  * 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;
 }