InstallationManager.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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\Package\PackageInterface;
  13. use Composer\Package\AliasPackage;
  14. use Composer\Plugin\PluginInstaller;
  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. * @return InstallerInterface
  84. *
  85. * @throws \InvalidArgumentException if installer for provided type is not registered
  86. */
  87. public function getInstaller($type)
  88. {
  89. $type = strtolower($type);
  90. if (isset($this->cache[$type])) {
  91. return $this->cache[$type];
  92. }
  93. foreach ($this->installers as $installer) {
  94. if ($installer->supports($type)) {
  95. return $this->cache[$type] = $installer;
  96. }
  97. }
  98. throw new \InvalidArgumentException('Unknown installer type: '.$type);
  99. }
  100. /**
  101. * Checks whether provided package is installed in one of the registered installers.
  102. *
  103. * @param InstalledRepositoryInterface $repo repository in which to check
  104. * @param PackageInterface $package package instance
  105. *
  106. * @return bool
  107. */
  108. public function isPackageInstalled(InstalledRepositoryInterface $repo, PackageInterface $package)
  109. {
  110. if ($package instanceof AliasPackage) {
  111. return $repo->hasPackage($package) && $this->isPackageInstalled($repo, $package->getAliasOf());
  112. }
  113. return $this->getInstaller($package->getType())->isInstalled($repo, $package);
  114. }
  115. /**
  116. * Executes solver operation.
  117. *
  118. * @param RepositoryInterface $repo repository in which to check
  119. * @param OperationInterface $operation operation instance
  120. */
  121. public function execute(RepositoryInterface $repo, OperationInterface $operation)
  122. {
  123. $method = $operation->getJobType();
  124. $this->$method($repo, $operation);
  125. }
  126. /**
  127. * Executes install operation.
  128. *
  129. * @param RepositoryInterface $repo repository in which to check
  130. * @param InstallOperation $operation operation instance
  131. */
  132. public function install(RepositoryInterface $repo, InstallOperation $operation)
  133. {
  134. $package = $operation->getPackage();
  135. $installer = $this->getInstaller($package->getType());
  136. $installer->install($repo, $package);
  137. $this->markForNotification($package);
  138. }
  139. /**
  140. * Executes update operation.
  141. *
  142. * @param RepositoryInterface $repo repository in which to check
  143. * @param UpdateOperation $operation operation instance
  144. */
  145. public function update(RepositoryInterface $repo, UpdateOperation $operation)
  146. {
  147. $initial = $operation->getInitialPackage();
  148. $target = $operation->getTargetPackage();
  149. $initialType = $initial->getType();
  150. $targetType = $target->getType();
  151. if ($initialType === $targetType) {
  152. $installer = $this->getInstaller($initialType);
  153. $installer->update($repo, $initial, $target);
  154. $this->markForNotification($target);
  155. } else {
  156. $this->getInstaller($initialType)->uninstall($repo, $initial);
  157. $this->getInstaller($targetType)->install($repo, $target);
  158. }
  159. }
  160. /**
  161. * Uninstalls package.
  162. *
  163. * @param RepositoryInterface $repo repository in which to check
  164. * @param UninstallOperation $operation operation instance
  165. */
  166. public function uninstall(RepositoryInterface $repo, UninstallOperation $operation)
  167. {
  168. $package = $operation->getPackage();
  169. $installer = $this->getInstaller($package->getType());
  170. $installer->uninstall($repo, $package);
  171. }
  172. /**
  173. * Executes markAliasInstalled operation.
  174. *
  175. * @param RepositoryInterface $repo repository in which to check
  176. * @param MarkAliasInstalledOperation $operation operation instance
  177. */
  178. public function markAliasInstalled(RepositoryInterface $repo, MarkAliasInstalledOperation $operation)
  179. {
  180. $package = $operation->getPackage();
  181. if (!$repo->hasPackage($package)) {
  182. $repo->addPackage(clone $package);
  183. }
  184. }
  185. /**
  186. * Executes markAlias operation.
  187. *
  188. * @param RepositoryInterface $repo repository in which to check
  189. * @param MarkAliasUninstalledOperation $operation operation instance
  190. */
  191. public function markAliasUninstalled(RepositoryInterface $repo, MarkAliasUninstalledOperation $operation)
  192. {
  193. $package = $operation->getPackage();
  194. $repo->removePackage($package);
  195. }
  196. /**
  197. * Returns the installation path of a package
  198. *
  199. * @param PackageInterface $package
  200. * @return string path
  201. */
  202. public function getInstallPath(PackageInterface $package)
  203. {
  204. $installer = $this->getInstaller($package->getType());
  205. return $installer->getInstallPath($package);
  206. }
  207. public function notifyInstalls()
  208. {
  209. foreach ($this->notifiablePackages as $repoUrl => $packages) {
  210. // non-batch API, deprecated
  211. if (strpos($repoUrl, '%package%')) {
  212. foreach ($packages as $package) {
  213. $url = str_replace('%package%', $package->getPrettyName(), $repoUrl);
  214. $params = array(
  215. 'version' => $package->getPrettyVersion(),
  216. 'version_normalized' => $package->getVersion(),
  217. );
  218. $opts = array('http' =>
  219. array(
  220. 'method' => 'POST',
  221. 'header' => array('Content-type: application/x-www-form-urlencoded'),
  222. 'content' => http_build_query($params, '', '&'),
  223. 'timeout' => 3,
  224. )
  225. );
  226. $context = StreamContextFactory::getContext($url, $opts);
  227. @file_get_contents($url, false, $context);
  228. }
  229. continue;
  230. }
  231. $postData = array('downloads' => array());
  232. foreach ($packages as $package) {
  233. $postData['downloads'][] = array(
  234. 'name' => $package->getPrettyName(),
  235. 'version' => $package->getVersion(),
  236. );
  237. }
  238. $opts = array('http' =>
  239. array(
  240. 'method' => 'POST',
  241. 'header' => array('Content-Type: application/json'),
  242. 'content' => json_encode($postData),
  243. 'timeout' => 6,
  244. )
  245. );
  246. $context = StreamContextFactory::getContext($repoUrl, $opts);
  247. @file_get_contents($repoUrl, false, $context);
  248. }
  249. $this->reset();
  250. }
  251. private function markForNotification(PackageInterface $package)
  252. {
  253. if ($package->getNotificationUrl()) {
  254. $this->notifiablePackages[$package->getNotificationUrl()][$package->getName()] = $package;
  255. }
  256. }
  257. }