Installer.php 57 KB

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