Installer.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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\Pool;
  16. use Composer\DependencyResolver\Request;
  17. use Composer\DependencyResolver\Solver;
  18. use Composer\DependencyResolver\SolverProblemsException;
  19. use Composer\Downloader\DownloadManager;
  20. use Composer\Installer\InstallationManager;
  21. use Composer\IO\IOInterface;
  22. use Composer\Package\AliasPackage;
  23. use Composer\Package\Link;
  24. use Composer\Package\LinkConstraint\VersionConstraint;
  25. use Composer\Package\Locker;
  26. use Composer\Package\PackageInterface;
  27. use Composer\Repository\CompositeRepository;
  28. use Composer\Repository\PlatformRepository;
  29. use Composer\Repository\RepositoryInterface;
  30. use Composer\Repository\RepositoryManager;
  31. use Composer\Script\EventDispatcher;
  32. use Composer\Script\ScriptEvents;
  33. /**
  34. * @author Jordi Boggiano <j.boggiano@seld.be>
  35. * @author Beau Simensen <beau@dflydev.com>
  36. * @author Konstantin Kudryashov <ever.zet@gmail.com>
  37. */
  38. class Installer
  39. {
  40. /**
  41. * @var IOInterface
  42. */
  43. protected $io;
  44. /**
  45. * @var PackageInterface
  46. */
  47. protected $package;
  48. /**
  49. * @var DownloadManager
  50. */
  51. protected $downloadManager;
  52. /**
  53. * @var RepositoryManager
  54. */
  55. protected $repositoryManager;
  56. /**
  57. * @var Locker
  58. */
  59. protected $locker;
  60. /**
  61. * @var InstallationManager
  62. */
  63. protected $installationManager;
  64. /**
  65. * @var EventDispatcher
  66. */
  67. protected $eventDispatcher;
  68. protected $preferSource = false;
  69. protected $dryRun = false;
  70. protected $verbose = false;
  71. protected $installRecommends = true;
  72. protected $installSuggests = false;
  73. protected $update = false;
  74. /**
  75. * @var RepositoryInterface
  76. */
  77. protected $additionalInstalledRepository;
  78. /**
  79. * Constructor
  80. *
  81. * @param IOInterface $io
  82. * @param PackageInterface $package
  83. * @param DownloadManager $downloadManager
  84. * @param RepositoryManager $repositoryManager
  85. * @param Locker $locker
  86. * @param InstallationManager $installationManager
  87. * @param EventDispatcher $eventDispatcher
  88. */
  89. public function __construct(IOInterface $io, PackageInterface $package, DownloadManager $downloadManager, RepositoryManager $repositoryManager, Locker $locker, InstallationManager $installationManager, EventDispatcher $eventDispatcher)
  90. {
  91. $this->io = $io;
  92. $this->package = $package;
  93. $this->downloadManager = $downloadManager;
  94. $this->repositoryManager = $repositoryManager;
  95. $this->locker = $locker;
  96. $this->installationManager = $installationManager;
  97. $this->eventDispatcher = $eventDispatcher;
  98. }
  99. /**
  100. * Run installation (or update)
  101. */
  102. public function run()
  103. {
  104. if ($this->dryRun) {
  105. $this->verbose = true;
  106. }
  107. if ($this->preferSource) {
  108. $this->downloadManager->setPreferSource(true);
  109. }
  110. // create local repo, this contains all packages that are installed in the local project
  111. $localRepo = $this->repositoryManager->getLocalRepository();
  112. // create installed repo, this contains all local packages + platform packages (php & extensions)
  113. $installedRepo = new CompositeRepository(array($localRepo, new PlatformRepository()));
  114. if ($this->additionalInstalledRepository) {
  115. $installedRepo->addRepository($this->additionalInstalledRepository);
  116. }
  117. // prepare aliased packages
  118. if (!$this->update && $this->locker->isLocked()) {
  119. $aliases = $this->locker->getAliases();
  120. } else {
  121. $aliases = $this->package->getAliases();
  122. }
  123. foreach ($aliases as $alias) {
  124. foreach ($this->repositoryManager->findPackages($alias['package'], $alias['version']) as $package) {
  125. $package->getRepository()->addPackage(new AliasPackage($package, $alias['alias_normalized'], $alias['alias']));
  126. }
  127. foreach ($this->repositoryManager->getLocalRepository()->findPackages($alias['package'], $alias['version']) as $package) {
  128. $this->repositoryManager->getLocalRepository()->addPackage(new AliasPackage($package, $alias['alias_normalized'], $alias['alias']));
  129. $this->repositoryManager->getLocalRepository()->removePackage($package);
  130. }
  131. }
  132. // creating repository pool
  133. $pool = new Pool;
  134. $pool->addRepository($installedRepo);
  135. foreach ($this->repositoryManager->getRepositories() as $repository) {
  136. $pool->addRepository($repository);
  137. }
  138. // dispatch pre event
  139. if (!$this->dryRun) {
  140. $eventName = $this->update ? ScriptEvents::PRE_UPDATE_CMD : ScriptEvents::PRE_INSTALL_CMD;
  141. $this->eventDispatcher->dispatchCommandEvent($eventName);
  142. }
  143. // creating requirements request
  144. $installFromLock = false;
  145. $request = new Request($pool);
  146. if ($this->update) {
  147. $this->io->write('Updating dependencies');
  148. $request->updateAll();
  149. $links = $this->collectLinks();
  150. foreach ($links as $link) {
  151. $request->install($link->getTarget(), $link->getConstraint());
  152. }
  153. } elseif ($this->locker->isLocked()) {
  154. $installFromLock = true;
  155. $this->io->write('Installing from lock file');
  156. if (!$this->locker->isFresh()) {
  157. $this->io->write('<warning>Your lock file is out of sync with your composer.json, run "composer.phar update" to update dependencies</warning>');
  158. }
  159. foreach ($this->locker->getLockedPackages() as $package) {
  160. $version = $package->getVersion();
  161. foreach ($aliases as $alias) {
  162. if ($alias['package'] === $package->getName() && $alias['version'] === $package->getVersion()) {
  163. $version = $alias['alias'];
  164. break;
  165. }
  166. }
  167. $constraint = new VersionConstraint('=', $version);
  168. $request->install($package->getName(), $constraint);
  169. }
  170. } else {
  171. $this->io->write('Installing dependencies');
  172. $links = $this->collectLinks();
  173. foreach ($links as $link) {
  174. $request->install($link->getTarget(), $link->getConstraint());
  175. }
  176. }
  177. // prepare solver
  178. $policy = new DefaultPolicy();
  179. $solver = new Solver($policy, $pool, $installedRepo);
  180. // solve dependencies
  181. try {
  182. $operations = $solver->solve($request);
  183. } catch (SolverProblemsException $e) {
  184. $this->io->write('<error>Your requirements could not be solved to an installable set of packages.</error>');
  185. $this->io->write($e->getMessage());
  186. return false;
  187. }
  188. // force dev packages to be updated to latest reference on update
  189. if ($this->update) {
  190. foreach ($localRepo->getPackages() as $package) {
  191. if ($package instanceof AliasPackage) {
  192. $package = $package->getAliasOf();
  193. }
  194. // skip non-dev packages
  195. if (!$package->isDev()) {
  196. continue;
  197. }
  198. // skip packages that will be updated/uninstalled
  199. foreach ($operations as $operation) {
  200. if (('update' === $operation->getJobType() && $package === $operation->getInitialPackage())
  201. || ('uninstall' === $operation->getJobType() && $package === $operation->getPackage())
  202. ) {
  203. continue 2;
  204. }
  205. }
  206. // force update
  207. $newPackage = $this->repositoryManager->findPackage($package->getName(), $package->getVersion());
  208. if ($newPackage && $newPackage->getSourceReference() !== $package->getSourceReference()) {
  209. $operations[] = new UpdateOperation($package, $newPackage);
  210. }
  211. }
  212. }
  213. // anti-alias local repository to allow updates to work fine
  214. foreach ($this->repositoryManager->getLocalRepository()->getPackages() as $package) {
  215. if ($package instanceof AliasPackage) {
  216. $this->repositoryManager->getLocalRepository()->addPackage(clone $package->getAliasOf());
  217. $this->repositoryManager->getLocalRepository()->removePackage($package);
  218. }
  219. }
  220. // execute operations
  221. if (!$operations) {
  222. $this->io->write('<info>Nothing to install/update</info>');
  223. }
  224. foreach ($operations as $operation) {
  225. if ($this->verbose) {
  226. $this->io->write((string) $operation);
  227. }
  228. if (!$this->dryRun) {
  229. $this->eventDispatcher->dispatchPackageEvent(constant('Composer\Script\ScriptEvents::PRE_PACKAGE_'.strtoupper($operation->getJobType())), $operation);
  230. // if installing from lock, restore dev packages' references to their locked state
  231. if ($installFromLock) {
  232. $package = null;
  233. if ('update' === $operation->getJobType()) {
  234. $package = $operation->getTargetPackage();
  235. } elseif ('install' === $operation->getJobType()) {
  236. $package = $operation->getPackage();
  237. }
  238. if ($package && $package->isDev()) {
  239. $lockData = $this->locker->getLockData();
  240. foreach ($lockData['packages'] as $lockedPackage) {
  241. if (!empty($lockedPackage['source-reference']) && strtolower($lockedPackage['package']) === $package->getName()) {
  242. $package->setSourceReference($lockedPackage['source-reference']);
  243. break;
  244. }
  245. }
  246. }
  247. }
  248. $this->installationManager->execute($operation);
  249. $this->eventDispatcher->dispatchPackageEvent(constant('Composer\Script\ScriptEvents::POST_PACKAGE_'.strtoupper($operation->getJobType())), $operation);
  250. $localRepo->write();
  251. }
  252. }
  253. if (!$this->dryRun) {
  254. if ($this->update || !$this->locker->isLocked()) {
  255. $this->locker->setLockData($localRepo->getPackages(), $aliases);
  256. $this->io->write('<info>Writing lock file</info>');
  257. }
  258. $localRepo->write();
  259. $this->io->write('<info>Generating autoload files</info>');
  260. $generator = new AutoloadGenerator;
  261. $generator->dump($localRepo, $this->package, $this->installationManager, $this->installationManager->getVendorPath().'/.composer');
  262. // dispatch post event
  263. $eventName = $this->update ? ScriptEvents::POST_UPDATE_CMD : ScriptEvents::POST_INSTALL_CMD;
  264. $this->eventDispatcher->dispatchCommandEvent($eventName);
  265. }
  266. return true;
  267. }
  268. private function collectLinks()
  269. {
  270. $links = $this->package->getRequires();
  271. if ($this->installRecommends) {
  272. $links = array_merge($links, $this->package->getRecommends());
  273. }
  274. if ($this->installSuggests) {
  275. $links = array_merge($links, $this->package->getSuggests());
  276. }
  277. return $links;
  278. }
  279. /**
  280. * Create Installer
  281. *
  282. * @param IOInterface $io
  283. * @param Composer $composer
  284. * @param EventDispatcher $eventDispatcher
  285. * @return Installer
  286. */
  287. static public function create(IOInterface $io, Composer $composer, EventDispatcher $eventDispatcher = null)
  288. {
  289. $eventDispatcher = $eventDispatcher ?: new EventDispatcher($composer, $io);
  290. return new static(
  291. $io,
  292. $composer->getPackage(),
  293. $composer->getDownloadManager(),
  294. $composer->getRepositoryManager(),
  295. $composer->getLocker(),
  296. $composer->getInstallationManager(),
  297. $eventDispatcher
  298. );
  299. }
  300. public function setAdditionalInstalledRepository(RepositoryInterface $additionalInstalledRepository)
  301. {
  302. $this->additionalInstalledRepository = $additionalInstalledRepository;
  303. return $this;
  304. }
  305. /**
  306. * wether to run in drymode or not
  307. *
  308. * @param boolean $dryRun
  309. * @return Installer
  310. */
  311. public function setDryRun($dryRun=true)
  312. {
  313. $this->dryRun = (boolean) $dryRun;
  314. return $this;
  315. }
  316. /**
  317. * install recommend packages
  318. *
  319. * @param boolean $noInstallRecommends
  320. * @return Installer
  321. */
  322. public function setInstallRecommends($installRecommends=true)
  323. {
  324. $this->installRecommends = (boolean) $installRecommends;
  325. return $this;
  326. }
  327. /**
  328. * also install suggested packages
  329. *
  330. * @param boolean $installSuggests
  331. * @return Installer
  332. */
  333. public function setInstallSuggests($installSuggests=true)
  334. {
  335. $this->installSuggests = (boolean) $installSuggests;
  336. return $this;
  337. }
  338. /**
  339. * prefer source installation
  340. *
  341. * @param boolean $preferSource
  342. * @return Installer
  343. */
  344. public function setPreferSource($preferSource=true)
  345. {
  346. $this->preferSource = (boolean) $preferSource;
  347. return $this;
  348. }
  349. /**
  350. * update packages
  351. *
  352. * @param boolean $update
  353. * @return Installer
  354. */
  355. public function setUpdate($update=true)
  356. {
  357. $this->update = (boolean) $update;
  358. return $this;
  359. }
  360. /**
  361. * run in verbose mode
  362. *
  363. * @param boolean $verbose
  364. * @return Installer
  365. */
  366. public function setVerbose($verbose=true)
  367. {
  368. $this->verbose = (boolean) $verbose;
  369. return $this;
  370. }
  371. }