InstallationManager.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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\Installer;
  12. use Composer\IO\IOInterface;
  13. use Composer\Package\PackageInterface;
  14. use Composer\Package\AliasPackage;
  15. use Composer\Repository\RepositoryInterface;
  16. use Composer\Repository\InstalledRepositoryInterface;
  17. use Composer\DependencyResolver\Operation\OperationInterface;
  18. use Composer\DependencyResolver\Operation\InstallOperation;
  19. use Composer\DependencyResolver\Operation\UpdateOperation;
  20. use Composer\DependencyResolver\Operation\UninstallOperation;
  21. use Composer\DependencyResolver\Operation\MarkAliasInstalledOperation;
  22. use Composer\DependencyResolver\Operation\MarkAliasUninstalledOperation;
  23. use Composer\Util\StreamContextFactory;
  24. /**
  25. * Package operation manager.
  26. *
  27. * @author Konstantin Kudryashov <ever.zet@gmail.com>
  28. * @author Jordi Boggiano <j.boggiano@seld.be>
  29. * @author Nils Adermann <naderman@naderman.de>
  30. */
  31. class InstallationManager
  32. {
  33. private $installers = array();
  34. private $cache = array();
  35. private $notifiablePackages = array();
  36. public function reset()
  37. {
  38. $this->notifiablePackages = array();
  39. }
  40. /**
  41. * Adds installer
  42. *
  43. * @param InstallerInterface $installer installer instance
  44. */
  45. public function addInstaller(InstallerInterface $installer)
  46. {
  47. array_unshift($this->installers, $installer);
  48. $this->cache = array();
  49. }
  50. /**
  51. * Removes installer
  52. *
  53. * @param InstallerInterface $installer installer instance
  54. */
  55. public function removeInstaller(InstallerInterface $installer)
  56. {
  57. if (false !== ($key = array_search($installer, $this->installers, true))) {
  58. array_splice($this->installers, $key, 1);
  59. $this->cache = array();
  60. }
  61. }
  62. /**
  63. * Disables plugins.
  64. *
  65. * We prevent any plugins from being instantiated by simply
  66. * deactivating the installer for them. This ensure that no third-party
  67. * code is ever executed.
  68. */
  69. public function disablePlugins()
  70. {
  71. foreach ($this->installers as $i => $installer) {
  72. if (!$installer instanceof PluginInstaller) {
  73. continue;
  74. }
  75. unset($this->installers[$i]);
  76. }
  77. }
  78. /**
  79. * Returns installer for a specific package type.
  80. *
  81. * @param string $type package type
  82. *
  83. * @throws \InvalidArgumentException if installer for provided type is not registered
  84. * @return InstallerInterface
  85. */
  86. public function getInstaller($type)
  87. {
  88. $type = strtolower($type);
  89. if (isset($this->cache[$type])) {
  90. return $this->cache[$type];
  91. }
  92. foreach ($this->installers as $installer) {
  93. if ($installer->supports($type)) {
  94. return $this->cache[$type] = $installer;
  95. }
  96. }
  97. throw new \InvalidArgumentException('Unknown installer type: '.$type);
  98. }
  99. /**
  100. * Checks whether provided package is installed in one of the registered installers.
  101. *
  102. * @param InstalledRepositoryInterface $repo repository in which to check
  103. * @param PackageInterface $package package instance
  104. *
  105. * @return bool
  106. */
  107. public function isPackageInstalled(InstalledRepositoryInterface $repo, PackageInterface $package)
  108. {
  109. if ($package instanceof AliasPackage) {
  110. return $repo->hasPackage($package) && $this->isPackageInstalled($repo, $package->getAliasOf());
  111. }
  112. return $this->getInstaller($package->getType())->isInstalled($repo, $package);
  113. }
  114. /**
  115. * Install binary for the given package.
  116. * If the installer associated to this package doesn't handle that function, it'll do nothing.
  117. *
  118. * @param PackageInterface $package Package instance
  119. */
  120. public function ensureBinariesPresence(PackageInterface $package)
  121. {
  122. try {
  123. $installer = $this->getInstaller($package->getType());
  124. } catch (\InvalidArgumentException $e) {
  125. // no installer found for the current package type (@see `getInstaller()`)
  126. return;
  127. }
  128. // if the given installer support installing binaries
  129. if ($installer instanceof BinaryPresenceInterface) {
  130. $installer->ensureBinariesPresence($package);
  131. }
  132. }
  133. /**
  134. * Executes solver operation.
  135. *
  136. * @param RepositoryInterface $repo repository in which to check
  137. * @param OperationInterface $operation operation instance
  138. */
  139. public function execute(RepositoryInterface $repo, OperationInterface $operation)
  140. {
  141. $method = $operation->getJobType();
  142. $this->$method($repo, $operation);
  143. }
  144. /**
  145. * Executes install operation.
  146. *
  147. * @param RepositoryInterface $repo repository in which to check
  148. * @param InstallOperation $operation operation instance
  149. */
  150. public function install(RepositoryInterface $repo, InstallOperation $operation)
  151. {
  152. $package = $operation->getPackage();
  153. $installer = $this->getInstaller($package->getType());
  154. $installer->install($repo, $package);
  155. $this->markForNotification($package);
  156. }
  157. /**
  158. * Executes update operation.
  159. *
  160. * @param RepositoryInterface $repo repository in which to check
  161. * @param UpdateOperation $operation operation instance
  162. */
  163. public function update(RepositoryInterface $repo, UpdateOperation $operation)
  164. {
  165. $initial = $operation->getInitialPackage();
  166. $target = $operation->getTargetPackage();
  167. $initialType = $initial->getType();
  168. $targetType = $target->getType();
  169. if ($initialType === $targetType) {
  170. $installer = $this->getInstaller($initialType);
  171. $installer->update($repo, $initial, $target);
  172. $this->markForNotification($target);
  173. } else {
  174. $this->getInstaller($initialType)->uninstall($repo, $initial);
  175. $this->getInstaller($targetType)->install($repo, $target);
  176. }
  177. }
  178. /**
  179. * Uninstalls package.
  180. *
  181. * @param RepositoryInterface $repo repository in which to check
  182. * @param UninstallOperation $operation operation instance
  183. */
  184. public function uninstall(RepositoryInterface $repo, UninstallOperation $operation)
  185. {
  186. $package = $operation->getPackage();
  187. $installer = $this->getInstaller($package->getType());
  188. $installer->uninstall($repo, $package);
  189. }
  190. /**
  191. * Executes markAliasInstalled operation.
  192. *
  193. * @param RepositoryInterface $repo repository in which to check
  194. * @param MarkAliasInstalledOperation $operation operation instance
  195. */
  196. public function markAliasInstalled(RepositoryInterface $repo, MarkAliasInstalledOperation $operation)
  197. {
  198. $package = $operation->getPackage();
  199. if (!$repo->hasPackage($package)) {
  200. $repo->addPackage(clone $package);
  201. }
  202. }
  203. /**
  204. * Executes markAlias operation.
  205. *
  206. * @param RepositoryInterface $repo repository in which to check
  207. * @param MarkAliasUninstalledOperation $operation operation instance
  208. */
  209. public function markAliasUninstalled(RepositoryInterface $repo, MarkAliasUninstalledOperation $operation)
  210. {
  211. $package = $operation->getPackage();
  212. $repo->removePackage($package);
  213. }
  214. /**
  215. * Returns the installation path of a package
  216. *
  217. * @param PackageInterface $package
  218. * @return string path
  219. */
  220. public function getInstallPath(PackageInterface $package)
  221. {
  222. $installer = $this->getInstaller($package->getType());
  223. return $installer->getInstallPath($package);
  224. }
  225. public function notifyInstalls(IOInterface $io)
  226. {
  227. foreach ($this->notifiablePackages as $repoUrl => $packages) {
  228. $repositoryName = parse_url($repoUrl, PHP_URL_HOST);
  229. if ($io->hasAuthentication($repositoryName)) {
  230. $auth = $io->getAuthentication($repositoryName);
  231. $authStr = base64_encode($auth['username'] . ':' . $auth['password']);
  232. $authHeader = 'Authorization: Basic '.$authStr;
  233. }
  234. // non-batch API, deprecated
  235. if (strpos($repoUrl, '%package%')) {
  236. foreach ($packages as $package) {
  237. $url = str_replace('%package%', $package->getPrettyName(), $repoUrl);
  238. $params = array(
  239. 'version' => $package->getPrettyVersion(),
  240. 'version_normalized' => $package->getVersion(),
  241. );
  242. $opts = array('http' =>
  243. array(
  244. 'method' => 'POST',
  245. 'header' => array('Content-type: application/x-www-form-urlencoded'),
  246. 'content' => http_build_query($params, '', '&'),
  247. 'timeout' => 3,
  248. ),
  249. );
  250. if (isset($authHeader)) {
  251. $opts['http']['header'][] = $authHeader;
  252. }
  253. $context = StreamContextFactory::getContext($url, $opts);
  254. @file_get_contents($url, false, $context);
  255. }
  256. continue;
  257. }
  258. $postData = array('downloads' => array());
  259. foreach ($packages as $package) {
  260. $postData['downloads'][] = array(
  261. 'name' => $package->getPrettyName(),
  262. 'version' => $package->getVersion(),
  263. );
  264. }
  265. $opts = array('http' =>
  266. array(
  267. 'method' => 'POST',
  268. 'header' => array('Content-Type: application/json'),
  269. 'content' => json_encode($postData),
  270. 'timeout' => 6,
  271. ),
  272. );
  273. if (isset($authHeader)) {
  274. $opts['http']['header'][] = $authHeader;
  275. }
  276. $context = StreamContextFactory::getContext($repoUrl, $opts);
  277. @file_get_contents($repoUrl, false, $context);
  278. }
  279. $this->reset();
  280. }
  281. private function markForNotification(PackageInterface $package)
  282. {
  283. if ($package->getNotificationUrl()) {
  284. $this->notifiablePackages[$package->getNotificationUrl()][$package->getName()] = $package;
  285. }
  286. }
  287. }