1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261 |
- <?php
- /*
- * This file is part of Composer.
- *
- * (c) Nils Adermann <naderman@naderman.de>
- * Jordi Boggiano <j.boggiano@seld.be>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Composer;
- use Composer\Autoload\AutoloadGenerator;
- use Composer\DependencyResolver\DefaultPolicy;
- use Composer\DependencyResolver\Operation\UpdateOperation;
- use Composer\DependencyResolver\Operation\InstallOperation;
- use Composer\DependencyResolver\Operation\UninstallOperation;
- use Composer\DependencyResolver\Operation\OperationInterface;
- use Composer\DependencyResolver\Pool;
- use Composer\DependencyResolver\Request;
- use Composer\DependencyResolver\Rule;
- use Composer\DependencyResolver\Solver;
- use Composer\DependencyResolver\SolverProblemsException;
- use Composer\Downloader\DownloadManager;
- use Composer\EventDispatcher\EventDispatcher;
- use Composer\Installer\InstallationManager;
- use Composer\Installer\InstallerEvents;
- use Composer\Installer\NoopInstaller;
- use Composer\IO\IOInterface;
- use Composer\Json\JsonFile;
- use Composer\Package\AliasPackage;
- use Composer\Package\CompletePackage;
- use Composer\Package\Link;
- use Composer\Package\LinkConstraint\VersionConstraint;
- use Composer\Package\Locker;
- use Composer\Package\PackageInterface;
- use Composer\Package\RootPackageInterface;
- use Composer\Repository\CompositeRepository;
- use Composer\Repository\InstalledArrayRepository;
- use Composer\Repository\InstalledFilesystemRepository;
- use Composer\Repository\PlatformRepository;
- use Composer\Repository\RepositoryInterface;
- use Composer\Repository\RepositoryManager;
- use Composer\Script\ScriptEvents;
- /**
- * @author Jordi Boggiano <j.boggiano@seld.be>
- * @author Beau Simensen <beau@dflydev.com>
- * @author Konstantin Kudryashov <ever.zet@gmail.com>
- * @author Nils Adermann <naderman@naderman.de>
- */
- class Installer
- {
- /**
- * @var IOInterface
- */
- protected $io;
- /**
- * @var Config
- */
- protected $config;
- /**
- * @var RootPackageInterface
- */
- protected $package;
- /**
- * @var DownloadManager
- */
- protected $downloadManager;
- /**
- * @var RepositoryManager
- */
- protected $repositoryManager;
- /**
- * @var Locker
- */
- protected $locker;
- /**
- * @var InstallationManager
- */
- protected $installationManager;
- /**
- * @var EventDispatcher
- */
- protected $eventDispatcher;
- /**
- * @var AutoloadGenerator
- */
- protected $autoloadGenerator;
- protected $preferSource = false;
- protected $preferDist = false;
- protected $optimizeAutoloader = false;
- protected $devMode = false;
- protected $dryRun = false;
- protected $verbose = false;
- protected $update = false;
- protected $runScripts = true;
- protected $ignorePlatformReqs = false;
- /**
- * Array of package names/globs flagged for update
- *
- * @var array|null
- */
- protected $updateWhitelist = null;
- protected $whitelistDependencies = false;
- /**
- * @var array
- */
- protected $suggestedPackages;
- /**
- * @var RepositoryInterface
- */
- protected $additionalInstalledRepository;
- /**
- * Constructor
- *
- * @param IOInterface $io
- * @param Config $config
- * @param RootPackageInterface $package
- * @param DownloadManager $downloadManager
- * @param RepositoryManager $repositoryManager
- * @param Locker $locker
- * @param InstallationManager $installationManager
- * @param EventDispatcher $eventDispatcher
- * @param AutoloadGenerator $autoloadGenerator
- */
- public function __construct(IOInterface $io, Config $config, RootPackageInterface $package, DownloadManager $downloadManager, RepositoryManager $repositoryManager, Locker $locker, InstallationManager $installationManager, EventDispatcher $eventDispatcher, AutoloadGenerator $autoloadGenerator)
- {
- $this->io = $io;
- $this->config = $config;
- $this->package = $package;
- $this->downloadManager = $downloadManager;
- $this->repositoryManager = $repositoryManager;
- $this->locker = $locker;
- $this->installationManager = $installationManager;
- $this->eventDispatcher = $eventDispatcher;
- $this->autoloadGenerator = $autoloadGenerator;
- }
- /**
- * Run installation (or update)
- *
- * @return int 0 on success or a positive error code on failure
- *
- * @throws \Exception
- */
- public function run()
- {
- 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->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->runScripts) {
- // dispatch pre event
- $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);
- // 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->write($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->write(
- 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, $policy, $pool, $installedRepo, $request);
- $solver = new Solver($policy, $pool, $installedRepo);
- $ops = $solver->solve($request, $this->ignorePlatformReqs);
- $this->eventDispatcher->dispatchInstallerEvent(InstallerEvents::POST_DEPENDENCIES_SOLVING, $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->package->getPreferStable()
- );
- if ($updatedLock) {
- $this->io->write('<info>Writing lock file</info>');
- }
- }
- // write autoloader
- if ($this->optimizeAutoloader) {
- $this->io->write('<info>Generating optimized autoload files</info>');
- } else {
- $this->io->write('<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->dispatchCommandEvent($eventName, $this->devMode);
- }
- $vendorDir = $this->config->get('vendor-dir');
- if (is_dir($vendorDir)) {
- touch($vendorDir);
- }
- }
- return 0;
- }
- protected function doInstall($localRepo, $installedRepo, $platformRepo, $aliases, $withDevReqs)
- {
- // init vars
- $lockedRepository = null;
- $repositories = null;
- // initialize locker to create aliased packages
- $installFromLock = false;
- if (!$this->update && $this->locker->isLocked()) {
- $installFromLock = true;
- try {
- $lockedRepository = $this->locker->getLockedRepository($withDevReqs);
- } catch (\RuntimeException $e) {
- // if there are dev requires, then we really can not install
- if ($this->package->getDevRequires()) {
- throw $e;
- }
- // no require-dev in composer.json and the lock file was created with no dev info, so skip them
- $lockedRepository = $this->locker->getLockedRepository();
- }
- }
- $this->whitelistUpdateDependencies(
- $localRepo,
- $withDevReqs,
- $this->package->getRequires(),
- $this->package->getDevRequires()
- );
- $this->io->write('<info>Loading composer repositories with package information</info>');
- // creating repository pool
- $policy = $this->createPolicy();
- $pool = $this->createPool($withDevReqs);
- $pool->addRepository($installedRepo, $aliases);
- if ($installFromLock) {
- $pool->addRepository($lockedRepository, $aliases);
- }
- if (!$installFromLock) {
- $repositories = $this->repositoryManager->getRepositories();
- foreach ($repositories as $repository) {
- $pool->addRepository($repository, $aliases);
- }
- }
- // creating requirements request
- $request = $this->createRequest($pool, $this->package, $platformRepo);
- if (!$installFromLock) {
- // remove unstable packages from the localRepo if they don't match the current stability settings
- $removedUnstablePackages = array();
- foreach ($localRepo->getPackages() as $package) {
- if (
- !$pool->isPackageAcceptable($package->getNames(), $package->getStability())
- && $this->installationManager->isPackageInstalled($localRepo, $package)
- ) {
- $removedUnstablePackages[$package->getName()] = true;
- $request->remove($package->getName(), new VersionConstraint('=', $package->getVersion()));
- }
- }
- }
- if ($this->update) {
- $this->io->write('<info>Updating dependencies'.($withDevReqs ? ' (including require-dev)' : '').'</info>');
- $request->updateAll();
- if ($withDevReqs) {
- $links = array_merge($this->package->getRequires(), $this->package->getDevRequires());
- } else {
- $links = $this->package->getRequires();
- }
- foreach ($links as $link) {
- $request->install($link->getTarget(), $link->getConstraint());
- }
- // 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->updateWhitelist) {
- if ($this->locker->isLocked()) {
- try {
- $currentPackages = $this->locker->getLockedRepository($withDevReqs)->getPackages();
- } catch (\RuntimeException $e) {
- // fetch only non-dev packages from lock if doing a dev update fails due to a previously incomplete lock file
- $currentPackages = $this->locker->getLockedRepository()->getPackages();
- }
- } else {
- $currentPackages = $installedRepo->getPackages();
- }
- // collect packages to fixate from root requirements 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) && !isset($removedUnstablePackages[$curPackage->getName()])) {
- $constraint = new VersionConstraint('=', $curPackage->getVersion());
- $request->install($curPackage->getName(), $constraint);
- }
- break;
- }
- }
- }
- }
- } elseif ($installFromLock) {
- $this->io->write('<info>Installing dependencies'.($withDevReqs ? ' (including require-dev)' : '').' from lock file</info>');
- if (!$this->locker->isFresh()) {
- $this->io->write('<warning>Warning: The lock file is not up to date with the latest changes in composer.json. You may be getting outdated dependencies. Run update to update them.</warning>');
- }
- foreach ($lockedRepository->getPackages() as $package) {
- $version = $package->getVersion();
- if (isset($aliases[$package->getName()][$version])) {
- $version = $aliases[$package->getName()][$version]['alias_normalized'];
- }
- $constraint = new VersionConstraint('=', $version);
- $constraint->setPrettyString($package->getPrettyVersion());
- $request->install($package->getName(), $constraint);
- }
- foreach ($this->locker->getPlatformRequirements($withDevReqs) as $link) {
- $request->install($link->getTarget(), $link->getConstraint());
- }
- } else {
- $this->io->write('<info>Installing dependencies'.($withDevReqs ? ' (including require-dev)' : '').'</info>');
- if ($withDevReqs) {
- $links = array_merge($this->package->getRequires(), $this->package->getDevRequires());
- } else {
- $links = $this->package->getRequires();
- }
- foreach ($links as $link) {
- $request->install($link->getTarget(), $link->getConstraint());
- }
- }
- // force dev packages to have the latest links if we update or install from a (potentially new) lock
- $this->processDevPackages($localRepo, $pool, $policy, $repositories, $lockedRepository, $installFromLock, 'force-links');
- // solve dependencies
- $this->eventDispatcher->dispatchInstallerEvent(InstallerEvents::PRE_DEPENDENCIES_SOLVING, $policy, $pool, $installedRepo, $request);
- $solver = new Solver($policy, $pool, $installedRepo);
- try {
- $operations = $solver->solve($request, $this->ignorePlatformReqs);
- $this->eventDispatcher->dispatchInstallerEvent(InstallerEvents::POST_DEPENDENCIES_SOLVING, $policy, $pool, $installedRepo, $request, $operations);
- } 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 max(1, $e->getCode());
- }
- // force dev packages to be updated if we update or install from a (potentially new) lock
- $operations = $this->processDevPackages($localRepo, $pool, $policy, $repositories, $lockedRepository, $installFromLock, 'force-updates', $operations);
- // execute operations
- if (!$operations) {
- $this->io->write('Nothing to install or update');
- }
- $operations = $this->movePluginsToFront($operations);
- $operations = $this->moveUninstallsToFront($operations);
- 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,
- );
- }
- }
- // not installing from lock, force dev packages' references if they're in root package refs
- if (!$installFromLock) {
- $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()]);
- $package->setDistReference($references[$package->getName()]);
- }
- }
- if ('update' === $operation->getJobType()
- && $operation->getTargetPackage()->isDev()
- && $operation->getTargetPackage()->getVersion() === $operation->getInitialPackage()->getVersion()
- && $operation->getTargetPackage()->getSourceReference() === $operation->getInitialPackage()->getSourceReference()
- ) {
- if ($this->io->isDebug()) {
- $this->io->write(' - Skipping update of '. $operation->getTargetPackage()->getPrettyName().' to the same reference-locked version');
- $this->io->write('');
- }
- continue;
- }
- }
- $event = 'Composer\Script\ScriptEvents::PRE_PACKAGE_'.strtoupper($operation->getJobType());
- if (defined($event) && $this->runScripts) {
- $this->eventDispatcher->dispatchPackageEvent(constant($event), $this->devMode, $operation);
- }
- // output non-alias ops in dry run, output alias ops in debug verbosity
- if ($this->dryRun && false === strpos($operation->getJobType(), 'Alias')) {
- $this->io->write(' - ' . $operation);
- $this->io->write('');
- } elseif ($this->io->isDebug() && false !== strpos($operation->getJobType(), 'Alias')) {
- $this->io->write(' - ' . $operation);
- $this->io->write('');
- }
- $this->installationManager->execute($localRepo, $operation);
- // output reasons why the operation was ran, only for install/update operations
- if ($this->verbose && $this->io->isVeryVerbose() && in_array($operation->getJobType(), array('install', 'update'))) {
- $reason = $operation->getReason();
- if ($reason instanceof Rule) {
- switch ($reason->getReason()) {
- case Rule::RULE_JOB_INSTALL:
- $this->io->write(' REASON: Required by root: '.$reason->getPrettyString($pool));
- $this->io->write('');
- break;
- case Rule::RULE_PACKAGE_REQUIRES:
- $this->io->write(' REASON: '.$reason->getPrettyString($pool));
- $this->io->write('');
- break;
- }
- }
- }
- $event = 'Composer\Script\ScriptEvents::POST_PACKAGE_'.strtoupper($operation->getJobType());
- if (defined($event) && $this->runScripts) {
- $this->eventDispatcher->dispatchPackageEvent(constant($event), $this->devMode, $operation);
- }
- if (!$this->dryRun) {
- $localRepo->write();
- }
- }
- return 0;
- }
- /**
- * Workaround: if your packages depend on plugins, we must be sure
- * that those are installed / updated first; else it would lead to packages
- * being installed multiple times in different folders, when running Composer
- * twice.
- *
- * While this does not fix the root-causes of https://github.com/composer/composer/issues/1147,
- * it at least fixes the symptoms and makes usage of composer possible (again)
- * in such scenarios.
- *
- * @param OperationInterface[] $operations
- * @return OperationInterface[] reordered operation list
- */
- private function movePluginsToFront(array $operations)
- {
- $installerOps = array();
- foreach ($operations as $idx => $op) {
- if ($op instanceof InstallOperation) {
- $package = $op->getPackage();
- } elseif ($op instanceof UpdateOperation) {
- $package = $op->getTargetPackage();
- } else {
- continue;
- }
- if ($package->getType() === 'composer-plugin' || $package->getType() === 'composer-installer') {
- // ignore requirements to platform or composer-plugin-api
- $requires = array_keys($package->getRequires());
- foreach ($requires as $index => $req) {
- if ($req === 'composer-plugin-api' || preg_match(PlatformRepository::PLATFORM_PACKAGE_REGEX, $req)) {
- unset($requires[$index]);
- }
- }
- // if there are no other requirements, move the plugin to the top of the op list
- if (!count($requires)) {
- $installerOps[] = $op;
- unset($operations[$idx]);
- }
- }
- }
- return array_merge($installerOps, $operations);
- }
- /**
- * Removals of packages should be executed before installations in
- * case two packages resolve to the same path (due to custom installers)
- *
- * @param OperationInterface[] $operations
- * @return OperationInterface[] reordered operation list
- */
- private function moveUninstallsToFront(array $operations)
- {
- $uninstOps = array();
- foreach ($operations as $idx => $op) {
- if ($op instanceof UninstallOperation) {
- $uninstOps[] = $op;
- unset($operations[$idx]);
- }
- }
- return array_merge($uninstOps, $operations);
- }
- private function createPool($withDevReqs)
- {
- $minimumStability = $this->package->getMinimumStability();
- $stabilityFlags = $this->package->getStabilityFlags();
- if (!$this->update && $this->locker->isLocked()) {
- $minimumStability = $this->locker->getMinimumStability();
- $stabilityFlags = $this->locker->getStabilityFlags();
- }
- $requires = $this->package->getRequires();
- if ($withDevReqs) {
- $requires = array_merge($requires, $this->package->getDevRequires());
- }
- $rootConstraints = array();
- foreach ($requires as $req => $constraint) {
- // skip platform requirements from the root package to avoid filtering out existing platform packages
- if ($this->ignorePlatformReqs && preg_match(PlatformRepository::PLATFORM_PACKAGE_REGEX, $req)) {
- continue;
- }
- $rootConstraints[$req] = $constraint->getConstraint();
- }
- return new Pool($minimumStability, $stabilityFlags, $rootConstraints);
- }
- private function createPolicy()
- {
- $preferStable = null;
- if (!$this->update && $this->locker->isLocked()) {
- $preferStable = $this->locker->getPreferStable();
- }
- // old lock file without prefer stable will return null
- // so in this case we use the composer.json info
- if (null === $preferStable) {
- $preferStable = $this->package->getPreferStable();
- }
- return new DefaultPolicy($preferStable);
- }
- private function createRequest(Pool $pool, RootPackageInterface $rootPackage, PlatformRepository $platformRepo)
- {
- $request = new Request($pool);
- $constraint = new VersionConstraint('=', $rootPackage->getVersion());
- $constraint->setPrettyString($rootPackage->getPrettyVersion());
- $request->install($rootPackage->getName(), $constraint);
- $fixedPackages = $platformRepo->getPackages();
- if ($this->additionalInstalledRepository) {
- $additionalFixedPackages = $this->additionalInstalledRepository->getPackages();
- $fixedPackages = array_merge($fixedPackages, $additionalFixedPackages);
- }
- // fix the version of all platform packages + additionally installed packages
- // to prevent the solver trying to remove or update those
- $provided = $rootPackage->getProvides();
- foreach ($fixedPackages as $package) {
- $constraint = new VersionConstraint('=', $package->getVersion());
- $constraint->setPrettyString($package->getPrettyVersion());
- // skip platform packages that are provided by the root package
- if ($package->getRepository() !== $platformRepo
- || !isset($provided[$package->getName()])
- || !$provided[$package->getName()]->getConstraint()->matches($constraint)
- ) {
- $request->fix($package->getName(), $constraint);
- }
- }
- return $request;
- }
- private function processDevPackages($localRepo, $pool, $policy, $repositories, $lockedRepository, $installFromLock, $task, array $operations = null)
- {
- if ($task === 'force-updates' && null === $operations) {
- throw new \InvalidArgumentException('Missing operations argument');
- }
- if ($task === 'force-links') {
- $operations = array();
- }
- foreach ($localRepo->getCanonicalPackages() 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) {
- foreach ($lockedRepository->findPackages($package->getName()) as $lockedPackage) {
- if ($lockedPackage->isDev() && $lockedPackage->getVersion() === $package->getVersion()) {
- if ($task === 'force-links') {
- $package->setRequires($lockedPackage->getRequires());
- $package->setConflicts($lockedPackage->getConflicts());
- $package->setProvides($lockedPackage->getProvides());
- $package->setReplaces($lockedPackage->getReplaces());
- } elseif ($task === 'force-updates') {
- if (($lockedPackage->getSourceReference() && $lockedPackage->getSourceReference() !== $package->getSourceReference())
- || ($lockedPackage->getDistReference() && $lockedPackage->getDistReference() !== $package->getDistReference())
- ) {
- $operations[] = new UpdateOperation($package, $lockedPackage);
- }
- }
- break;
- }
- }
- } 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;
- }
- // find similar packages (name/version) in all repositories
- $matches = $pool->whatProvides($package->getName(), new VersionConstraint('=', $package->getVersion()));
- foreach ($matches as $index => $match) {
- // skip local packages
- if (!in_array($match->getRepository(), $repositories, true)) {
- unset($matches[$index]);
- continue;
- }
- // skip providers/replacers
- if ($match->getName() !== $package->getName()) {
- unset($matches[$index]);
- continue;
- }
- $matches[$index] = $match->getId();
- }
- // select prefered package according to policy rules
- if ($matches && $matches = $policy->selectPreferedPackages($pool, array(), $matches)) {
- $newPackage = $pool->literalToPackage($matches[0]);
- if ($task === 'force-links' && $newPackage) {
- $package->setRequires($newPackage->getRequires());
- $package->setConflicts($newPackage->getConflicts());
- $package->setProvides($newPackage->getProvides());
- $package->setReplaces($newPackage->getReplaces());
- }
- if ($task === 'force-updates' && $newPackage && (
- (($newPackage->getSourceReference() && $newPackage->getSourceReference() !== $package->getSourceReference())
- || ($newPackage->getDistReference() && $newPackage->getDistReference() !== $package->getDistReference())
- )
- )) {
- $operations[] = new UpdateOperation($package, $newPackage);
- }
- }
- }
- if ($task === 'force-updates') {
- // 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);
- }
- }
- }
- }
- return $operations;
- }
- private function getRootAliases()
- {
- if (!$this->update && $this->locker->isLocked()) {
- $aliases = $this->locker->getAliases();
- } else {
- $aliases = $this->package->getAliases();
- }
- $normalizedAliases = array();
- foreach ($aliases as $alias) {
- $normalizedAliases[$alias['package']][$alias['version']] = array(
- 'alias' => $alias['alias'],
- 'alias_normalized' => $alias['alias_normalized']
- );
- }
- return $normalizedAliases;
- }
- private function aliasPlatformPackages(PlatformRepository $platformRepo, $aliases)
- {
- foreach ($aliases as $package => $versions) {
- foreach ($versions as $version => $alias) {
- $packages = $platformRepo->findPackages($package, $version);
- foreach ($packages as $package) {
- $aliasPackage = new AliasPackage($package, $alias['alias_normalized'], $alias['alias']);
- $aliasPackage->setRootPackageAlias(true);
- $platformRepo->addPackage($aliasPackage);
- }
- }
- }
- }
- private function isUpdateable(PackageInterface $package)
- {
- if (!$this->updateWhitelist) {
- throw new \LogicException('isUpdateable should only be called when a whitelist is present');
- }
- foreach ($this->updateWhitelist as $whiteListedPattern => $void) {
- $patternRegexp = $this->packageNameToRegexp($whiteListedPattern);
- if (preg_match($patternRegexp, $package->getName())) {
- return true;
- }
- }
- return false;
- }
- /**
- * Build a regexp from a package name, expanding * globs as required
- *
- * @param string $whiteListedPattern
- * @return string
- */
- private function packageNameToRegexp($whiteListedPattern)
- {
- $cleanedWhiteListedPattern = str_replace('\\*', '.*', preg_quote($whiteListedPattern));
- return "{^" . $cleanedWhiteListedPattern . "$}i";
- }
- private function extractPlatformRequirements($links)
- {
- $platformReqs = array();
- foreach ($links as $link) {
- if (preg_match(PlatformRepository::PLATFORM_PACKAGE_REGEX, $link->getTarget())) {
- $platformReqs[$link->getTarget()] = $link->getPrettyConstraint();
- }
- }
- return $platformReqs;
- }
- /**
- * Adds all dependencies of the update whitelist to the whitelist, too.
- *
- * Packages which are listed as requirements in the root package will be
- * skipped including their dependencies, unless they are listed in the
- * update whitelist themselves.
- *
- * @param RepositoryInterface $localRepo
- * @param boolean $devMode
- * @param array $rootRequires An array of links to packages in require of the root package
- * @param array $rootDevRequires An array of links to packages in require-dev of the root package
- */
- private function whitelistUpdateDependencies($localRepo, $devMode, array $rootRequires, array $rootDevRequires)
- {
- if (!$this->updateWhitelist) {
- return;
- }
- $requiredPackageNames = array();
- foreach (array_merge($rootRequires, $rootDevRequires) as $require) {
- $requiredPackageNames[] = $require->getTarget();
- }
- if ($devMode) {
- $rootRequires = array_merge($rootRequires, $rootDevRequires);
- }
- $skipPackages = array();
- foreach ($rootRequires as $require) {
- $skipPackages[$require->getTarget()] = true;
- }
- $pool = new Pool;
- $pool->addRepository($localRepo);
- $seen = array();
- $rootRequiredPackageNames = array_keys($rootRequires);
- foreach ($this->updateWhitelist as $packageName => $void) {
- $packageQueue = new \SplQueue;
- $depPackages = $pool->whatProvides($packageName);
- $nameMatchesRequiredPackage = in_array($packageName, $requiredPackageNames, true);
- // check if the name is a glob pattern that did not match directly
- if (!$nameMatchesRequiredPackage) {
- $whitelistPatternRegexp = $this->packageNameToRegexp($packageName);
- foreach ($rootRequiredPackageNames as $rootRequiredPackageName) {
- if (preg_match($whitelistPatternRegexp, $rootRequiredPackageName)) {
- $nameMatchesRequiredPackage = true;
- break;
- }
- }
- }
- if (count($depPackages) == 0 && !$nameMatchesRequiredPackage && !in_array($packageName, array('nothing', 'lock'))) {
- $this->io->write('<warning>Package "' . $packageName . '" listed for update is not installed. Ignoring.</warning>');
- }
- foreach ($depPackages as $depPackage) {
- $packageQueue->enqueue($depPackage);
- }
- while (!$packageQueue->isEmpty()) {
- $package = $packageQueue->dequeue();
- if (isset($seen[$package->getId()])) {
- continue;
- }
- $seen[$package->getId()] = true;
- $this->updateWhitelist[$package->getName()] = true;
- if (!$this->whitelistDependencies) {
- continue;
- }
- $requires = $package->getRequires();
- foreach ($requires as $require) {
- $requirePackages = $pool->whatProvides($require->getTarget());
- foreach ($requirePackages as $requirePackage) {
- if (isset($skipPackages[$requirePackage->getName()])) {
- continue;
- }
- $packageQueue->enqueue($requirePackage);
- }
- }
- }
- }
- }
- /**
- * Replace local repositories with InstalledArrayRepository instances
- *
- * This is to prevent any accidental modification of the existing repos on disk
- *
- * @param RepositoryManager $rm
- */
- private function mockLocalRepositories(RepositoryManager $rm)
- {
- $packages = array();
- foreach ($rm->getLocalRepository()->getPackages() as $package) {
- $packages[(string) $package] = clone $package;
- }
- foreach ($packages as $key => $package) {
- if ($package instanceof AliasPackage) {
- $alias = (string) $package->getAliasOf();
- $packages[$key] = new AliasPackage($packages[$alias], $package->getVersion(), $package->getPrettyVersion());
- }
- }
- $rm->setLocalRepository(
- new InstalledArrayRepository($packages)
- );
- }
- /**
- * Create Installer
- *
- * @param IOInterface $io
- * @param Composer $composer
- * @return Installer
- */
- public static function create(IOInterface $io, Composer $composer)
- {
- return new static(
- $io,
- $composer->getConfig(),
- $composer->getPackage(),
- $composer->getDownloadManager(),
- $composer->getRepositoryManager(),
- $composer->getLocker(),
- $composer->getInstallationManager(),
- $composer->getEventDispatcher(),
- $composer->getAutoloadGenerator()
- );
- }
- public function setAdditionalInstalledRepository(RepositoryInterface $additionalInstalledRepository)
- {
- $this->additionalInstalledRepository = $additionalInstalledRepository;
- return $this;
- }
- /**
- * Whether to run in drymode or not
- *
- * @param boolean $dryRun
- * @return Installer
- */
- public function setDryRun($dryRun = true)
- {
- $this->dryRun = (boolean) $dryRun;
- return $this;
- }
- /**
- * Checks, if this is a dry run (simulation mode).
- *
- * @return bool
- */
- public function isDryRun()
- {
- return $this->dryRun;
- }
- /**
- * prefer source installation
- *
- * @param boolean $preferSource
- * @return Installer
- */
- public function setPreferSource($preferSource = true)
- {
- $this->preferSource = (boolean) $preferSource;
- return $this;
- }
- /**
- * prefer dist installation
- *
- * @param boolean $preferDist
- * @return Installer
- */
- public function setPreferDist($preferDist = true)
- {
- $this->preferDist = (boolean) $preferDist;
- return $this;
- }
- /**
- * Whether or not generated autoloader are optimized
- *
- * @param bool $optimizeAutoloader
- * @return Installer
- */
- public function setOptimizeAutoloader($optimizeAutoloader = false)
- {
- $this->optimizeAutoloader = (boolean) $optimizeAutoloader;
- return $this;
- }
- /**
- * update packages
- *
- * @param boolean $update
- * @return Installer
- */
- public function setUpdate($update = true)
- {
- $this->update = (boolean) $update;
- return $this;
- }
- /**
- * enables dev packages
- *
- * @param boolean $devMode
- * @return Installer
- */
- public function setDevMode($devMode = true)
- {
- $this->devMode = (boolean) $devMode;
- return $this;
- }
- /**
- * set whether to run scripts or not
- *
- * @param boolean $runScripts
- * @return Installer
- */
- public function setRunScripts($runScripts = true)
- {
- $this->runScripts = (boolean) $runScripts;
- return $this;
- }
- /**
- * set the config instance
- *
- * @param Config $config
- * @return Installer
- */
- public function setConfig(Config $config)
- {
- $this->config = $config;
- return $this;
- }
- /**
- * run in verbose mode
- *
- * @param boolean $verbose
- * @return Installer
- */
- public function setVerbose($verbose = true)
- {
- $this->verbose = (boolean) $verbose;
- return $this;
- }
- /**
- * Checks, if running in verbose mode.
- *
- * @return bool
- */
- public function isVerbose()
- {
- return $this->verbose;
- }
- /**
- * set ignore Platform Package requirements
- *
- * @param boolean $ignorePlatformReqs
- * @return Installer
- */
- public function setIgnorePlatformRequirements($ignorePlatformReqs = false)
- {
- $this->ignorePlatformReqs = (boolean) $ignorePlatformReqs;
- return $this;
- }
- /**
- * restrict the update operation to a few packages, all other packages
- * that are already installed will be kept at their current version
- *
- * @param array $packages
- * @return Installer
- */
- public function setUpdateWhitelist(array $packages)
- {
- $this->updateWhitelist = array_flip(array_map('strtolower', $packages));
- return $this;
- }
- /**
- * Should dependencies of whitelisted packages be updated recursively?
- *
- * @param boolean $updateDependencies
- * @return Installer
- */
- public function setWhitelistDependencies($updateDependencies = true)
- {
- $this->whitelistDependencies = (boolean) $updateDependencies;
- return $this;
- }
- /**
- * Disables plugins.
- *
- * Call this if you want to ensure that third-party code never gets
- * executed. The default is to automatically install, and execute
- * custom third-party installers.
- *
- * @return Installer
- */
- public function disablePlugins()
- {
- $this->installationManager->disablePlugins();
- return $this;
- }
- }
|