Installer.php 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer;
  12. use Composer\Autoload\AutoloadGenerator;
  13. use Composer\DependencyResolver\DefaultPolicy;
  14. use Composer\DependencyResolver\Operation\UpdateOperation;
  15. use Composer\DependencyResolver\Operation\InstallOperation;
  16. use Composer\DependencyResolver\Operation\UninstallOperation;
  17. use Composer\DependencyResolver\Operation\OperationInterface;
  18. use Composer\DependencyResolver\PolicyInterface;
  19. use Composer\DependencyResolver\Pool;
  20. use Composer\DependencyResolver\Request;
  21. use Composer\DependencyResolver\Rule;
  22. use Composer\DependencyResolver\Solver;
  23. use Composer\DependencyResolver\SolverProblemsException;
  24. use Composer\Downloader\DownloadManager;
  25. use Composer\EventDispatcher\EventDispatcher;
  26. use Composer\Installer\InstallationManager;
  27. use Composer\Installer\InstallerEvents;
  28. use Composer\Installer\NoopInstaller;
  29. use Composer\Installer\SuggestedPackagesReporter;
  30. use Composer\IO\IOInterface;
  31. use Composer\Package\AliasPackage;
  32. use Composer\Package\CompletePackage;
  33. use Composer\Package\Link;
  34. use Composer\Semver\Constraint\Constraint;
  35. use Composer\Package\Locker;
  36. use Composer\Package\PackageInterface;
  37. use Composer\Package\RootPackageInterface;
  38. use Composer\Repository\CompositeRepository;
  39. use Composer\Repository\InstalledArrayRepository;
  40. use Composer\Repository\PlatformRepository;
  41. use Composer\Repository\RepositoryInterface;
  42. use Composer\Repository\RepositoryManager;
  43. use Composer\Repository\WritableRepositoryInterface;
  44. use Composer\Script\ScriptEvents;
  45. /**
  46. * @author Jordi Boggiano <j.boggiano@seld.be>
  47. * @author Beau Simensen <beau@dflydev.com>
  48. * @author Konstantin Kudryashov <ever.zet@gmail.com>
  49. * @author Nils Adermann <naderman@naderman.de>
  50. */
  51. class Installer
  52. {
  53. /**
  54. * @var IOInterface
  55. */
  56. protected $io;
  57. /**
  58. * @var Config
  59. */
  60. protected $config;
  61. /**
  62. * @var RootPackageInterface
  63. */
  64. protected $package;
  65. /**
  66. * @var DownloadManager
  67. */
  68. protected $downloadManager;
  69. /**
  70. * @var RepositoryManager
  71. */
  72. protected $repositoryManager;
  73. /**
  74. * @var Locker
  75. */
  76. protected $locker;
  77. /**
  78. * @var InstallationManager
  79. */
  80. protected $installationManager;
  81. /**
  82. * @var EventDispatcher
  83. */
  84. protected $eventDispatcher;
  85. /**
  86. * @var AutoloadGenerator
  87. */
  88. protected $autoloadGenerator;
  89. protected $preferSource = false;
  90. protected $preferDist = false;
  91. protected $optimizeAutoloader = false;
  92. protected $classMapAuthoritative = false;
  93. protected $devMode = false;
  94. protected $dryRun = false;
  95. protected $verbose = false;
  96. protected $update = false;
  97. protected $dumpAutoloader = true;
  98. protected $runScripts = true;
  99. protected $ignorePlatformReqs = false;
  100. protected $preferStable = false;
  101. protected $preferLowest = false;
  102. /**
  103. * Array of package names/globs flagged for update
  104. *
  105. * @var array|null
  106. */
  107. protected $updateWhitelist = null;
  108. protected $whitelistDependencies = false;
  109. /**
  110. * @var SuggestedPackagesReporter
  111. */
  112. protected $suggestedPackagesReporter;
  113. /**
  114. * @var RepositoryInterface
  115. */
  116. protected $additionalInstalledRepository;
  117. /**
  118. * Constructor
  119. *
  120. * @param IOInterface $io
  121. * @param Config $config
  122. * @param RootPackageInterface $package
  123. * @param DownloadManager $downloadManager
  124. * @param RepositoryManager $repositoryManager
  125. * @param Locker $locker
  126. * @param InstallationManager $installationManager
  127. * @param EventDispatcher $eventDispatcher
  128. * @param AutoloadGenerator $autoloadGenerator
  129. */
  130. public function __construct(IOInterface $io, Config $config, RootPackageInterface $package, DownloadManager $downloadManager, RepositoryManager $repositoryManager, Locker $locker, InstallationManager $installationManager, EventDispatcher $eventDispatcher, AutoloadGenerator $autoloadGenerator)
  131. {
  132. $this->io = $io;
  133. $this->config = $config;
  134. $this->package = $package;
  135. $this->downloadManager = $downloadManager;
  136. $this->repositoryManager = $repositoryManager;
  137. $this->locker = $locker;
  138. $this->installationManager = $installationManager;
  139. $this->eventDispatcher = $eventDispatcher;
  140. $this->autoloadGenerator = $autoloadGenerator;
  141. }
  142. /**
  143. * Run installation (or update)
  144. *
  145. * @throws \Exception
  146. * @return int 0 on success or a positive error code on failure
  147. */
  148. public function run()
  149. {
  150. // Disable GC to save CPU cycles, as the dependency solver can create hundreds of thousands
  151. // of PHP objects, the GC can spend quite some time walking the tree of references looking
  152. // for stuff to collect while there is nothing to collect. This slows things down dramatically
  153. // and turning it off results in much better performance. Do not try this at home however.
  154. gc_collect_cycles();
  155. gc_disable();
  156. if ($this->dryRun) {
  157. $this->verbose = true;
  158. $this->runScripts = false;
  159. $this->installationManager->addInstaller(new NoopInstaller);
  160. $this->mockLocalRepositories($this->repositoryManager);
  161. }
  162. if ($this->runScripts) {
  163. // dispatch pre event
  164. $eventName = $this->update ? ScriptEvents::PRE_UPDATE_CMD : ScriptEvents::PRE_INSTALL_CMD;
  165. $this->eventDispatcher->dispatchScript($eventName, $this->devMode);
  166. }
  167. $this->downloadManager->setPreferSource($this->preferSource);
  168. $this->downloadManager->setPreferDist($this->preferDist);
  169. // clone root package to have one in the installed repo that does not require anything
  170. // we don't want it to be uninstallable, but its requirements should not conflict
  171. // with the lock file for example
  172. $installedRootPackage = clone $this->package;
  173. $installedRootPackage->setRequires(array());
  174. $installedRootPackage->setDevRequires(array());
  175. // create installed repo, this contains all local packages + platform packages (php & extensions)
  176. $localRepo = $this->repositoryManager->getLocalRepository();
  177. if (!$this->update && $this->locker->isLocked()) {
  178. $platformOverrides = $this->locker->getPlatformOverrides();
  179. } else {
  180. $platformOverrides = $this->config->get('platform') ?: array();
  181. }
  182. $platformRepo = new PlatformRepository(array(), $platformOverrides);
  183. $repos = array(
  184. $localRepo,
  185. new InstalledArrayRepository(array($installedRootPackage)),
  186. $platformRepo,
  187. );
  188. $installedRepo = new CompositeRepository($repos);
  189. if ($this->additionalInstalledRepository) {
  190. $installedRepo->addRepository($this->additionalInstalledRepository);
  191. }
  192. $aliases = $this->getRootAliases();
  193. $this->aliasPlatformPackages($platformRepo, $aliases);
  194. if (!$this->suggestedPackagesReporter) {
  195. $this->suggestedPackagesReporter = new SuggestedPackagesReporter($this->io);
  196. }
  197. try {
  198. $res = $this->doInstall($localRepo, $installedRepo, $platformRepo, $aliases, $this->devMode);
  199. if ($res !== 0) {
  200. return $res;
  201. }
  202. } catch (\Exception $e) {
  203. if (!$this->dryRun) {
  204. $this->installationManager->notifyInstalls($this->io);
  205. }
  206. throw $e;
  207. }
  208. if (!$this->dryRun) {
  209. $this->installationManager->notifyInstalls($this->io);
  210. }
  211. // output suggestions if we're in dev mode
  212. if ($this->devMode) {
  213. $this->suggestedPackagesReporter->output($installedRepo);
  214. }
  215. # Find abandoned packages and warn user
  216. foreach ($localRepo->getPackages() as $package) {
  217. if (!$package instanceof CompletePackage || !$package->isAbandoned()) {
  218. continue;
  219. }
  220. $replacement = (is_string($package->getReplacementPackage()))
  221. ? 'Use ' . $package->getReplacementPackage() . ' instead'
  222. : 'No replacement was suggested';
  223. $this->io->writeError(
  224. sprintf(
  225. "<warning>Package %s is abandoned, you should avoid using it. %s.</warning>",
  226. $package->getPrettyName(),
  227. $replacement
  228. )
  229. );
  230. }
  231. if (!$this->dryRun) {
  232. // write lock
  233. if ($this->update || !$this->locker->isLocked()) {
  234. $localRepo->reload();
  235. // if this is not run in dev mode and the root has dev requires, the lock must
  236. // contain null to prevent dev installs from a non-dev lock
  237. $devPackages = ($this->devMode || !$this->package->getDevRequires()) ? array() : null;
  238. // split dev and non-dev requirements by checking what would be removed if we update without the dev requirements
  239. if ($this->devMode && $this->package->getDevRequires()) {
  240. $policy = $this->createPolicy();
  241. $pool = $this->createPool(true);
  242. $pool->addRepository($installedRepo, $aliases);
  243. // creating requirements request
  244. $request = $this->createRequest($this->package, $platformRepo);
  245. $request->updateAll();
  246. foreach ($this->package->getRequires() as $link) {
  247. $request->install($link->getTarget(), $link->getConstraint());
  248. }
  249. $this->eventDispatcher->dispatchInstallerEvent(InstallerEvents::PRE_DEPENDENCIES_SOLVING, false, $policy, $pool, $installedRepo, $request);
  250. $solver = new Solver($policy, $pool, $installedRepo);
  251. $ops = $solver->solve($request, $this->ignorePlatformReqs);
  252. $this->eventDispatcher->dispatchInstallerEvent(InstallerEvents::POST_DEPENDENCIES_SOLVING, false, $policy, $pool, $installedRepo, $request, $ops);
  253. foreach ($ops as $op) {
  254. if ($op->getJobType() === 'uninstall') {
  255. $devPackages[] = $op->getPackage();
  256. }
  257. }
  258. }
  259. $platformReqs = $this->extractPlatformRequirements($this->package->getRequires());
  260. $platformDevReqs = $this->devMode ? $this->extractPlatformRequirements($this->package->getDevRequires()) : array();
  261. $updatedLock = $this->locker->setLockData(
  262. array_diff($localRepo->getCanonicalPackages(), (array) $devPackages),
  263. $devPackages,
  264. $platformReqs,
  265. $platformDevReqs,
  266. $aliases,
  267. $this->package->getMinimumStability(),
  268. $this->package->getStabilityFlags(),
  269. $this->preferStable || $this->package->getPreferStable(),
  270. $this->preferLowest,
  271. $this->config->get('platform') ?: array()
  272. );
  273. if ($updatedLock) {
  274. $this->io->writeError('<info>Writing lock file</info>');
  275. }
  276. }
  277. if ($this->dumpAutoloader) {
  278. // write autoloader
  279. if ($this->optimizeAutoloader) {
  280. $this->io->writeError('<info>Generating optimized autoload files</info>');
  281. } else {
  282. $this->io->writeError('<info>Generating autoload files</info>');
  283. }
  284. $this->autoloadGenerator->setDevMode($this->devMode);
  285. $this->autoloadGenerator->setClassMapAuthoritative($this->classMapAuthoritative);
  286. $this->autoloadGenerator->setRunScripts($this->runScripts);
  287. $this->autoloadGenerator->dump($this->config, $localRepo, $this->package, $this->installationManager, 'composer', $this->optimizeAutoloader);
  288. }
  289. if ($this->runScripts) {
  290. // dispatch post event
  291. $eventName = $this->update ? ScriptEvents::POST_UPDATE_CMD : ScriptEvents::POST_INSTALL_CMD;
  292. $this->eventDispatcher->dispatchScript($eventName, $this->devMode);
  293. }
  294. $vendorDir = $this->config->get('vendor-dir');
  295. if (is_dir($vendorDir)) {
  296. // suppress errors as this fails sometimes on OSX for no apparent reason
  297. // see https://github.com/composer/composer/issues/4070#issuecomment-129792748
  298. @touch($vendorDir);
  299. }
  300. }
  301. // re-enable GC except on HHVM which triggers a warning here
  302. if (!defined('HHVM_VERSION')) {
  303. gc_enable();
  304. }
  305. return 0;
  306. }
  307. /**
  308. * @param RepositoryInterface $localRepo
  309. * @param RepositoryInterface $installedRepo
  310. * @param PlatformRepository $platformRepo
  311. * @param array $aliases
  312. * @param bool $withDevReqs
  313. * @return int
  314. */
  315. protected function doInstall($localRepo, $installedRepo, $platformRepo, $aliases, $withDevReqs)
  316. {
  317. // init vars
  318. $lockedRepository = null;
  319. $repositories = null;
  320. // initialize locker to create aliased packages
  321. $installFromLock = !$this->update && $this->locker->isLocked();
  322. // initialize locked repo if we are installing from lock or in a partial update
  323. // and a lock file is present as we need to force install non-whitelisted lock file
  324. // packages in that case
  325. if ($installFromLock || (!empty($this->updateWhitelist) && $this->locker->isLocked())) {
  326. try {
  327. $lockedRepository = $this->locker->getLockedRepository($withDevReqs);
  328. } catch (\RuntimeException $e) {
  329. // if there are dev requires, then we really can not install
  330. if ($this->package->getDevRequires()) {
  331. throw $e;
  332. }
  333. // no require-dev in composer.json and the lock file was created with no dev info, so skip them
  334. $lockedRepository = $this->locker->getLockedRepository();
  335. }
  336. }
  337. $this->whitelistUpdateDependencies(
  338. $localRepo,
  339. $withDevReqs,
  340. $this->package->getRequires(),
  341. $this->package->getDevRequires()
  342. );
  343. $this->io->writeError('<info>Loading composer repositories with package information</info>');
  344. // creating repository pool
  345. $policy = $this->createPolicy();
  346. $pool = $this->createPool($withDevReqs, $installFromLock ? $lockedRepository : null);
  347. $pool->addRepository($installedRepo, $aliases);
  348. if (!$installFromLock) {
  349. $repositories = $this->repositoryManager->getRepositories();
  350. foreach ($repositories as $repository) {
  351. $pool->addRepository($repository, $aliases);
  352. }
  353. }
  354. // Add the locked repository after the others in case we are doing a
  355. // partial update so missing packages can be found there still.
  356. // For installs from lock it's the only one added so it is first
  357. if ($lockedRepository) {
  358. $pool->addRepository($lockedRepository, $aliases);
  359. }
  360. // creating requirements request
  361. $request = $this->createRequest($this->package, $platformRepo);
  362. if (!$installFromLock) {
  363. // remove unstable packages from the localRepo if they don't match the current stability settings
  364. $removedUnstablePackages = array();
  365. foreach ($localRepo->getPackages() as $package) {
  366. if (
  367. !$pool->isPackageAcceptable($package->getNames(), $package->getStability())
  368. && $this->installationManager->isPackageInstalled($localRepo, $package)
  369. ) {
  370. $removedUnstablePackages[$package->getName()] = true;
  371. $request->remove($package->getName(), new Constraint('=', $package->getVersion()));
  372. }
  373. }
  374. }
  375. if ($this->update) {
  376. $this->io->writeError('<info>Updating dependencies'.($withDevReqs ? ' (including require-dev)' : '').'</info>');
  377. $request->updateAll();
  378. if ($withDevReqs) {
  379. $links = array_merge($this->package->getRequires(), $this->package->getDevRequires());
  380. } else {
  381. $links = $this->package->getRequires();
  382. }
  383. foreach ($links as $link) {
  384. $request->install($link->getTarget(), $link->getConstraint());
  385. }
  386. // if the updateWhitelist is enabled, packages not in it are also fixed
  387. // to the version specified in the lock, or their currently installed version
  388. if ($this->updateWhitelist) {
  389. $currentPackages = $this->getCurrentPackages($withDevReqs, $installedRepo);
  390. // collect packages to fixate from root requirements as well as installed packages
  391. $candidates = array();
  392. foreach ($links as $link) {
  393. $candidates[$link->getTarget()] = true;
  394. }
  395. foreach ($localRepo->getPackages() as $package) {
  396. $candidates[$package->getName()] = true;
  397. }
  398. // fix them to the version in lock (or currently installed) if they are not updateable
  399. foreach ($candidates as $candidate => $dummy) {
  400. foreach ($currentPackages as $curPackage) {
  401. if ($curPackage->getName() === $candidate) {
  402. if (!$this->isUpdateable($curPackage) && !isset($removedUnstablePackages[$curPackage->getName()])) {
  403. $constraint = new Constraint('=', $curPackage->getVersion());
  404. $request->install($curPackage->getName(), $constraint);
  405. }
  406. break;
  407. }
  408. }
  409. }
  410. }
  411. } elseif ($installFromLock) {
  412. $this->io->writeError('<info>Installing dependencies'.($withDevReqs ? ' (including require-dev)' : '').' from lock file</info>');
  413. if (!$this->locker->isFresh()) {
  414. $this->io->writeError('<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>');
  415. }
  416. foreach ($lockedRepository->getPackages() as $package) {
  417. $version = $package->getVersion();
  418. if (isset($aliases[$package->getName()][$version])) {
  419. $version = $aliases[$package->getName()][$version]['alias_normalized'];
  420. }
  421. $constraint = new Constraint('=', $version);
  422. $constraint->setPrettyString($package->getPrettyVersion());
  423. $request->install($package->getName(), $constraint);
  424. }
  425. foreach ($this->locker->getPlatformRequirements($withDevReqs) as $link) {
  426. $request->install($link->getTarget(), $link->getConstraint());
  427. }
  428. } else {
  429. $this->io->writeError('<info>Installing dependencies'.($withDevReqs ? ' (including require-dev)' : '').'</info>');
  430. if ($withDevReqs) {
  431. $links = array_merge($this->package->getRequires(), $this->package->getDevRequires());
  432. } else {
  433. $links = $this->package->getRequires();
  434. }
  435. foreach ($links as $link) {
  436. $request->install($link->getTarget(), $link->getConstraint());
  437. }
  438. }
  439. // force dev packages to have the latest links if we update or install from a (potentially new) lock
  440. $this->processDevPackages($localRepo, $pool, $policy, $repositories, $installedRepo, $lockedRepository, $installFromLock, $withDevReqs, 'force-links');
  441. // solve dependencies
  442. $this->eventDispatcher->dispatchInstallerEvent(InstallerEvents::PRE_DEPENDENCIES_SOLVING, $this->devMode, $policy, $pool, $installedRepo, $request);
  443. $solver = new Solver($policy, $pool, $installedRepo);
  444. try {
  445. $operations = $solver->solve($request, $this->ignorePlatformReqs);
  446. $this->eventDispatcher->dispatchInstallerEvent(InstallerEvents::POST_DEPENDENCIES_SOLVING, $this->devMode, $policy, $pool, $installedRepo, $request, $operations);
  447. } catch (SolverProblemsException $e) {
  448. $this->io->writeError('<error>Your requirements could not be resolved to an installable set of packages.</error>');
  449. $this->io->writeError($e->getMessage());
  450. return max(1, $e->getCode());
  451. }
  452. $this->io->writeError("Analyzed ".count($pool)." packages to resolve dependencies", true, IOInterface::VERBOSE);
  453. $this->io->writeError("Analyzed ".$solver->getRuleSetSize()." rules to resolve dependencies", true, IOInterface::VERBOSE);
  454. // force dev packages to be updated if we update or install from a (potentially new) lock
  455. $operations = $this->processDevPackages($localRepo, $pool, $policy, $repositories, $installedRepo, $lockedRepository, $installFromLock, $withDevReqs, 'force-updates', $operations);
  456. // execute operations
  457. if (!$operations) {
  458. $this->io->writeError('Nothing to install or update');
  459. }
  460. $operations = $this->movePluginsToFront($operations);
  461. $operations = $this->moveUninstallsToFront($operations);
  462. foreach ($operations as $operation) {
  463. // collect suggestions
  464. if ('install' === $operation->getJobType()) {
  465. $this->suggestedPackagesReporter->addSuggestionsFromPackage($operation->getPackage());
  466. }
  467. // not installing from lock, force dev packages' references if they're in root package refs
  468. if (!$installFromLock) {
  469. $package = null;
  470. if ('update' === $operation->getJobType()) {
  471. $package = $operation->getTargetPackage();
  472. } elseif ('install' === $operation->getJobType()) {
  473. $package = $operation->getPackage();
  474. }
  475. if ($package && $package->isDev()) {
  476. $references = $this->package->getReferences();
  477. if (isset($references[$package->getName()])) {
  478. $package->setSourceReference($references[$package->getName()]);
  479. $package->setDistReference($references[$package->getName()]);
  480. }
  481. }
  482. if ('update' === $operation->getJobType()
  483. && $operation->getTargetPackage()->isDev()
  484. && $operation->getTargetPackage()->getVersion() === $operation->getInitialPackage()->getVersion()
  485. && (!$operation->getTargetPackage()->getSourceReference() || $operation->getTargetPackage()->getSourceReference() === $operation->getInitialPackage()->getSourceReference())
  486. && (!$operation->getTargetPackage()->getDistReference() || $operation->getTargetPackage()->getDistReference() === $operation->getInitialPackage()->getDistReference())
  487. ) {
  488. $this->io->writeError(' - Skipping update of '. $operation->getTargetPackage()->getPrettyName().' to the same reference-locked version', true, IOInterface::DEBUG);
  489. $this->io->writeError('', true, IOInterface::DEBUG);
  490. continue;
  491. }
  492. }
  493. $event = 'Composer\Installer\PackageEvents::PRE_PACKAGE_'.strtoupper($operation->getJobType());
  494. if (defined($event) && $this->runScripts) {
  495. $this->eventDispatcher->dispatchPackageEvent(constant($event), $this->devMode, $policy, $pool, $installedRepo, $request, $operations, $operation);
  496. }
  497. // output non-alias ops in dry run, output alias ops in debug verbosity
  498. if ($this->dryRun && false === strpos($operation->getJobType(), 'Alias')) {
  499. $this->io->writeError(' - ' . $operation);
  500. $this->io->writeError('');
  501. } elseif ($this->io->isDebug() && false !== strpos($operation->getJobType(), 'Alias')) {
  502. $this->io->writeError(' - ' . $operation);
  503. $this->io->writeError('');
  504. }
  505. $this->installationManager->execute($localRepo, $operation);
  506. // output reasons why the operation was ran, only for install/update operations
  507. if ($this->verbose && $this->io->isVeryVerbose() && in_array($operation->getJobType(), array('install', 'update'))) {
  508. $reason = $operation->getReason();
  509. if ($reason instanceof Rule) {
  510. switch ($reason->getReason()) {
  511. case Rule::RULE_JOB_INSTALL:
  512. $this->io->writeError(' REASON: Required by root: '.$reason->getPrettyString($pool));
  513. $this->io->writeError('');
  514. break;
  515. case Rule::RULE_PACKAGE_REQUIRES:
  516. $this->io->writeError(' REASON: '.$reason->getPrettyString($pool));
  517. $this->io->writeError('');
  518. break;
  519. }
  520. }
  521. }
  522. $event = 'Composer\Installer\PackageEvents::POST_PACKAGE_'.strtoupper($operation->getJobType());
  523. if (defined($event) && $this->runScripts) {
  524. $this->eventDispatcher->dispatchPackageEvent(constant($event), $this->devMode, $policy, $pool, $installedRepo, $request, $operations, $operation);
  525. }
  526. if (!$this->dryRun) {
  527. $localRepo->write();
  528. }
  529. }
  530. if (!$this->dryRun) {
  531. // force source/dist urls to be updated for all packages
  532. $this->processPackageUrls($pool, $policy, $localRepo, $repositories);
  533. $localRepo->write();
  534. }
  535. return 0;
  536. }
  537. /**
  538. * Workaround: if your packages depend on plugins, we must be sure
  539. * that those are installed / updated first; else it would lead to packages
  540. * being installed multiple times in different folders, when running Composer
  541. * twice.
  542. *
  543. * While this does not fix the root-causes of https://github.com/composer/composer/issues/1147,
  544. * it at least fixes the symptoms and makes usage of composer possible (again)
  545. * in such scenarios.
  546. *
  547. * @param OperationInterface[] $operations
  548. * @return OperationInterface[] reordered operation list
  549. */
  550. private function movePluginsToFront(array $operations)
  551. {
  552. $installerOps = array();
  553. foreach ($operations as $idx => $op) {
  554. if ($op instanceof InstallOperation) {
  555. $package = $op->getPackage();
  556. } elseif ($op instanceof UpdateOperation) {
  557. $package = $op->getTargetPackage();
  558. } else {
  559. continue;
  560. }
  561. if ($package->getType() === 'composer-plugin' || $package->getType() === 'composer-installer') {
  562. // ignore requirements to platform or composer-plugin-api
  563. $requires = array_keys($package->getRequires());
  564. foreach ($requires as $index => $req) {
  565. if ($req === 'composer-plugin-api' || preg_match(PlatformRepository::PLATFORM_PACKAGE_REGEX, $req)) {
  566. unset($requires[$index]);
  567. }
  568. }
  569. // if there are no other requirements, move the plugin to the top of the op list
  570. if (!count($requires)) {
  571. $installerOps[] = $op;
  572. unset($operations[$idx]);
  573. }
  574. }
  575. }
  576. return array_merge($installerOps, $operations);
  577. }
  578. /**
  579. * Removals of packages should be executed before installations in
  580. * case two packages resolve to the same path (due to custom installers)
  581. *
  582. * @param OperationInterface[] $operations
  583. * @return OperationInterface[] reordered operation list
  584. */
  585. private function moveUninstallsToFront(array $operations)
  586. {
  587. $uninstOps = array();
  588. foreach ($operations as $idx => $op) {
  589. if ($op instanceof UninstallOperation) {
  590. $uninstOps[] = $op;
  591. unset($operations[$idx]);
  592. }
  593. }
  594. return array_merge($uninstOps, $operations);
  595. }
  596. /**
  597. * @param bool $withDevReqs
  598. * @param RepositoryInterface|null $lockedRepository
  599. * @return Pool
  600. */
  601. private function createPool($withDevReqs, RepositoryInterface $lockedRepository = null)
  602. {
  603. if (!$this->update && $this->locker->isLocked()) { // install from lock
  604. $minimumStability = $this->locker->getMinimumStability();
  605. $stabilityFlags = $this->locker->getStabilityFlags();
  606. $requires = array();
  607. foreach ($lockedRepository->getPackages() as $package) {
  608. $constraint = new Constraint('=', $package->getVersion());
  609. $constraint->setPrettyString($package->getPrettyVersion());
  610. $requires[$package->getName()] = $constraint;
  611. }
  612. } else {
  613. $minimumStability = $this->package->getMinimumStability();
  614. $stabilityFlags = $this->package->getStabilityFlags();
  615. $requires = $this->package->getRequires();
  616. if ($withDevReqs) {
  617. $requires = array_merge($requires, $this->package->getDevRequires());
  618. }
  619. }
  620. $rootConstraints = array();
  621. foreach ($requires as $req => $constraint) {
  622. // skip platform requirements from the root package to avoid filtering out existing platform packages
  623. if ($this->ignorePlatformReqs && preg_match(PlatformRepository::PLATFORM_PACKAGE_REGEX, $req)) {
  624. continue;
  625. }
  626. if ($constraint instanceof Link) {
  627. $rootConstraints[$req] = $constraint->getConstraint();
  628. } else {
  629. $rootConstraints[$req] = $constraint;
  630. }
  631. }
  632. return new Pool($minimumStability, $stabilityFlags, $rootConstraints);
  633. }
  634. /**
  635. * @return DefaultPolicy
  636. */
  637. private function createPolicy()
  638. {
  639. $preferStable = null;
  640. $preferLowest = null;
  641. if (!$this->update && $this->locker->isLocked()) {
  642. $preferStable = $this->locker->getPreferStable();
  643. $preferLowest = $this->locker->getPreferLowest();
  644. }
  645. // old lock file without prefer stable/lowest will return null
  646. // so in this case we use the composer.json info
  647. if (null === $preferStable) {
  648. $preferStable = $this->preferStable || $this->package->getPreferStable();
  649. }
  650. if (null === $preferLowest) {
  651. $preferLowest = $this->preferLowest;
  652. }
  653. return new DefaultPolicy($preferStable, $preferLowest);
  654. }
  655. /**
  656. * @param RootPackageInterface $rootPackage
  657. * @param PlatformRepository $platformRepo
  658. * @return Request
  659. */
  660. private function createRequest(RootPackageInterface $rootPackage, PlatformRepository $platformRepo)
  661. {
  662. $request = new Request();
  663. $constraint = new Constraint('=', $rootPackage->getVersion());
  664. $constraint->setPrettyString($rootPackage->getPrettyVersion());
  665. $request->install($rootPackage->getName(), $constraint);
  666. $fixedPackages = $platformRepo->getPackages();
  667. if ($this->additionalInstalledRepository) {
  668. $additionalFixedPackages = $this->additionalInstalledRepository->getPackages();
  669. $fixedPackages = array_merge($fixedPackages, $additionalFixedPackages);
  670. }
  671. // fix the version of all platform packages + additionally installed packages
  672. // to prevent the solver trying to remove or update those
  673. $provided = $rootPackage->getProvides();
  674. foreach ($fixedPackages as $package) {
  675. $constraint = new Constraint('=', $package->getVersion());
  676. $constraint->setPrettyString($package->getPrettyVersion());
  677. // skip platform packages that are provided by the root package
  678. if ($package->getRepository() !== $platformRepo
  679. || !isset($provided[$package->getName()])
  680. || !$provided[$package->getName()]->getConstraint()->matches($constraint)
  681. ) {
  682. $request->fix($package->getName(), $constraint);
  683. }
  684. }
  685. return $request;
  686. }
  687. /**
  688. * @param WritableRepositoryInterface $localRepo
  689. * @param Pool $pool
  690. * @param PolicyInterface $policy
  691. * @param array $repositories
  692. * @param RepositoryInterface $installedRepo
  693. * @param RepositoryInterface $lockedRepository
  694. * @param bool $installFromLock
  695. * @param bool $withDevReqs
  696. * @param string $task
  697. * @param array|null $operations
  698. * @return array
  699. */
  700. private function processDevPackages($localRepo, $pool, $policy, $repositories, $installedRepo, $lockedRepository, $installFromLock, $withDevReqs, $task, array $operations = null)
  701. {
  702. if ($task === 'force-updates' && null === $operations) {
  703. throw new \InvalidArgumentException('Missing operations argument');
  704. }
  705. if ($task === 'force-links') {
  706. $operations = array();
  707. }
  708. if (!$installFromLock && $this->updateWhitelist) {
  709. $currentPackages = $this->getCurrentPackages($withDevReqs, $installedRepo);
  710. }
  711. foreach ($localRepo->getCanonicalPackages() as $package) {
  712. // skip non-dev packages
  713. if (!$package->isDev()) {
  714. continue;
  715. }
  716. // skip packages that will be updated/uninstalled
  717. foreach ($operations as $operation) {
  718. if (('update' === $operation->getJobType() && $operation->getInitialPackage()->equals($package))
  719. || ('uninstall' === $operation->getJobType() && $operation->getPackage()->equals($package))
  720. ) {
  721. continue 2;
  722. }
  723. }
  724. // force update to locked version if it does not match the installed version
  725. if ($installFromLock) {
  726. foreach ($lockedRepository->findPackages($package->getName()) as $lockedPackage) {
  727. if ($lockedPackage->isDev() && $lockedPackage->getVersion() === $package->getVersion()) {
  728. if ($task === 'force-links') {
  729. $package->setRequires($lockedPackage->getRequires());
  730. $package->setConflicts($lockedPackage->getConflicts());
  731. $package->setProvides($lockedPackage->getProvides());
  732. $package->setReplaces($lockedPackage->getReplaces());
  733. } elseif ($task === 'force-updates') {
  734. if (($lockedPackage->getSourceReference() && $lockedPackage->getSourceReference() !== $package->getSourceReference())
  735. || ($lockedPackage->getDistReference() && $lockedPackage->getDistReference() !== $package->getDistReference())
  736. ) {
  737. $operations[] = new UpdateOperation($package, $lockedPackage);
  738. }
  739. }
  740. break;
  741. }
  742. }
  743. } else {
  744. // force update to latest on update
  745. if ($this->update) {
  746. // skip package if the whitelist is enabled and it is not in it
  747. if ($this->updateWhitelist && !$this->isUpdateable($package)) {
  748. // check if non-updateable packages are out of date compared to the lock file to ensure we don't corrupt it
  749. foreach ($currentPackages as $curPackage) {
  750. if ($curPackage->isDev() && $curPackage->getName() === $package->getName() && $curPackage->getVersion() === $package->getVersion()) {
  751. if ($task === 'force-links') {
  752. $package->setRequires($curPackage->getRequires());
  753. $package->setConflicts($curPackage->getConflicts());
  754. $package->setProvides($curPackage->getProvides());
  755. $package->setReplaces($curPackage->getReplaces());
  756. } elseif ($task === 'force-updates') {
  757. if (($curPackage->getSourceReference() && $curPackage->getSourceReference() !== $package->getSourceReference())
  758. || ($curPackage->getDistReference() && $curPackage->getDistReference() !== $package->getDistReference())
  759. ) {
  760. $operations[] = new UpdateOperation($package, $curPackage);
  761. }
  762. }
  763. break;
  764. }
  765. }
  766. continue;
  767. }
  768. // find similar packages (name/version) in all repositories
  769. $matches = $pool->whatProvides($package->getName(), new Constraint('=', $package->getVersion()));
  770. foreach ($matches as $index => $match) {
  771. // skip local packages
  772. if (!in_array($match->getRepository(), $repositories, true)) {
  773. unset($matches[$index]);
  774. continue;
  775. }
  776. // skip providers/replacers
  777. if ($match->getName() !== $package->getName()) {
  778. unset($matches[$index]);
  779. continue;
  780. }
  781. $matches[$index] = $match->getId();
  782. }
  783. // select preferred package according to policy rules
  784. if ($matches && $matches = $policy->selectPreferredPackages($pool, array(), $matches)) {
  785. $newPackage = $pool->literalToPackage($matches[0]);
  786. if ($task === 'force-links' && $newPackage) {
  787. $package->setRequires($newPackage->getRequires());
  788. $package->setConflicts($newPackage->getConflicts());
  789. $package->setProvides($newPackage->getProvides());
  790. $package->setReplaces($newPackage->getReplaces());
  791. }
  792. if ($task === 'force-updates' && $newPackage && (
  793. (($newPackage->getSourceReference() && $newPackage->getSourceReference() !== $package->getSourceReference())
  794. || ($newPackage->getDistReference() && $newPackage->getDistReference() !== $package->getDistReference())
  795. )
  796. )) {
  797. $operations[] = new UpdateOperation($package, $newPackage);
  798. }
  799. }
  800. }
  801. if ($task === 'force-updates') {
  802. // force installed package to update to referenced version in root package if it does not match the installed version
  803. $references = $this->package->getReferences();
  804. if (isset($references[$package->getName()]) && $references[$package->getName()] !== $package->getSourceReference()) {
  805. // changing the source ref to update to will be handled in the operations loop below
  806. $operations[] = new UpdateOperation($package, clone $package);
  807. }
  808. }
  809. }
  810. }
  811. return $operations;
  812. }
  813. /**
  814. * Loads the most "current" list of packages that are installed meaning from lock ideally or from installed repo as fallback
  815. * @param bool $withDevReqs
  816. * @param RepositoryInterface $installedRepo
  817. * @return array
  818. */
  819. private function getCurrentPackages($withDevReqs, $installedRepo)
  820. {
  821. if ($this->locker->isLocked()) {
  822. try {
  823. return $this->locker->getLockedRepository($withDevReqs)->getPackages();
  824. } catch (\RuntimeException $e) {
  825. // fetch only non-dev packages from lock if doing a dev update fails due to a previously incomplete lock file
  826. return $this->locker->getLockedRepository()->getPackages();
  827. }
  828. }
  829. return $installedRepo->getPackages();
  830. }
  831. /**
  832. * @return array
  833. */
  834. private function getRootAliases()
  835. {
  836. if (!$this->update && $this->locker->isLocked()) {
  837. $aliases = $this->locker->getAliases();
  838. } else {
  839. $aliases = $this->package->getAliases();
  840. }
  841. $normalizedAliases = array();
  842. foreach ($aliases as $alias) {
  843. $normalizedAliases[$alias['package']][$alias['version']] = array(
  844. 'alias' => $alias['alias'],
  845. 'alias_normalized' => $alias['alias_normalized'],
  846. );
  847. }
  848. return $normalizedAliases;
  849. }
  850. /**
  851. * @param Pool $pool
  852. * @param PolicyInterface $policy
  853. * @param WritableRepositoryInterface $localRepo
  854. * @param array $repositories
  855. */
  856. private function processPackageUrls($pool, $policy, $localRepo, $repositories)
  857. {
  858. if (!$this->update) {
  859. return;
  860. }
  861. foreach ($localRepo->getCanonicalPackages() as $package) {
  862. // find similar packages (name/version) in all repositories
  863. $matches = $pool->whatProvides($package->getName(), new Constraint('=', $package->getVersion()));
  864. foreach ($matches as $index => $match) {
  865. // skip local packages
  866. if (!in_array($match->getRepository(), $repositories, true)) {
  867. unset($matches[$index]);
  868. continue;
  869. }
  870. // skip providers/replacers
  871. if ($match->getName() !== $package->getName()) {
  872. unset($matches[$index]);
  873. continue;
  874. }
  875. $matches[$index] = $match->getId();
  876. }
  877. // select preferred package according to policy rules
  878. if ($matches && $matches = $policy->selectPreferredPackages($pool, array(), $matches)) {
  879. $newPackage = $pool->literalToPackage($matches[0]);
  880. // update the dist and source URLs
  881. $sourceUrl = $package->getSourceUrl();
  882. $newSourceUrl = $newPackage->getSourceUrl();
  883. if ($sourceUrl !== $newSourceUrl) {
  884. $package->setSourceType($newPackage->getSourceType());
  885. $package->setSourceUrl($newSourceUrl);
  886. $package->setSourceReference($newPackage->getSourceReference());
  887. }
  888. // only update dist url for github/bitbucket dists as they use a combination of dist url + dist reference to install
  889. // but for other urls this is ambiguous and could result in bad outcomes
  890. if (preg_match('{^https?://(?:(?:www\.)?bitbucket\.org|(api\.)?github\.com)/}', $newPackage->getDistUrl())) {
  891. $package->setDistUrl($newPackage->getDistUrl());
  892. }
  893. }
  894. }
  895. }
  896. /**
  897. * @param PlatformRepository $platformRepo
  898. * @param array $aliases
  899. */
  900. private function aliasPlatformPackages(PlatformRepository $platformRepo, $aliases)
  901. {
  902. foreach ($aliases as $package => $versions) {
  903. foreach ($versions as $version => $alias) {
  904. $packages = $platformRepo->findPackages($package, $version);
  905. foreach ($packages as $package) {
  906. $aliasPackage = new AliasPackage($package, $alias['alias_normalized'], $alias['alias']);
  907. $aliasPackage->setRootPackageAlias(true);
  908. $platformRepo->addPackage($aliasPackage);
  909. }
  910. }
  911. }
  912. }
  913. /**
  914. * @param PackageInterface $package
  915. * @return bool
  916. */
  917. private function isUpdateable(PackageInterface $package)
  918. {
  919. if (!$this->updateWhitelist) {
  920. throw new \LogicException('isUpdateable should only be called when a whitelist is present');
  921. }
  922. foreach ($this->updateWhitelist as $whiteListedPattern => $void) {
  923. $patternRegexp = $this->packageNameToRegexp($whiteListedPattern);
  924. if (preg_match($patternRegexp, $package->getName())) {
  925. return true;
  926. }
  927. }
  928. return false;
  929. }
  930. /**
  931. * Build a regexp from a package name, expanding * globs as required
  932. *
  933. * @param string $whiteListedPattern
  934. * @return string
  935. */
  936. private function packageNameToRegexp($whiteListedPattern)
  937. {
  938. $cleanedWhiteListedPattern = str_replace('\\*', '.*', preg_quote($whiteListedPattern));
  939. return "{^" . $cleanedWhiteListedPattern . "$}i";
  940. }
  941. /**
  942. * @param array $links
  943. * @return array
  944. */
  945. private function extractPlatformRequirements($links)
  946. {
  947. $platformReqs = array();
  948. foreach ($links as $link) {
  949. if (preg_match(PlatformRepository::PLATFORM_PACKAGE_REGEX, $link->getTarget())) {
  950. $platformReqs[$link->getTarget()] = $link->getPrettyConstraint();
  951. }
  952. }
  953. return $platformReqs;
  954. }
  955. /**
  956. * Adds all dependencies of the update whitelist to the whitelist, too.
  957. *
  958. * Packages which are listed as requirements in the root package will be
  959. * skipped including their dependencies, unless they are listed in the
  960. * update whitelist themselves.
  961. *
  962. * @param RepositoryInterface $localRepo
  963. * @param bool $devMode
  964. * @param array $rootRequires An array of links to packages in require of the root package
  965. * @param array $rootDevRequires An array of links to packages in require-dev of the root package
  966. */
  967. private function whitelistUpdateDependencies($localRepo, $devMode, array $rootRequires, array $rootDevRequires)
  968. {
  969. if (!$this->updateWhitelist) {
  970. return;
  971. }
  972. $requiredPackageNames = array();
  973. foreach (array_merge($rootRequires, $rootDevRequires) as $require) {
  974. $requiredPackageNames[] = $require->getTarget();
  975. }
  976. if ($devMode) {
  977. $rootRequires = array_merge($rootRequires, $rootDevRequires);
  978. }
  979. $skipPackages = array();
  980. foreach ($rootRequires as $require) {
  981. $skipPackages[$require->getTarget()] = true;
  982. }
  983. $pool = new Pool;
  984. $pool->addRepository($localRepo);
  985. $seen = array();
  986. $rootRequiredPackageNames = array_keys($rootRequires);
  987. foreach ($this->updateWhitelist as $packageName => $void) {
  988. $packageQueue = new \SplQueue;
  989. $depPackages = $pool->whatProvides($packageName);
  990. $nameMatchesRequiredPackage = in_array($packageName, $requiredPackageNames, true);
  991. // check if the name is a glob pattern that did not match directly
  992. if (!$nameMatchesRequiredPackage) {
  993. $whitelistPatternRegexp = $this->packageNameToRegexp($packageName);
  994. foreach ($rootRequiredPackageNames as $rootRequiredPackageName) {
  995. if (preg_match($whitelistPatternRegexp, $rootRequiredPackageName)) {
  996. $nameMatchesRequiredPackage = true;
  997. break;
  998. }
  999. }
  1000. }
  1001. if (count($depPackages) == 0 && !$nameMatchesRequiredPackage && !in_array($packageName, array('nothing', 'lock'))) {
  1002. $this->io->writeError('<warning>Package "' . $packageName . '" listed for update is not installed. Ignoring.</warning>');
  1003. }
  1004. foreach ($depPackages as $depPackage) {
  1005. $packageQueue->enqueue($depPackage);
  1006. }
  1007. while (!$packageQueue->isEmpty()) {
  1008. $package = $packageQueue->dequeue();
  1009. if (isset($seen[$package->getId()])) {
  1010. continue;
  1011. }
  1012. $seen[$package->getId()] = true;
  1013. $this->updateWhitelist[$package->getName()] = true;
  1014. if (!$this->whitelistDependencies) {
  1015. continue;
  1016. }
  1017. $requires = $package->getRequires();
  1018. foreach ($requires as $require) {
  1019. $requirePackages = $pool->whatProvides($require->getTarget());
  1020. foreach ($requirePackages as $requirePackage) {
  1021. if (isset($skipPackages[$requirePackage->getName()])) {
  1022. $this->io->writeError('<warning>Dependency "' . $requirePackage->getName() . '" is also a root requirement, but is not explicitly whitelisted. Ignoring.</warning>');
  1023. continue;
  1024. }
  1025. $packageQueue->enqueue($requirePackage);
  1026. }
  1027. }
  1028. }
  1029. }
  1030. }
  1031. /**
  1032. * Replace local repositories with InstalledArrayRepository instances
  1033. *
  1034. * This is to prevent any accidental modification of the existing repos on disk
  1035. *
  1036. * @param RepositoryManager $rm
  1037. */
  1038. private function mockLocalRepositories(RepositoryManager $rm)
  1039. {
  1040. $packages = array();
  1041. foreach ($rm->getLocalRepository()->getPackages() as $package) {
  1042. $packages[(string) $package] = clone $package;
  1043. }
  1044. foreach ($packages as $key => $package) {
  1045. if ($package instanceof AliasPackage) {
  1046. $alias = (string) $package->getAliasOf();
  1047. $packages[$key] = new AliasPackage($packages[$alias], $package->getVersion(), $package->getPrettyVersion());
  1048. }
  1049. }
  1050. $rm->setLocalRepository(
  1051. new InstalledArrayRepository($packages)
  1052. );
  1053. }
  1054. /**
  1055. * Create Installer
  1056. *
  1057. * @param IOInterface $io
  1058. * @param Composer $composer
  1059. * @return Installer
  1060. */
  1061. public static function create(IOInterface $io, Composer $composer)
  1062. {
  1063. return new static(
  1064. $io,
  1065. $composer->getConfig(),
  1066. $composer->getPackage(),
  1067. $composer->getDownloadManager(),
  1068. $composer->getRepositoryManager(),
  1069. $composer->getLocker(),
  1070. $composer->getInstallationManager(),
  1071. $composer->getEventDispatcher(),
  1072. $composer->getAutoloadGenerator()
  1073. );
  1074. }
  1075. /**
  1076. * @param RepositoryInterface $additionalInstalledRepository
  1077. * @return $this
  1078. */
  1079. public function setAdditionalInstalledRepository(RepositoryInterface $additionalInstalledRepository)
  1080. {
  1081. $this->additionalInstalledRepository = $additionalInstalledRepository;
  1082. return $this;
  1083. }
  1084. /**
  1085. * Whether to run in drymode or not
  1086. *
  1087. * @param bool $dryRun
  1088. * @return Installer
  1089. */
  1090. public function setDryRun($dryRun = true)
  1091. {
  1092. $this->dryRun = (boolean) $dryRun;
  1093. return $this;
  1094. }
  1095. /**
  1096. * Checks, if this is a dry run (simulation mode).
  1097. *
  1098. * @return bool
  1099. */
  1100. public function isDryRun()
  1101. {
  1102. return $this->dryRun;
  1103. }
  1104. /**
  1105. * prefer source installation
  1106. *
  1107. * @param bool $preferSource
  1108. * @return Installer
  1109. */
  1110. public function setPreferSource($preferSource = true)
  1111. {
  1112. $this->preferSource = (boolean) $preferSource;
  1113. return $this;
  1114. }
  1115. /**
  1116. * prefer dist installation
  1117. *
  1118. * @param bool $preferDist
  1119. * @return Installer
  1120. */
  1121. public function setPreferDist($preferDist = true)
  1122. {
  1123. $this->preferDist = (boolean) $preferDist;
  1124. return $this;
  1125. }
  1126. /**
  1127. * Whether or not generated autoloader are optimized
  1128. *
  1129. * @param bool $optimizeAutoloader
  1130. * @return Installer
  1131. */
  1132. public function setOptimizeAutoloader($optimizeAutoloader = false)
  1133. {
  1134. $this->optimizeAutoloader = (boolean) $optimizeAutoloader;
  1135. if (!$this->optimizeAutoloader) {
  1136. // Force classMapAuthoritative off when not optimizing the
  1137. // autoloader
  1138. $this->setClassMapAuthoritative(false);
  1139. }
  1140. return $this;
  1141. }
  1142. /**
  1143. * Whether or not generated autoloader considers the class map
  1144. * authoritative.
  1145. *
  1146. * @param bool $classMapAuthoritative
  1147. * @return Installer
  1148. */
  1149. public function setClassMapAuthoritative($classMapAuthoritative = false)
  1150. {
  1151. $this->classMapAuthoritative = (boolean) $classMapAuthoritative;
  1152. if ($this->classMapAuthoritative) {
  1153. // Force optimizeAutoloader when classmap is authoritative
  1154. $this->setOptimizeAutoloader(true);
  1155. }
  1156. return $this;
  1157. }
  1158. /**
  1159. * update packages
  1160. *
  1161. * @param bool $update
  1162. * @return Installer
  1163. */
  1164. public function setUpdate($update = true)
  1165. {
  1166. $this->update = (boolean) $update;
  1167. return $this;
  1168. }
  1169. /**
  1170. * enables dev packages
  1171. *
  1172. * @param bool $devMode
  1173. * @return Installer
  1174. */
  1175. public function setDevMode($devMode = true)
  1176. {
  1177. $this->devMode = (boolean) $devMode;
  1178. return $this;
  1179. }
  1180. /**
  1181. * set whether to run autoloader or not
  1182. *
  1183. * @param bool $dumpAutoloader
  1184. * @return Installer
  1185. */
  1186. public function setDumpAutoloader($dumpAutoloader = true)
  1187. {
  1188. $this->dumpAutoloader = (boolean) $dumpAutoloader;
  1189. return $this;
  1190. }
  1191. /**
  1192. * set whether to run scripts or not
  1193. *
  1194. * @param bool $runScripts
  1195. * @return Installer
  1196. */
  1197. public function setRunScripts($runScripts = true)
  1198. {
  1199. $this->runScripts = (boolean) $runScripts;
  1200. return $this;
  1201. }
  1202. /**
  1203. * set the config instance
  1204. *
  1205. * @param Config $config
  1206. * @return Installer
  1207. */
  1208. public function setConfig(Config $config)
  1209. {
  1210. $this->config = $config;
  1211. return $this;
  1212. }
  1213. /**
  1214. * run in verbose mode
  1215. *
  1216. * @param bool $verbose
  1217. * @return Installer
  1218. */
  1219. public function setVerbose($verbose = true)
  1220. {
  1221. $this->verbose = (boolean) $verbose;
  1222. return $this;
  1223. }
  1224. /**
  1225. * Checks, if running in verbose mode.
  1226. *
  1227. * @return bool
  1228. */
  1229. public function isVerbose()
  1230. {
  1231. return $this->verbose;
  1232. }
  1233. /**
  1234. * set ignore Platform Package requirements
  1235. *
  1236. * @param bool $ignorePlatformReqs
  1237. * @return Installer
  1238. */
  1239. public function setIgnorePlatformRequirements($ignorePlatformReqs = false)
  1240. {
  1241. $this->ignorePlatformReqs = (boolean) $ignorePlatformReqs;
  1242. return $this;
  1243. }
  1244. /**
  1245. * restrict the update operation to a few packages, all other packages
  1246. * that are already installed will be kept at their current version
  1247. *
  1248. * @param array $packages
  1249. * @return Installer
  1250. */
  1251. public function setUpdateWhitelist(array $packages)
  1252. {
  1253. $this->updateWhitelist = array_flip(array_map('strtolower', $packages));
  1254. return $this;
  1255. }
  1256. /**
  1257. * Should dependencies of whitelisted packages be updated recursively?
  1258. *
  1259. * @param bool $updateDependencies
  1260. * @return Installer
  1261. */
  1262. public function setWhitelistDependencies($updateDependencies = true)
  1263. {
  1264. $this->whitelistDependencies = (boolean) $updateDependencies;
  1265. return $this;
  1266. }
  1267. /**
  1268. * Should packages be preferred in a stable version when updating?
  1269. *
  1270. * @param bool $preferStable
  1271. * @return Installer
  1272. */
  1273. public function setPreferStable($preferStable = true)
  1274. {
  1275. $this->preferStable = (boolean) $preferStable;
  1276. return $this;
  1277. }
  1278. /**
  1279. * Should packages be preferred in a lowest version when updating?
  1280. *
  1281. * @param bool $preferLowest
  1282. * @return Installer
  1283. */
  1284. public function setPreferLowest($preferLowest = true)
  1285. {
  1286. $this->preferLowest = (boolean) $preferLowest;
  1287. return $this;
  1288. }
  1289. /**
  1290. * Disables plugins.
  1291. *
  1292. * Call this if you want to ensure that third-party code never gets
  1293. * executed. The default is to automatically install, and execute
  1294. * custom third-party installers.
  1295. *
  1296. * @return Installer
  1297. */
  1298. public function disablePlugins()
  1299. {
  1300. $this->installationManager->disablePlugins();
  1301. return $this;
  1302. }
  1303. /**
  1304. * @param SuggestedPackagesReporter $suggestedPackagesReporter
  1305. * @return Installer
  1306. */
  1307. public function setSuggestedPackagesReporter(SuggestedPackagesReporter $suggestedPackagesReporter)
  1308. {
  1309. $this->suggestedPackagesReporter = $suggestedPackagesReporter;
  1310. return $this;
  1311. }
  1312. }