Installer.php 58 KB

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