Installer.php 57 KB

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