Installer.php 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  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\Pool;
  19. use Composer\DependencyResolver\Request;
  20. use Composer\DependencyResolver\Rule;
  21. use Composer\DependencyResolver\Solver;
  22. use Composer\DependencyResolver\SolverProblemsException;
  23. use Composer\Downloader\DownloadManager;
  24. use Composer\EventDispatcher\EventDispatcher;
  25. use Composer\Installer\InstallationManager;
  26. use Composer\Config;
  27. use Composer\Installer\NoopInstaller;
  28. use Composer\IO\IOInterface;
  29. use Composer\Json\JsonFile;
  30. use Composer\Package\AliasPackage;
  31. use Composer\Package\Link;
  32. use Composer\Package\LinkConstraint\VersionConstraint;
  33. use Composer\Package\Locker;
  34. use Composer\Package\PackageInterface;
  35. use Composer\Package\RootPackageInterface;
  36. use Composer\Repository\CompositeRepository;
  37. use Composer\Repository\InstalledArrayRepository;
  38. use Composer\Repository\InstalledFilesystemRepository;
  39. use Composer\Repository\PlatformRepository;
  40. use Composer\Repository\RepositoryInterface;
  41. use Composer\Repository\RepositoryManager;
  42. use Composer\Script\ScriptEvents;
  43. /**
  44. * @author Jordi Boggiano <j.boggiano@seld.be>
  45. * @author Beau Simensen <beau@dflydev.com>
  46. * @author Konstantin Kudryashov <ever.zet@gmail.com>
  47. * @author Nils Adermann <naderman@naderman.de>
  48. */
  49. class Installer
  50. {
  51. /**
  52. * @var IOInterface
  53. */
  54. protected $io;
  55. /**
  56. * @var Config
  57. */
  58. protected $config;
  59. /**
  60. * @var RootPackageInterface
  61. */
  62. protected $package;
  63. /**
  64. * @var DownloadManager
  65. */
  66. protected $downloadManager;
  67. /**
  68. * @var RepositoryManager
  69. */
  70. protected $repositoryManager;
  71. /**
  72. * @var Locker
  73. */
  74. protected $locker;
  75. /**
  76. * @var InstallationManager
  77. */
  78. protected $installationManager;
  79. /**
  80. * @var EventDispatcher
  81. */
  82. protected $eventDispatcher;
  83. /**
  84. * @var AutoloadGenerator
  85. */
  86. protected $autoloadGenerator;
  87. protected $preferSource = false;
  88. protected $preferDist = false;
  89. protected $optimizeAutoloader = false;
  90. protected $devMode = false;
  91. protected $dryRun = false;
  92. protected $verbose = false;
  93. protected $update = false;
  94. protected $runScripts = true;
  95. protected $updateWhitelist = null;
  96. protected $whitelistDependencies = false;
  97. /**
  98. * @var array
  99. */
  100. protected $suggestedPackages;
  101. /**
  102. * @var RepositoryInterface
  103. */
  104. protected $additionalInstalledRepository;
  105. /**
  106. * Constructor
  107. *
  108. * @param IOInterface $io
  109. * @param Config $config
  110. * @param RootPackageInterface $package
  111. * @param DownloadManager $downloadManager
  112. * @param RepositoryManager $repositoryManager
  113. * @param Locker $locker
  114. * @param InstallationManager $installationManager
  115. * @param EventDispatcher $eventDispatcher
  116. * @param AutoloadGenerator $autoloadGenerator
  117. */
  118. public function __construct(IOInterface $io, Config $config, RootPackageInterface $package, DownloadManager $downloadManager, RepositoryManager $repositoryManager, Locker $locker, InstallationManager $installationManager, EventDispatcher $eventDispatcher, AutoloadGenerator $autoloadGenerator)
  119. {
  120. $this->io = $io;
  121. $this->config = $config;
  122. $this->package = $package;
  123. $this->downloadManager = $downloadManager;
  124. $this->repositoryManager = $repositoryManager;
  125. $this->locker = $locker;
  126. $this->installationManager = $installationManager;
  127. $this->eventDispatcher = $eventDispatcher;
  128. $this->autoloadGenerator = $autoloadGenerator;
  129. }
  130. /**
  131. * Run installation (or update)
  132. */
  133. public function run()
  134. {
  135. if ($this->dryRun) {
  136. $this->verbose = true;
  137. $this->runScripts = false;
  138. $this->installationManager->addInstaller(new NoopInstaller);
  139. $this->mockLocalRepositories($this->repositoryManager);
  140. }
  141. // TODO remove this BC feature at some point
  142. // purge old require-dev packages to avoid conflicts with the new way of handling dev requirements
  143. $devRepo = new InstalledFilesystemRepository(new JsonFile($this->config->get('vendor-dir').'/composer/installed_dev.json'));
  144. if ($devRepo->getPackages()) {
  145. $this->io->write('<warning>BC Notice: Removing old dev packages to migrate to the new require-dev handling.</warning>');
  146. foreach ($devRepo->getPackages() as $package) {
  147. if ($this->installationManager->isPackageInstalled($devRepo, $package)) {
  148. $this->installationManager->uninstall($devRepo, new UninstallOperation($package));
  149. }
  150. }
  151. unlink($this->config->get('vendor-dir').'/composer/installed_dev.json');
  152. }
  153. unset($devRepo, $package);
  154. // end BC
  155. if ($this->preferSource) {
  156. $this->downloadManager->setPreferSource(true);
  157. }
  158. if ($this->preferDist) {
  159. $this->downloadManager->setPreferDist(true);
  160. }
  161. // clone root package to have one in the installed repo that does not require anything
  162. // we don't want it to be uninstallable, but its requirements should not conflict
  163. // with the lock file for example
  164. $installedRootPackage = clone $this->package;
  165. $installedRootPackage->setRequires(array());
  166. $installedRootPackage->setDevRequires(array());
  167. // create installed repo, this contains all local packages + platform packages (php & extensions)
  168. $localRepo = $this->repositoryManager->getLocalRepository();
  169. $platformRepo = new PlatformRepository();
  170. $repos = array(
  171. $localRepo,
  172. new InstalledArrayRepository(array($installedRootPackage)),
  173. $platformRepo,
  174. );
  175. $installedRepo = new CompositeRepository($repos);
  176. if ($this->additionalInstalledRepository) {
  177. $installedRepo->addRepository($this->additionalInstalledRepository);
  178. }
  179. $aliases = $this->getRootAliases();
  180. $this->aliasPlatformPackages($platformRepo, $aliases);
  181. if ($this->runScripts) {
  182. // dispatch pre event
  183. $eventName = $this->update ? ScriptEvents::PRE_UPDATE_CMD : ScriptEvents::PRE_INSTALL_CMD;
  184. $this->eventDispatcher->dispatchCommandEvent($eventName, $this->devMode);
  185. }
  186. try {
  187. $this->suggestedPackages = array();
  188. if (!$this->doInstall($localRepo, $installedRepo, $platformRepo, $aliases, $this->devMode)) {
  189. return false;
  190. }
  191. } catch (\Exception $e) {
  192. $this->installationManager->notifyInstalls();
  193. throw $e;
  194. }
  195. $this->installationManager->notifyInstalls();
  196. // output suggestions
  197. foreach ($this->suggestedPackages as $suggestion) {
  198. $target = $suggestion['target'];
  199. foreach ($installedRepo->getPackages() as $package) {
  200. if (in_array($target, $package->getNames())) {
  201. continue 2;
  202. }
  203. }
  204. $this->io->write($suggestion['source'].' suggests installing '.$suggestion['target'].' ('.$suggestion['reason'].')');
  205. }
  206. if (!$this->dryRun) {
  207. // write lock
  208. if ($this->update || !$this->locker->isLocked()) {
  209. $localRepo->reload();
  210. // if this is not run in dev mode and the root has dev requires, the lock must
  211. // contain null to prevent dev installs from a non-dev lock
  212. $devPackages = ($this->devMode || !$this->package->getDevRequires()) ? array() : null;
  213. // split dev and non-dev requirements by checking what would be removed if we update without the dev requirements
  214. if ($this->devMode && $this->package->getDevRequires()) {
  215. $policy = $this->createPolicy();
  216. $pool = $this->createPool();
  217. $pool->addRepository($installedRepo, $aliases);
  218. // creating requirements request
  219. $request = $this->createRequest($pool, $this->package, $platformRepo);
  220. $request->updateAll();
  221. foreach ($this->package->getRequires() as $link) {
  222. $request->install($link->getTarget(), $link->getConstraint());
  223. }
  224. $solver = new Solver($policy, $pool, $installedRepo);
  225. $ops = $solver->solve($request);
  226. foreach ($ops as $op) {
  227. if ($op->getJobType() === 'uninstall') {
  228. $devPackages[] = $op->getPackage();
  229. }
  230. }
  231. }
  232. $platformReqs = $this->extractPlatformRequirements($this->package->getRequires());
  233. $platformDevReqs = $this->devMode ? $this->extractPlatformRequirements($this->package->getDevRequires()) : array();
  234. $updatedLock = $this->locker->setLockData(
  235. array_diff($localRepo->getCanonicalPackages(), (array) $devPackages),
  236. $devPackages,
  237. $platformReqs,
  238. $platformDevReqs,
  239. $aliases,
  240. $this->package->getMinimumStability(),
  241. $this->package->getStabilityFlags()
  242. );
  243. if ($updatedLock) {
  244. $this->io->write('<info>Writing lock file</info>');
  245. }
  246. }
  247. // write autoloader
  248. $this->io->write('<info>Generating autoload files</info>');
  249. $this->autoloadGenerator->dump($this->config, $localRepo, $this->package, $this->installationManager, 'composer', $this->optimizeAutoloader);
  250. if ($this->runScripts) {
  251. // dispatch post event
  252. $eventName = $this->update ? ScriptEvents::POST_UPDATE_CMD : ScriptEvents::POST_INSTALL_CMD;
  253. $this->eventDispatcher->dispatchCommandEvent($eventName, $this->devMode);
  254. }
  255. }
  256. return true;
  257. }
  258. protected function doInstall($localRepo, $installedRepo, $platformRepo, $aliases, $withDevReqs)
  259. {
  260. // init vars
  261. $lockedRepository = null;
  262. $repositories = null;
  263. // initialize locker to create aliased packages
  264. $installFromLock = false;
  265. if (!$this->update && $this->locker->isLocked()) {
  266. $installFromLock = true;
  267. try {
  268. $lockedRepository = $this->locker->getLockedRepository($withDevReqs);
  269. } catch (\RuntimeException $e) {
  270. // if there are dev requires, then we really can not install
  271. if ($this->package->getDevRequires()) {
  272. throw $e;
  273. }
  274. // no require-dev in composer.json and the lock file was created with no dev info, so skip them
  275. $lockedRepository = $this->locker->getLockedRepository();
  276. }
  277. }
  278. $this->whitelistUpdateDependencies(
  279. $localRepo,
  280. $withDevReqs,
  281. $this->package->getRequires(),
  282. $this->package->getDevRequires()
  283. );
  284. $this->io->write('<info>Loading composer repositories with package information</info>');
  285. // creating repository pool
  286. $policy = $this->createPolicy();
  287. $pool = $this->createPool();
  288. $pool->addRepository($installedRepo, $aliases);
  289. if ($installFromLock) {
  290. $pool->addRepository($lockedRepository, $aliases);
  291. }
  292. if (!$installFromLock) {
  293. $repositories = $this->repositoryManager->getRepositories();
  294. foreach ($repositories as $repository) {
  295. $pool->addRepository($repository, $aliases);
  296. }
  297. }
  298. // creating requirements request
  299. $request = $this->createRequest($pool, $this->package, $platformRepo);
  300. if (!$installFromLock) {
  301. // remove unstable packages from the localRepo if they don't match the current stability settings
  302. $removedUnstablePackages = array();
  303. foreach ($localRepo->getPackages() as $package) {
  304. if (
  305. !$pool->isPackageAcceptable($package->getNames(), $package->getStability())
  306. && $this->installationManager->isPackageInstalled($localRepo, $package)
  307. ) {
  308. $removedUnstablePackages[$package->getName()] = true;
  309. $request->remove($package->getName(), new VersionConstraint('=', $package->getVersion()));
  310. }
  311. }
  312. }
  313. if ($this->update) {
  314. $this->io->write('<info>Updating dependencies'.($withDevReqs?' (including require-dev)':'').'</info>');
  315. $request->updateAll();
  316. if ($withDevReqs) {
  317. $links = array_merge($this->package->getRequires(), $this->package->getDevRequires());
  318. } else {
  319. $links = $this->package->getRequires();
  320. }
  321. foreach ($links as $link) {
  322. $request->install($link->getTarget(), $link->getConstraint());
  323. }
  324. // if the updateWhitelist is enabled, packages not in it are also fixed
  325. // to the version specified in the lock, or their currently installed version
  326. if ($this->updateWhitelist) {
  327. if ($this->locker->isLocked()) {
  328. try {
  329. $currentPackages = $this->locker->getLockedRepository($withDevReqs)->getPackages();
  330. } catch (\RuntimeException $e) {
  331. // fetch only non-dev packages from lock if doing a dev update fails due to a previously incomplete lock file
  332. $currentPackages = $this->locker->getLockedRepository()->getPackages();
  333. }
  334. } else {
  335. $currentPackages = $installedRepo->getPackages();
  336. }
  337. // collect packages to fixate from root requirements as well as installed packages
  338. $candidates = array();
  339. foreach ($links as $link) {
  340. $candidates[$link->getTarget()] = true;
  341. }
  342. foreach ($localRepo->getPackages() as $package) {
  343. $candidates[$package->getName()] = true;
  344. }
  345. // fix them to the version in lock (or currently installed) if they are not updateable
  346. foreach ($candidates as $candidate => $dummy) {
  347. foreach ($currentPackages as $curPackage) {
  348. if ($curPackage->getName() === $candidate) {
  349. if (!$this->isUpdateable($curPackage) && !isset($removedUnstablePackages[$curPackage->getName()])) {
  350. $constraint = new VersionConstraint('=', $curPackage->getVersion());
  351. $request->install($curPackage->getName(), $constraint);
  352. }
  353. break;
  354. }
  355. }
  356. }
  357. }
  358. } elseif ($installFromLock) {
  359. $this->io->write('<info>Installing dependencies'.($withDevReqs?' (including require-dev)':'').' from lock file</info>');
  360. if (!$this->locker->isFresh()) {
  361. $this->io->write('<warning>Warning: The lock file is not up to date with the latest changes in composer.json. You may be getting outdated dependencies. Run update to update them.</warning>');
  362. }
  363. foreach ($lockedRepository->getPackages() as $package) {
  364. $version = $package->getVersion();
  365. if (isset($aliases[$package->getName()][$version])) {
  366. $version = $aliases[$package->getName()][$version]['alias_normalized'];
  367. }
  368. $constraint = new VersionConstraint('=', $version);
  369. $constraint->setPrettyString($package->getPrettyVersion());
  370. $request->install($package->getName(), $constraint);
  371. }
  372. foreach ($this->locker->getPlatformRequirements($withDevReqs) as $link) {
  373. $request->install($link->getTarget(), $link->getConstraint());
  374. }
  375. } else {
  376. $this->io->write('<info>Installing dependencies'.($withDevReqs?' (including require-dev)':'').'</info>');
  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. }
  386. // force dev packages to have the latest links if we update or install from a (potentially new) lock
  387. $this->processDevPackages($localRepo, $pool, $policy, $repositories, $lockedRepository, $installFromLock, 'force-links');
  388. // solve dependencies
  389. $solver = new Solver($policy, $pool, $installedRepo);
  390. try {
  391. $operations = $solver->solve($request);
  392. } catch (SolverProblemsException $e) {
  393. $this->io->write('<error>Your requirements could not be resolved to an installable set of packages.</error>');
  394. $this->io->write($e->getMessage());
  395. return false;
  396. }
  397. // force dev packages to be updated if we update or install from a (potentially new) lock
  398. $operations = $this->processDevPackages($localRepo, $pool, $policy, $repositories, $lockedRepository, $installFromLock, 'force-updates', $operations);
  399. // execute operations
  400. if (!$operations) {
  401. $this->io->write('Nothing to install or update');
  402. }
  403. $operations = $this->movePluginsToFront($operations);
  404. foreach ($operations as $operation) {
  405. // collect suggestions
  406. if ('install' === $operation->getJobType()) {
  407. foreach ($operation->getPackage()->getSuggests() as $target => $reason) {
  408. $this->suggestedPackages[] = array(
  409. 'source' => $operation->getPackage()->getPrettyName(),
  410. 'target' => $target,
  411. 'reason' => $reason,
  412. );
  413. }
  414. }
  415. $event = 'Composer\Script\ScriptEvents::PRE_PACKAGE_'.strtoupper($operation->getJobType());
  416. if (defined($event) && $this->runScripts) {
  417. $this->eventDispatcher->dispatchPackageEvent(constant($event), $this->devMode, $operation);
  418. }
  419. // not installing from lock, force dev packages' references if they're in root package refs
  420. if (!$installFromLock) {
  421. $package = null;
  422. if ('update' === $operation->getJobType()) {
  423. $package = $operation->getTargetPackage();
  424. } elseif ('install' === $operation->getJobType()) {
  425. $package = $operation->getPackage();
  426. }
  427. if ($package && $package->isDev()) {
  428. $references = $this->package->getReferences();
  429. if (isset($references[$package->getName()])) {
  430. $package->setSourceReference($references[$package->getName()]);
  431. $package->setDistReference($references[$package->getName()]);
  432. }
  433. }
  434. }
  435. // output non-alias ops in dry run, output alias ops in debug verbosity
  436. if ($this->dryRun && false === strpos($operation->getJobType(), 'Alias')) {
  437. $this->io->write(' - ' . $operation);
  438. $this->io->write('');
  439. } elseif ($this->io->isDebug() && false !== strpos($operation->getJobType(), 'Alias')) {
  440. $this->io->write(' - ' . $operation);
  441. $this->io->write('');
  442. }
  443. $this->installationManager->execute($localRepo, $operation);
  444. // output reasons why the operation was ran, only for install/update operations
  445. if ($this->verbose && $this->io->isVeryVerbose() && in_array($operation->getJobType(), array('install', 'update'))) {
  446. $reason = $operation->getReason();
  447. if ($reason instanceof Rule) {
  448. switch ($reason->getReason()) {
  449. case Rule::RULE_JOB_INSTALL:
  450. $this->io->write(' REASON: Required by root: '.$reason->getRequiredPackage());
  451. $this->io->write('');
  452. break;
  453. case Rule::RULE_PACKAGE_REQUIRES:
  454. $this->io->write(' REASON: '.$reason->getPrettyString());
  455. $this->io->write('');
  456. break;
  457. }
  458. }
  459. }
  460. $event = 'Composer\Script\ScriptEvents::POST_PACKAGE_'.strtoupper($operation->getJobType());
  461. if (defined($event) && $this->runScripts) {
  462. $this->eventDispatcher->dispatchPackageEvent(constant($event), $this->devMode, $operation);
  463. }
  464. if (!$this->dryRun) {
  465. $localRepo->write();
  466. }
  467. }
  468. return true;
  469. }
  470. /**
  471. * Workaround: if your packages depend on plugins, we must be sure
  472. * that those are installed / updated first; else it would lead to packages
  473. * being installed multiple times in different folders, when running Composer
  474. * twice.
  475. *
  476. * While this does not fix the root-causes of https://github.com/composer/composer/issues/1147,
  477. * it at least fixes the symptoms and makes usage of composer possible (again)
  478. * in such scenarios.
  479. *
  480. * @param OperationInterface[] $operations
  481. * @return OperationInterface[] reordered operation list
  482. */
  483. private function movePluginsToFront(array $operations)
  484. {
  485. $installerOps = array();
  486. foreach ($operations as $idx => $op) {
  487. if ($op instanceof InstallOperation) {
  488. $package = $op->getPackage();
  489. } elseif ($op instanceof UpdateOperation) {
  490. $package = $op->getTargetPackage();
  491. } else {
  492. continue;
  493. }
  494. if ($package->getRequires() === array() && ($package->getType() === 'composer-plugin' || $package->getType() === 'composer-installer')) {
  495. $installerOps[] = $op;
  496. unset($operations[$idx]);
  497. }
  498. }
  499. return array_merge($installerOps, $operations);
  500. }
  501. private function createPool()
  502. {
  503. $minimumStability = $this->package->getMinimumStability();
  504. $stabilityFlags = $this->package->getStabilityFlags();
  505. if (!$this->update && $this->locker->isLocked()) {
  506. $minimumStability = $this->locker->getMinimumStability();
  507. $stabilityFlags = $this->locker->getStabilityFlags();
  508. }
  509. return new Pool($minimumStability, $stabilityFlags);
  510. }
  511. private function createPolicy()
  512. {
  513. return new DefaultPolicy($this->package->getPreferStable());
  514. }
  515. private function createRequest(Pool $pool, RootPackageInterface $rootPackage, PlatformRepository $platformRepo)
  516. {
  517. $request = new Request($pool);
  518. $constraint = new VersionConstraint('=', $rootPackage->getVersion());
  519. $constraint->setPrettyString($rootPackage->getPrettyVersion());
  520. $request->install($rootPackage->getName(), $constraint);
  521. $fixedPackages = $platformRepo->getPackages();
  522. if ($this->additionalInstalledRepository) {
  523. $additionalFixedPackages = $this->additionalInstalledRepository->getPackages();
  524. $fixedPackages = array_merge($fixedPackages, $additionalFixedPackages);
  525. }
  526. // fix the version of all platform packages + additionally installed packages
  527. // to prevent the solver trying to remove or update those
  528. $provided = $rootPackage->getProvides();
  529. foreach ($fixedPackages as $package) {
  530. $constraint = new VersionConstraint('=', $package->getVersion());
  531. $constraint->setPrettyString($package->getPrettyVersion());
  532. // skip platform packages that are provided by the root package
  533. if ($package->getRepository() !== $platformRepo
  534. || !isset($provided[$package->getName()])
  535. || !$provided[$package->getName()]->getConstraint()->matches($constraint)
  536. ) {
  537. $request->install($package->getName(), $constraint);
  538. }
  539. }
  540. return $request;
  541. }
  542. private function processDevPackages($localRepo, $pool, $policy, $repositories, $lockedRepository, $installFromLock, $task, array $operations = null)
  543. {
  544. if ($task === 'force-updates' && null === $operations) {
  545. throw new \InvalidArgumentException('Missing operations argument');
  546. }
  547. if ($task === 'force-links') {
  548. $operations = array();
  549. }
  550. foreach ($localRepo->getCanonicalPackages() as $package) {
  551. // skip non-dev packages
  552. if (!$package->isDev()) {
  553. continue;
  554. }
  555. // skip packages that will be updated/uninstalled
  556. foreach ($operations as $operation) {
  557. if (('update' === $operation->getJobType() && $operation->getInitialPackage()->equals($package))
  558. || ('uninstall' === $operation->getJobType() && $operation->getPackage()->equals($package))
  559. ) {
  560. continue 2;
  561. }
  562. }
  563. // force update to locked version if it does not match the installed version
  564. if ($installFromLock) {
  565. foreach ($lockedRepository->findPackages($package->getName()) as $lockedPackage) {
  566. if ($lockedPackage->isDev() && $lockedPackage->getVersion() === $package->getVersion()) {
  567. if ($task === 'force-links') {
  568. $package->setRequires($lockedPackage->getRequires());
  569. $package->setConflicts($lockedPackage->getConflicts());
  570. $package->setProvides($lockedPackage->getProvides());
  571. $package->setReplaces($lockedPackage->getReplaces());
  572. } elseif ($task === 'force-updates') {
  573. if (($lockedPackage->getSourceReference() && $lockedPackage->getSourceReference() !== $package->getSourceReference())
  574. || ($lockedPackage->getDistReference() && $lockedPackage->getDistReference() !== $package->getDistReference())
  575. ) {
  576. $operations[] = new UpdateOperation($package, $lockedPackage);
  577. }
  578. }
  579. break;
  580. }
  581. }
  582. } else {
  583. // force update to latest on update
  584. if ($this->update) {
  585. // skip package if the whitelist is enabled and it is not in it
  586. if ($this->updateWhitelist && !$this->isUpdateable($package)) {
  587. continue;
  588. }
  589. // find similar packages (name/version) in all repositories
  590. $matches = $pool->whatProvides($package->getName(), new VersionConstraint('=', $package->getVersion()));
  591. foreach ($matches as $index => $match) {
  592. // skip local packages
  593. if (!in_array($match->getRepository(), $repositories, true)) {
  594. unset($matches[$index]);
  595. continue;
  596. }
  597. // skip providers/replacers
  598. if ($match->getName() !== $package->getName()) {
  599. unset($matches[$index]);
  600. continue;
  601. }
  602. $matches[$index] = $match->getId();
  603. }
  604. // select prefered package according to policy rules
  605. if ($matches && $matches = $policy->selectPreferedPackages($pool, array(), $matches)) {
  606. $newPackage = $pool->literalToPackage($matches[0]);
  607. if ($task === 'force-links' && $newPackage) {
  608. $package->setRequires($newPackage->getRequires());
  609. $package->setConflicts($newPackage->getConflicts());
  610. $package->setProvides($newPackage->getProvides());
  611. $package->setReplaces($newPackage->getReplaces());
  612. }
  613. if ($task === 'force-updates' && $newPackage && (
  614. (($newPackage->getSourceReference() && $newPackage->getSourceReference() !== $package->getSourceReference())
  615. || ($newPackage->getDistReference() && $newPackage->getDistReference() !== $package->getDistReference())
  616. )
  617. )) {
  618. $operations[] = new UpdateOperation($package, $newPackage);
  619. }
  620. }
  621. }
  622. if ($task === 'force-updates') {
  623. // force installed package to update to referenced version if it does not match the installed version
  624. $references = $this->package->getReferences();
  625. if (isset($references[$package->getName()]) && $references[$package->getName()] !== $package->getSourceReference()) {
  626. // changing the source ref to update to will be handled in the operations loop below
  627. $operations[] = new UpdateOperation($package, clone $package);
  628. }
  629. }
  630. }
  631. }
  632. return $operations;
  633. }
  634. private function getRootAliases()
  635. {
  636. if (!$this->update && $this->locker->isLocked()) {
  637. $aliases = $this->locker->getAliases();
  638. } else {
  639. $aliases = $this->package->getAliases();
  640. }
  641. $normalizedAliases = array();
  642. foreach ($aliases as $alias) {
  643. $normalizedAliases[$alias['package']][$alias['version']] = array(
  644. 'alias' => $alias['alias'],
  645. 'alias_normalized' => $alias['alias_normalized']
  646. );
  647. }
  648. return $normalizedAliases;
  649. }
  650. private function aliasPlatformPackages(PlatformRepository $platformRepo, $aliases)
  651. {
  652. foreach ($aliases as $package => $versions) {
  653. foreach ($versions as $version => $alias) {
  654. $packages = $platformRepo->findPackages($package, $version);
  655. foreach ($packages as $package) {
  656. $aliasPackage = new AliasPackage($package, $alias['alias_normalized'], $alias['alias']);
  657. $aliasPackage->setRootPackageAlias(true);
  658. $platformRepo->addPackage($aliasPackage);
  659. }
  660. }
  661. }
  662. }
  663. private function isUpdateable(PackageInterface $package)
  664. {
  665. if (!$this->updateWhitelist) {
  666. throw new \LogicException('isUpdateable should only be called when a whitelist is present');
  667. }
  668. foreach ($this->updateWhitelist as $whiteListedPattern => $void) {
  669. $cleanedWhiteListedPattern = str_replace('\\*', '.*', preg_quote($whiteListedPattern));
  670. if (preg_match("{^".$cleanedWhiteListedPattern."$}i", $package->getName())) {
  671. return true;
  672. }
  673. }
  674. return false;
  675. }
  676. private function extractPlatformRequirements($links)
  677. {
  678. $platformReqs = array();
  679. foreach ($links as $link) {
  680. if (preg_match(PlatformRepository::PLATFORM_PACKAGE_REGEX, $link->getTarget())) {
  681. $platformReqs[$link->getTarget()] = $link->getPrettyConstraint();
  682. }
  683. }
  684. return $platformReqs;
  685. }
  686. /**
  687. * Adds all dependencies of the update whitelist to the whitelist, too.
  688. *
  689. * Packages which are listed as requirements in the root package will be
  690. * skipped including their dependencies, unless they are listed in the
  691. * update whitelist themselves.
  692. *
  693. * @param RepositoryInterface $localRepo
  694. * @param boolean $devMode
  695. * @param array $rootRequires An array of links to packages in require of the root package
  696. * @param array $rootDevRequires An array of links to packages in require-dev of the root package
  697. */
  698. private function whitelistUpdateDependencies($localRepo, $devMode, array $rootRequires, array $rootDevRequires)
  699. {
  700. if (!$this->updateWhitelist) {
  701. return;
  702. }
  703. $requiredPackageNames = array();
  704. foreach (array_merge($rootRequires, $rootDevRequires) as $require) {
  705. $requiredPackageNames[] = $require->getTarget();
  706. }
  707. if ($devMode) {
  708. $rootRequires = array_merge($rootRequires, $rootDevRequires);
  709. }
  710. $skipPackages = array();
  711. foreach ($rootRequires as $require) {
  712. $skipPackages[$require->getTarget()] = true;
  713. }
  714. $pool = new Pool;
  715. $pool->addRepository($localRepo);
  716. $seen = array();
  717. foreach ($this->updateWhitelist as $packageName => $void) {
  718. $packageQueue = new \SplQueue;
  719. $depPackages = $pool->whatProvides($packageName);
  720. if (count($depPackages) == 0 && !in_array($packageName, $requiredPackageNames) && !in_array($packageName, array('nothing', 'lock'))) {
  721. $this->io->write('<warning>Package "' . $packageName . '" listed for update is not installed. Ignoring.<warning>');
  722. }
  723. foreach ($depPackages as $depPackage) {
  724. $packageQueue->enqueue($depPackage);
  725. }
  726. while (!$packageQueue->isEmpty()) {
  727. $package = $packageQueue->dequeue();
  728. if (isset($seen[$package->getId()])) {
  729. continue;
  730. }
  731. $seen[$package->getId()] = true;
  732. $this->updateWhitelist[$package->getName()] = true;
  733. if (!$this->whitelistDependencies) {
  734. continue;
  735. }
  736. $requires = $package->getRequires();
  737. foreach ($requires as $require) {
  738. $requirePackages = $pool->whatProvides($require->getTarget());
  739. foreach ($requirePackages as $requirePackage) {
  740. if (isset($skipPackages[$requirePackage->getName()])) {
  741. continue;
  742. }
  743. $packageQueue->enqueue($requirePackage);
  744. }
  745. }
  746. }
  747. }
  748. }
  749. /**
  750. * Replace local repositories with InstalledArrayRepository instances
  751. *
  752. * This is to prevent any accidental modification of the existing repos on disk
  753. *
  754. * @param RepositoryManager $rm
  755. */
  756. private function mockLocalRepositories(RepositoryManager $rm)
  757. {
  758. $packages = array();
  759. foreach ($rm->getLocalRepository()->getPackages() as $package) {
  760. $packages[(string) $package] = clone $package;
  761. }
  762. foreach ($packages as $key => $package) {
  763. if ($package instanceof AliasPackage) {
  764. $alias = (string) $package->getAliasOf();
  765. $packages[$key] = new AliasPackage($packages[$alias], $package->getVersion(), $package->getPrettyVersion());
  766. }
  767. }
  768. $rm->setLocalRepository(
  769. new InstalledArrayRepository($packages)
  770. );
  771. }
  772. /**
  773. * Create Installer
  774. *
  775. * @param IOInterface $io
  776. * @param Composer $composer
  777. * @return Installer
  778. */
  779. public static function create(IOInterface $io, Composer $composer)
  780. {
  781. return new static(
  782. $io,
  783. $composer->getConfig(),
  784. $composer->getPackage(),
  785. $composer->getDownloadManager(),
  786. $composer->getRepositoryManager(),
  787. $composer->getLocker(),
  788. $composer->getInstallationManager(),
  789. $composer->getEventDispatcher(),
  790. $composer->getAutoloadGenerator()
  791. );
  792. }
  793. public function setAdditionalInstalledRepository(RepositoryInterface $additionalInstalledRepository)
  794. {
  795. $this->additionalInstalledRepository = $additionalInstalledRepository;
  796. return $this;
  797. }
  798. /**
  799. * Whether to run in drymode or not
  800. *
  801. * @param boolean $dryRun
  802. * @return Installer
  803. */
  804. public function setDryRun($dryRun = true)
  805. {
  806. $this->dryRun = (boolean) $dryRun;
  807. return $this;
  808. }
  809. /**
  810. * prefer source installation
  811. *
  812. * @param boolean $preferSource
  813. * @return Installer
  814. */
  815. public function setPreferSource($preferSource = true)
  816. {
  817. $this->preferSource = (boolean) $preferSource;
  818. return $this;
  819. }
  820. /**
  821. * prefer dist installation
  822. *
  823. * @param boolean $preferDist
  824. * @return Installer
  825. */
  826. public function setPreferDist($preferDist = true)
  827. {
  828. $this->preferDist = (boolean) $preferDist;
  829. return $this;
  830. }
  831. /**
  832. * Whether or not generated autoloader are optimized
  833. *
  834. * @param bool $optimizeAutoloader
  835. * @return Installer
  836. */
  837. public function setOptimizeAutoloader($optimizeAutoloader = false)
  838. {
  839. $this->optimizeAutoloader = (boolean) $optimizeAutoloader;
  840. return $this;
  841. }
  842. /**
  843. * update packages
  844. *
  845. * @param boolean $update
  846. * @return Installer
  847. */
  848. public function setUpdate($update = true)
  849. {
  850. $this->update = (boolean) $update;
  851. return $this;
  852. }
  853. /**
  854. * enables dev packages
  855. *
  856. * @param boolean $devMode
  857. * @return Installer
  858. */
  859. public function setDevMode($devMode = true)
  860. {
  861. $this->devMode = (boolean) $devMode;
  862. return $this;
  863. }
  864. /**
  865. * set whether to run scripts or not
  866. *
  867. * @param boolean $runScripts
  868. * @return Installer
  869. */
  870. public function setRunScripts($runScripts = true)
  871. {
  872. $this->runScripts = (boolean) $runScripts;
  873. return $this;
  874. }
  875. /**
  876. * set the config instance
  877. *
  878. * @param Config $config
  879. * @return Installer
  880. */
  881. public function setConfig(Config $config)
  882. {
  883. $this->config = $config;
  884. return $this;
  885. }
  886. /**
  887. * run in verbose mode
  888. *
  889. * @param boolean $verbose
  890. * @return Installer
  891. */
  892. public function setVerbose($verbose = true)
  893. {
  894. $this->verbose = (boolean) $verbose;
  895. return $this;
  896. }
  897. /**
  898. * restrict the update operation to a few packages, all other packages
  899. * that are already installed will be kept at their current version
  900. *
  901. * @param array $packages
  902. * @return Installer
  903. */
  904. public function setUpdateWhitelist(array $packages)
  905. {
  906. $this->updateWhitelist = array_flip(array_map('strtolower', $packages));
  907. return $this;
  908. }
  909. /**
  910. * Should dependencies of whitelisted packages be updated recursively?
  911. *
  912. * @param boolean $updateDependencies
  913. * @return Installer
  914. */
  915. public function setWhitelistDependencies($updateDependencies = true)
  916. {
  917. $this->whitelistDependencies = (boolean) $updateDependencies;
  918. return $this;
  919. }
  920. /**
  921. * Disables plugins.
  922. *
  923. * Call this if you want to ensure that third-party code never gets
  924. * executed. The default is to automatically install, and execute
  925. * custom third-party installers.
  926. *
  927. * @return Installer
  928. */
  929. public function disablePlugins()
  930. {
  931. $this->installationManager->disablePlugins();
  932. return $this;
  933. }
  934. }