示例#1
0
 protected function doInstall($localRepo, $installedRepo, $aliases, $devMode = false)
 {
     $minimumStability = $this->package->getMinimumStability();
     $stabilityFlags = $this->package->getStabilityFlags();
     // initialize locker to create aliased packages
     if (!$this->update && $this->locker->isLocked($devMode)) {
         $lockedPackages = $this->locker->getLockedPackages($devMode);
         $minimumStability = $this->locker->getMinimumStability();
         $stabilityFlags = $this->locker->getStabilityFlags();
     }
     $this->whitelistUpdateDependencies($localRepo, $devMode, $this->package->getRequires(), $this->package->getDevRequires());
     $this->io->write('<info>Loading composer repositories with package information</info>');
     // creating repository pool
     $pool = new Pool($minimumStability, $stabilityFlags);
     $pool->addRepository($installedRepo);
     foreach ($this->repositoryManager->getRepositories() as $repository) {
         $pool->addRepository($repository);
     }
     // creating requirements request
     $installFromLock = false;
     $request = new Request($pool);
     $constraint = new VersionConstraint('=', $this->package->getVersion());
     $request->install($this->package->getName(), $constraint);
     if ($this->update) {
         $this->io->write('<info>Updating ' . ($devMode ? 'dev ' : '') . 'dependencies</info>');
         $request->updateAll();
         $links = $devMode ? $this->package->getDevRequires() : $this->package->getRequires();
         foreach ($links as $link) {
             $request->install($link->getTarget(), $link->getConstraint());
         }
     } elseif ($this->locker->isLocked($devMode)) {
         $installFromLock = true;
         $this->io->write('<info>Installing ' . ($devMode ? 'dev ' : '') . 'dependencies from lock file</info>');
         if (!$this->locker->isFresh() && !$devMode) {
             $this->io->write('<warning>Your lock file is out of sync with your composer.json, run "composer.phar update" to update dependencies</warning>');
         }
         foreach ($lockedPackages as $package) {
             $version = $package->getVersion();
             foreach ($aliases as $alias) {
                 if ($alias['package'] === $package->getName() && $alias['version'] === $package->getVersion()) {
                     $version = $alias['alias_normalized'];
                     break;
                 }
             }
             $constraint = new VersionConstraint('=', $version);
             $request->install($package->getName(), $constraint);
         }
     } else {
         $this->io->write('<info>Installing ' . ($devMode ? 'dev ' : '') . 'dependencies</info>');
         $links = $devMode ? $this->package->getDevRequires() : $this->package->getRequires();
         foreach ($links as $link) {
             $request->install($link->getTarget(), $link->getConstraint());
         }
     }
     // fix the version of all installed packages (+ platform) that are not
     // in the current local repo to prevent rogue updates (e.g. non-dev
     // updating when in dev)
     foreach ($installedRepo->getPackages() as $package) {
         if ($package->getRepository() === $localRepo) {
             continue;
         }
         $constraint = new VersionConstraint('=', $package->getVersion());
         $request->install($package->getName(), $constraint);
     }
     // if the updateWhitelist is enabled, packages not in it are also fixed
     // to the version specified in the lock, or their currently installed version
     if ($this->update && $this->updateWhitelist) {
         if ($this->locker->isLocked($devMode)) {
             $currentPackages = $this->locker->getLockedPackages($devMode);
         } else {
             $currentPackages = $installedRepo->getPackages();
         }
         // collect links from composer as well as installed packages
         $candidates = array();
         foreach ($links as $link) {
             $candidates[$link->getTarget()] = true;
         }
         foreach ($localRepo->getPackages() as $package) {
             $candidates[$package->getName()] = true;
         }
         // fix them to the version in lock (or currently installed) if they are not updateable
         foreach ($candidates as $candidate => $dummy) {
             foreach ($currentPackages as $curPackage) {
                 if ($curPackage->getName() === $candidate) {
                     if ($this->isUpdateable($curPackage)) {
                         break;
                     }
                     $constraint = new VersionConstraint('=', $curPackage->getVersion());
                     $request->install($curPackage->getName(), $constraint);
                 }
             }
         }
     }
     // prepare solver
     $policy = new DefaultPolicy();
     $solver = new Solver($policy, $pool, $installedRepo);
     // solve dependencies
     try {
         $operations = $solver->solve($request);
     } catch (SolverProblemsException $e) {
         $this->io->write('<error>Your requirements could not be resolved to an installable set of packages.</error>');
         $this->io->write($e->getMessage());
         return false;
     }
     // force dev packages to be updated if we update or install from a (potentially new) lock
     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() && $operation->getInitialPackage()->equals($package) || 'uninstall' === $operation->getJobType() && $operation->getPackage()->equals($package)) {
                 continue 2;
             }
         }
         // force update to locked version if it does not match the installed version
         if ($installFromLock) {
             $lockData = $this->locker->getLockData();
             unset($lockedReference);
             foreach ($lockData['packages'] as $lockedPackage) {
                 if (!empty($lockedPackage['source-reference']) && strtolower($lockedPackage['package']) === $package->getName()) {
                     $lockedReference = $lockedPackage['source-reference'];
                     break;
                 }
             }
             if (isset($lockedReference) && $lockedReference !== $package->getSourceReference()) {
                 // changing the source ref to update to will be handled in the operations loop below
                 $operations[] = new UpdateOperation($package, clone $package);
             }
         } else {
             // force update to latest on update
             if ($this->update) {
                 // skip package if the whitelist is enabled and it is not in it
                 if ($this->updateWhitelist && !$this->isUpdateable($package)) {
                     continue;
                 }
                 $newPackage = $this->repositoryManager->findPackage($package->getName(), $package->getVersion());
                 if ($newPackage && $newPackage->getSourceReference() !== $package->getSourceReference()) {
                     $operations[] = new UpdateOperation($package, $newPackage);
                 }
             }
             // force installed package to update to referenced version if it does not match the installed version
             $references = $this->package->getReferences();
             if (isset($references[$package->getName()]) && $references[$package->getName()] !== $package->getSourceReference()) {
                 // changing the source ref to update to will be handled in the operations loop below
                 $operations[] = new UpdateOperation($package, clone $package);
             }
         }
     }
     // execute operations
     if (!$operations) {
         $this->io->write('Nothing to install or update');
     }
     foreach ($operations as $operation) {
         // collect suggestions
         if ('install' === $operation->getJobType()) {
             foreach ($operation->getPackage()->getSuggests() as $target => $reason) {
                 $this->suggestedPackages[] = array('source' => $operation->getPackage()->getPrettyName(), 'target' => $target, 'reason' => $reason);
             }
         }
         $event = 'Composer\\Script\\ScriptEvents::PRE_PACKAGE_' . strtoupper($operation->getJobType());
         if (defined($event) && $this->runScripts) {
             $this->eventDispatcher->dispatchPackageEvent(constant($event), $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 = $this->locker->getLockData();
                 foreach ($lockData['packages'] as $lockedPackage) {
                     if (!empty($lockedPackage['source-reference']) && strtolower($lockedPackage['package']) === $package->getName()) {
                         // update commit date to allow recovery in case the commit disappeared
                         if (!empty($lockedPackage['commit-date'])) {
                             $package->setReleaseDate(new \DateTime('@' . $lockedPackage['commit-date']));
                         }
                         $package->setSourceReference($lockedPackage['source-reference']);
                         break;
                     }
                 }
             }
         } else {
             // not installing from lock, force dev packages' references if they're in root package refs
             $package = null;
             if ('update' === $operation->getJobType()) {
                 $package = $operation->getTargetPackage();
             } elseif ('install' === $operation->getJobType()) {
                 $package = $operation->getPackage();
             }
             if ($package && $package->isDev()) {
                 $references = $this->package->getReferences();
                 if (isset($references[$package->getName()])) {
                     $package->setSourceReference($references[$package->getName()]);
                 }
             }
         }
         // output alias operations in verbose mode, or all ops in dry run
         if ($this->dryRun || $this->verbose && false !== strpos($operation->getJobType(), 'Alias')) {
             $this->io->write('  - ' . $operation);
         }
         $this->installationManager->execute($localRepo, $operation);
         $event = 'Composer\\Script\\ScriptEvents::POST_PACKAGE_' . strtoupper($operation->getJobType());
         if (defined($event) && $this->runScripts) {
             $this->eventDispatcher->dispatchPackageEvent(constant($event), $operation);
         }
         if (!$this->dryRun) {
             $localRepo->write();
         }
     }
     return true;
 }
示例#2
0
 protected function doInstall($localRepo, $installedRepo, $aliases, $devMode = false)
 {
     // creating repository pool
     $pool = new Pool();
     $pool->addRepository($installedRepo);
     foreach ($this->repositoryManager->getRepositories() as $repository) {
         $pool->addRepository($repository);
     }
     // creating requirements request
     $installFromLock = false;
     $request = new Request($pool);
     if ($this->update) {
         $this->io->write('<info>Updating ' . ($devMode ? 'dev ' : '') . 'dependencies</info>');
         $request->updateAll();
         $links = $devMode ? $this->package->getDevRequires() : $this->package->getRequires();
         foreach ($links as $link) {
             $request->install($link->getTarget(), $link->getConstraint());
         }
     } elseif ($this->locker->isLocked($devMode)) {
         $installFromLock = true;
         $this->io->write('<info>Installing ' . ($devMode ? 'dev ' : '') . 'dependencies from lock file</info>');
         if (!$this->locker->isFresh() && !$devMode) {
             $this->io->write('<warning>Your lock file is out of sync with your composer.json, run "composer.phar update" to update dependencies</warning>');
         }
         foreach ($this->locker->getLockedPackages($devMode) as $package) {
             $version = $package->getVersion();
             foreach ($aliases as $alias) {
                 if ($alias['package'] === $package->getName() && $alias['version'] === $package->getVersion()) {
                     $version = $alias['alias'];
                     break;
                 }
             }
             $constraint = new VersionConstraint('=', $version);
             $request->install($package->getName(), $constraint);
         }
     } else {
         $this->io->write('<info>Installing ' . ($devMode ? 'dev ' : '') . 'dependencies</info>');
         $links = $devMode ? $this->package->getDevRequires() : $this->package->getRequires();
         foreach ($links as $link) {
             $request->install($link->getTarget(), $link->getConstraint());
         }
     }
     // fix the version all installed packages that are not in the current local repo to prevent rogue updates
     foreach ($installedRepo->getPackages() as $package) {
         if ($package->getRepository() === $localRepo || $package->getRepository() instanceof PlatformRepository) {
             continue;
         }
         $constraint = new VersionConstraint('=', $package->getVersion());
         $request->install($package->getName(), $constraint);
     }
     // prepare solver
     $policy = new DefaultPolicy();
     $solver = new Solver($policy, $pool, $installedRepo);
     // solve dependencies
     try {
         $operations = $solver->solve($request);
     } catch (SolverProblemsException $e) {
         $this->io->write('<error>Your requirements could not be solved to an installable set of packages.</error>');
         $this->io->write($e->getMessage());
         return false;
     }
     // force dev packages to be updated if we update or install from a (potentially new) lock
     if ($this->update || $installFromLock) {
         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() && $operation->getInitialPackage()->equals($package) || 'uninstall' === $operation->getJobType() && $operation->getPackage()->equals($package)) {
                     continue 2;
                 }
             }
             // force update to latest on update
             if ($this->update) {
                 $newPackage = $this->repositoryManager->findPackage($package->getName(), $package->getVersion());
                 if ($newPackage && $newPackage->getSourceReference() !== $package->getSourceReference()) {
                     $operations[] = new UpdateOperation($package, $newPackage);
                 }
             } elseif ($installFromLock) {
                 // force update to locked version if it does not match the installed version
                 $lockData = $this->locker->getLockData();
                 unset($lockedReference);
                 foreach ($lockData['packages'] as $lockedPackage) {
                     if (!empty($lockedPackage['source-reference']) && strtolower($lockedPackage['package']) === $package->getName()) {
                         $lockedReference = $lockedPackage['source-reference'];
                         break;
                     }
                 }
                 if (isset($lockedReference) && $lockedReference !== $package->getSourceReference()) {
                     // changing the source ref to update to will be handled in the operations loop below
                     $operations[] = new UpdateOperation($package, $package);
                 }
             }
         }
     }
     // anti-alias local repository to allow updates to work fine
     foreach ($localRepo->getPackages() as $package) {
         if ($package instanceof AliasPackage) {
             $package->getRepository()->addPackage(clone $package->getAliasOf());
             $package->getRepository()->removePackage($package);
         }
     }
     // execute operations
     if (!$operations) {
         $this->io->write('Nothing to install or update');
     }
     foreach ($operations as $operation) {
         if ($this->verbose) {
             $this->io->write((string) $operation);
         }
         // collect suggestions
         if ('install' === $operation->getJobType()) {
             foreach ($operation->getPackage()->getSuggests() as $target => $reason) {
                 $this->suggestedPackages[] = array('source' => $operation->getPackage()->getPrettyName(), 'target' => $target, 'reason' => $reason);
             }
         }
         if (!$this->dryRun) {
             $this->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 = $this->locker->getLockData();
                     foreach ($lockData['packages'] as $lockedPackage) {
                         if (!empty($lockedPackage['source-reference']) && strtolower($lockedPackage['package']) === $package->getName()) {
                             $package->setSourceReference($lockedPackage['source-reference']);
                             break;
                         }
                     }
                 }
             }
             $this->installationManager->execute($localRepo, $operation);
             $this->eventDispatcher->dispatchPackageEvent(constant('Composer\\Script\\ScriptEvents::POST_PACKAGE_' . strtoupper($operation->getJobType())), $operation);
             $localRepo->write();
         }
     }
     // reload local repository for the dev pass to work ok with aliases since it was anti-aliased above
     if (!$devMode) {
         $localRepo->reload();
     }
     return true;
 }
示例#3
0
 /**
  * Find the dev-master package.
  *
  * @param RepositoryManager $manager Composer repository manger
  * @param string            $name    Package's name
  *
  * @return PackageInterface The found package
  */
 protected function findDevPackage(RepositoryManager $manager, $name)
 {
     return $manager->findPackage($name, '9999999-dev');
 }
示例#4
0
 /**
  * Run installation (or update)
  *
  * @param Boolean $preferSource
  * @param Boolean $dryRun
  * @param Boolean $verbose
  * @param Boolean $noInstallRecommends
  * @param Boolean $installSuggests
  * @param Boolean $update
  * @param RepositoryInterface $additionalInstalledRepository
  */
 public function run($preferSource = false, $dryRun = false, $verbose = false, $noInstallRecommends = false, $installSuggests = false, $update = false, RepositoryInterface $additionalInstalledRepository = null)
 {
     if ($dryRun) {
         $verbose = true;
     }
     if ($preferSource) {
         $this->downloadManager->setPreferSource(true);
     }
     $this->repositoryManager = $this->repositoryManager;
     // create local repo, this contains all packages that are installed in the local project
     $localRepo = $this->repositoryManager->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);
     }
     // prepare aliased packages
     if (!$update && $this->locker->isLocked()) {
         $aliases = $this->locker->getAliases();
     } else {
         $aliases = $this->package->getAliases();
     }
     foreach ($aliases as $alias) {
         foreach ($this->repositoryManager->findPackages($alias['package'], $alias['version']) as $package) {
             $package->getRepository()->addPackage(new AliasPackage($package, $alias['alias_normalized'], $alias['alias']));
         }
         foreach ($this->repositoryManager->getLocalRepository()->findPackages($alias['package'], $alias['version']) as $package) {
             $this->repositoryManager->getLocalRepository()->addPackage(new AliasPackage($package, $alias['alias_normalized'], $alias['alias']));
             $this->repositoryManager->getLocalRepository()->removePackage($package);
         }
     }
     // creating repository pool
     $pool = new Pool();
     $pool->addRepository($installedRepo);
     foreach ($this->repositoryManager->getRepositories() as $repository) {
         $pool->addRepository($repository);
     }
     // dispatch pre event
     if (!$dryRun) {
         $eventName = $update ? ScriptEvents::PRE_UPDATE_CMD : ScriptEvents::PRE_INSTALL_CMD;
         $this->eventDispatcher->dispatchCommandEvent($eventName);
     }
     // creating requirements request
     $installFromLock = false;
     $request = new Request($pool);
     if ($update) {
         $this->io->write('<info>Updating dependencies</info>');
         $request->updateAll();
         $links = $this->collectLinks($noInstallRecommends, $installSuggests);
         foreach ($links as $link) {
             $request->install($link->getTarget(), $link->getConstraint());
         }
     } elseif ($this->locker->isLocked()) {
         $installFromLock = true;
         $this->io->write('<info>Installing from lock file</info>');
         if (!$this->locker->isFresh()) {
             $this->io->write('<warning>Your lock file is out of sync with your composer.json, run "composer.phar update" to update dependencies</warning>');
         }
         foreach ($this->locker->getLockedPackages() as $package) {
             $version = $package->getVersion();
             foreach ($aliases as $alias) {
                 if ($alias['package'] === $package->getName() && $alias['version'] === $package->getVersion()) {
                     $version = $alias['alias'];
                     break;
                 }
             }
             $constraint = new VersionConstraint('=', $version);
             $request->install($package->getName(), $constraint);
         }
     } else {
         $this->io->write('<info>Installing dependencies</info>');
         $links = $this->collectLinks($noInstallRecommends, $installSuggests);
         foreach ($links as $link) {
             $request->install($link->getTarget(), $link->getConstraint());
         }
     }
     // prepare solver
     $policy = new DefaultPolicy();
     $solver = new Solver($policy, $pool, $installedRepo);
     // solve dependencies
     $operations = $solver->solve($request);
     // force dev packages to be updated to latest reference on update
     if ($update) {
         foreach ($localRepo->getPackages() as $package) {
             if ($package instanceof AliasPackage) {
                 $package = $package->getAliasOf();
             }
             // 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 = $this->repositoryManager->findPackage($package->getName(), $package->getVersion());
             if ($newPackage && $newPackage->getSourceReference() !== $package->getSourceReference()) {
                 $operations[] = new UpdateOperation($package, $newPackage);
             }
         }
     }
     // anti-alias local repository to allow updates to work fine
     foreach ($this->repositoryManager->getLocalRepository()->getPackages() as $package) {
         if ($package instanceof AliasPackage) {
             $this->repositoryManager->getLocalRepository()->addPackage(clone $package->getAliasOf());
             $this->repositoryManager->getLocalRepository()->removePackage($package);
         }
     }
     // execute operations
     if (!$operations) {
         $this->io->write('<info>Nothing to install/update</info>');
     }
     foreach ($operations as $operation) {
         if ($verbose) {
             $this->io->write((string) $operation);
         }
         if (!$dryRun) {
             $this->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 = $this->locker->getLockData();
                     foreach ($lockData['packages'] as $lockedPackage) {
                         if (!empty($lockedPackage['source-reference']) && strtolower($lockedPackage['package']) === $package->getName()) {
                             $package->setSourceReference($lockedPackage['source-reference']);
                             break;
                         }
                     }
                 }
             }
             $this->installationManager->execute($operation);
             $this->eventDispatcher->dispatchPackageEvent(constant('Composer\\Script\\ScriptEvents::POST_PACKAGE_' . strtoupper($operation->getJobType())), $operation);
         }
     }
     if (!$dryRun) {
         if ($update || !$this->locker->isLocked()) {
             $this->locker->setLockData($localRepo->getPackages(), $aliases);
             $this->io->write('<info>Writing lock file</info>');
         }
         $localRepo->write();
         $this->io->write('<info>Generating autoload files</info>');
         $generator = new AutoloadGenerator();
         $generator->dump($localRepo, $this->package, $this->installationManager, $this->installationManager->getVendorPath() . '/.composer');
         // dispatch post event
         $eventName = $update ? ScriptEvents::POST_UPDATE_CMD : ScriptEvents::POST_INSTALL_CMD;
         $this->eventDispatcher->dispatchCommandEvent($eventName);
     }
 }