InstallationManager.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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\Repository\RepositoryInterface;
  15. use Composer\Repository\InstalledRepositoryInterface;
  16. use Composer\DependencyResolver\Operation\OperationInterface;
  17. use Composer\DependencyResolver\Operation\InstallOperation;
  18. use Composer\DependencyResolver\Operation\UpdateOperation;
  19. use Composer\DependencyResolver\Operation\UninstallOperation;
  20. use Composer\DependencyResolver\Operation\MarkAliasInstalledOperation;
  21. use Composer\DependencyResolver\Operation\MarkAliasUninstalledOperation;
  22. use Composer\Util\StreamContextFactory;
  23. /**
  24. * Package operation manager.
  25. *
  26. * @author Konstantin Kudryashov <ever.zet@gmail.com>
  27. * @author Jordi Boggiano <j.boggiano@seld.be>
  28. * @author Nils Adermann <naderman@naderman.de>
  29. */
  30. class InstallationManager
  31. {
  32. private $installers = array();
  33. private $cache = array();
  34. private $notifiablePackages = array();
  35. public function reset()
  36. {
  37. $this->notifiablePackages = array();
  38. }
  39. /**
  40. * Adds installer
  41. *
  42. * @param InstallerInterface $installer installer instance
  43. */
  44. public function addInstaller(InstallerInterface $installer)
  45. {
  46. array_unshift($this->installers, $installer);
  47. $this->cache = array();
  48. }
  49. /**
  50. * Removes installer
  51. *
  52. * @param InstallerInterface $installer installer instance
  53. */
  54. public function removeInstaller(InstallerInterface $installer)
  55. {
  56. if (false !== ($key = array_search($installer, $this->installers, true))) {
  57. array_splice($this->installers, $key, 1);
  58. $this->cache = array();
  59. }
  60. }
  61. /**
  62. * Disables plugins.
  63. *
  64. * We prevent any plugins from being instantiated by simply
  65. * deactivating the installer for them. This ensure that no third-party
  66. * code is ever executed.
  67. */
  68. public function disablePlugins()
  69. {
  70. foreach ($this->installers as $i => $installer) {
  71. if (!$installer instanceof PluginInstaller) {
  72. continue;
  73. }
  74. unset($this->installers[$i]);
  75. }
  76. }
  77. /**
  78. * Returns installer for a specific package type.
  79. *
  80. * @param string $type package type
  81. *
  82. * @return InstallerInterface
  83. *
  84. * @throws \InvalidArgumentException if installer for provided type is not registered
  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. * Executes solver operation.
  116. *
  117. * @param RepositoryInterface $repo repository in which to check
  118. * @param OperationInterface $operation operation instance
  119. */
  120. public function execute(RepositoryInterface $repo, OperationInterface $operation)
  121. {
  122. $method = $operation->getJobType();
  123. $this->$method($repo, $operation);
  124. }
  125. /**
  126. * Executes install operation.
  127. *
  128. * @param RepositoryInterface $repo repository in which to check
  129. * @param InstallOperation $operation operation instance
  130. */
  131. public function install(RepositoryInterface $repo, InstallOperation $operation)
  132. {
  133. $package = $operation->getPackage();
  134. $installer = $this->getInstaller($package->getType());
  135. $installer->install($repo, $package);
  136. $this->markForNotification($package);
  137. }
  138. /**
  139. * Executes update operation.
  140. *
  141. * @param RepositoryInterface $repo repository in which to check
  142. * @param UpdateOperation $operation operation instance
  143. */
  144. public function update(RepositoryInterface $repo, UpdateOperation $operation)
  145. {
  146. $initial = $operation->getInitialPackage();
  147. $target = $operation->getTargetPackage();
  148. $initialType = $initial->getType();
  149. $targetType = $target->getType();
  150. if ($initialType === $targetType) {
  151. $installer = $this->getInstaller($initialType);
  152. $installer->update($repo, $initial, $target);
  153. $this->markForNotification($target);
  154. } else {
  155. $this->getInstaller($initialType)->uninstall($repo, $initial);
  156. $this->getInstaller($targetType)->install($repo, $target);
  157. }
  158. }
  159. /**
  160. * Uninstalls package.
  161. *
  162. * @param RepositoryInterface $repo repository in which to check
  163. * @param UninstallOperation $operation operation instance
  164. */
  165. public function uninstall(RepositoryInterface $repo, UninstallOperation $operation)
  166. {
  167. $package = $operation->getPackage();
  168. $installer = $this->getInstaller($package->getType());
  169. $installer->uninstall($repo, $package);
  170. }
  171. /**
  172. * Executes markAliasInstalled operation.
  173. *
  174. * @param RepositoryInterface $repo repository in which to check
  175. * @param MarkAliasInstalledOperation $operation operation instance
  176. */
  177. public function markAliasInstalled(RepositoryInterface $repo, MarkAliasInstalledOperation $operation)
  178. {
  179. $package = $operation->getPackage();
  180. if (!$repo->hasPackage($package)) {
  181. $repo->addPackage(clone $package);
  182. }
  183. }
  184. /**
  185. * Executes markAlias operation.
  186. *
  187. * @param RepositoryInterface $repo repository in which to check
  188. * @param MarkAliasUninstalledOperation $operation operation instance
  189. */
  190. public function markAliasUninstalled(RepositoryInterface $repo, MarkAliasUninstalledOperation $operation)
  191. {
  192. $package = $operation->getPackage();
  193. $repo->removePackage($package);
  194. }
  195. /**
  196. * Returns the installation path of a package
  197. *
  198. * @param PackageInterface $package
  199. * @return string path
  200. */
  201. public function getInstallPath(PackageInterface $package)
  202. {
  203. $installer = $this->getInstaller($package->getType());
  204. return $installer->getInstallPath($package);
  205. }
  206. public function notifyInstalls()
  207. {
  208. foreach ($this->notifiablePackages as $repoUrl => $packages) {
  209. // non-batch API, deprecated
  210. if (strpos($repoUrl, '%package%')) {
  211. foreach ($packages as $package) {
  212. $url = str_replace('%package%', $package->getPrettyName(), $repoUrl);
  213. $params = array(
  214. 'version' => $package->getPrettyVersion(),
  215. 'version_normalized' => $package->getVersion(),
  216. );
  217. $opts = array('http' =>
  218. array(
  219. 'method' => 'POST',
  220. 'header' => array('Content-type: application/x-www-form-urlencoded'),
  221. 'content' => http_build_query($params, '', '&'),
  222. 'timeout' => 3,
  223. )
  224. );
  225. $context = StreamContextFactory::getContext($url, $opts);
  226. @file_get_contents($url, false, $context);
  227. }
  228. continue;
  229. }
  230. $postData = array('downloads' => array());
  231. foreach ($packages as $package) {
  232. $postData['downloads'][] = array(
  233. 'name' => $package->getPrettyName(),
  234. 'version' => $package->getVersion(),
  235. );
  236. }
  237. $opts = array('http' =>
  238. array(
  239. 'method' => 'POST',
  240. 'header' => array('Content-Type: application/json'),
  241. 'content' => json_encode($postData),
  242. 'timeout' => 6,
  243. )
  244. );
  245. $context = StreamContextFactory::getContext($repoUrl, $opts);
  246. @file_get_contents($repoUrl, false, $context);
  247. }
  248. $this->reset();
  249. }
  250. private function markForNotification(PackageInterface $package)
  251. {
  252. if ($package->getNotificationUrl()) {
  253. $this->notifiablePackages[$package->getNotificationUrl()][$package->getName()] = $package;
  254. }
  255. }
  256. }