InstallationManager.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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\NotifiableRepositoryInterface;
  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\Filesystem;
  24. /**
  25. * Package operation manager.
  26. *
  27. * @author Konstantin Kudryashov <ever.zet@gmail.com>
  28. * @author Jordi Boggiano <j.boggiano@seld.be>
  29. */
  30. class InstallationManager
  31. {
  32. private $installers = array();
  33. private $cache = array();
  34. private $vendorPath;
  35. /**
  36. * Creates an instance of InstallationManager
  37. *
  38. * @param string $vendorDir Relative path to the vendor directory
  39. * @throws \InvalidArgumentException
  40. */
  41. public function __construct($vendorDir = 'vendor')
  42. {
  43. $fs = new Filesystem();
  44. if ($fs->isAbsolutePath($vendorDir)) {
  45. $basePath = getcwd();
  46. $relativePath = $fs->findShortestPath($basePath.'/file', $vendorDir);
  47. if ($fs->isAbsolutePath($relativePath)) {
  48. throw new \InvalidArgumentException("Vendor dir ($vendorDir) must be accessible from the directory ($basePath).");
  49. }
  50. $this->vendorPath = $relativePath;
  51. } else {
  52. $this->vendorPath = rtrim($vendorDir, '/');
  53. }
  54. }
  55. /**
  56. * Adds installer
  57. *
  58. * @param InstallerInterface $installer installer instance
  59. */
  60. public function addInstaller(InstallerInterface $installer)
  61. {
  62. array_unshift($this->installers, $installer);
  63. $this->cache = array();
  64. }
  65. /**
  66. * Returns installer for a specific package type.
  67. *
  68. * @param string $type package type
  69. *
  70. * @return InstallerInterface
  71. *
  72. * @throws InvalidArgumentException if installer for provided type is not registered
  73. */
  74. public function getInstaller($type)
  75. {
  76. $type = strtolower($type);
  77. if (isset($this->cache[$type])) {
  78. return $this->cache[$type];
  79. }
  80. foreach ($this->installers as $installer) {
  81. if ($installer->supports($type)) {
  82. return $this->cache[$type] = $installer;
  83. }
  84. }
  85. throw new \InvalidArgumentException('Unknown installer type: '.$type);
  86. }
  87. /**
  88. * Checks whether provided package is installed in one of the registered installers.
  89. *
  90. * @param InstalledRepositoryInterface $repo repository in which to check
  91. * @param PackageInterface $package package instance
  92. *
  93. * @return Boolean
  94. */
  95. public function isPackageInstalled(InstalledRepositoryInterface $repo, PackageInterface $package)
  96. {
  97. if ($package instanceof AliasPackage) {
  98. return $repo->hasPackage($package) && $this->isPackageInstalled($repo, $package->getAliasOf());
  99. }
  100. return $this->getInstaller($package->getType())->isInstalled($repo, $package);
  101. }
  102. /**
  103. * Executes solver operation.
  104. *
  105. * @param RepositoryInterface $repo repository in which to check
  106. * @param OperationInterface $operation operation instance
  107. */
  108. public function execute(RepositoryInterface $repo, OperationInterface $operation)
  109. {
  110. $method = $operation->getJobType();
  111. $this->$method($repo, $operation);
  112. }
  113. /**
  114. * Executes install operation.
  115. *
  116. * @param RepositoryInterface $repo repository in which to check
  117. * @param InstallOperation $operation operation instance
  118. */
  119. public function install(RepositoryInterface $repo, InstallOperation $operation)
  120. {
  121. $package = $operation->getPackage();
  122. $installer = $this->getInstaller($package->getType());
  123. $installer->install($repo, $package);
  124. $this->notifyInstall($package);
  125. }
  126. /**
  127. * Executes update operation.
  128. *
  129. * @param RepositoryInterface $repo repository in which to check
  130. * @param InstallOperation $operation operation instance
  131. */
  132. public function update(RepositoryInterface $repo, UpdateOperation $operation)
  133. {
  134. $initial = $operation->getInitialPackage();
  135. $target = $operation->getTargetPackage();
  136. $initialType = $initial->getType();
  137. $targetType = $target->getType();
  138. if ($initialType === $targetType) {
  139. $installer = $this->getInstaller($initialType);
  140. $installer->update($repo, $initial, $target);
  141. $this->notifyInstall($target);
  142. } else {
  143. $this->getInstaller($initialType)->uninstall($repo, $initial);
  144. $this->getInstaller($targetType)->install($repo, $target);
  145. }
  146. }
  147. /**
  148. * Uninstalls package.
  149. *
  150. * @param RepositoryInterface $repo repository in which to check
  151. * @param UninstallOperation $operation operation instance
  152. */
  153. public function uninstall(RepositoryInterface $repo, UninstallOperation $operation)
  154. {
  155. $package = $operation->getPackage();
  156. $installer = $this->getInstaller($package->getType());
  157. $installer->uninstall($repo, $package);
  158. }
  159. /**
  160. * Executes markAliasInstalled operation.
  161. *
  162. * @param RepositoryInterface $repo repository in which to check
  163. * @param MarkAliasInstalledOperation $operation operation instance
  164. */
  165. public function markAliasInstalled(RepositoryInterface $repo, MarkAliasInstalledOperation $operation)
  166. {
  167. $package = $operation->getPackage();
  168. if (!$repo->hasPackage($package)) {
  169. $repo->addPackage(clone $package);
  170. }
  171. }
  172. /**
  173. * Executes markAlias operation.
  174. *
  175. * @param RepositoryInterface $repo repository in which to check
  176. * @param MarkAliasUninstalledOperation $operation operation instance
  177. */
  178. public function markAliasUninstalled(RepositoryInterface $repo, MarkAliasUninstalledOperation $operation)
  179. {
  180. $package = $operation->getPackage();
  181. $repo->removePackage($package);
  182. }
  183. /**
  184. * Returns the installation path of a package
  185. *
  186. * @param PackageInterface $package
  187. * @return string path
  188. */
  189. public function getInstallPath(PackageInterface $package)
  190. {
  191. $installer = $this->getInstaller($package->getType());
  192. return $installer->getInstallPath($package);
  193. }
  194. /**
  195. * Returns the vendor path
  196. *
  197. * @param boolean $absolute Whether or not to return an absolute path
  198. * @return string path
  199. */
  200. public function getVendorPath($absolute = false)
  201. {
  202. if (!$absolute) {
  203. return $this->vendorPath;
  204. }
  205. return getcwd().DIRECTORY_SEPARATOR.$this->vendorPath;
  206. }
  207. private function notifyInstall(PackageInterface $package)
  208. {
  209. if ($package->getRepository() instanceof NotifiableRepositoryInterface) {
  210. $package->getRepository()->notifyInstall($package);
  211. }
  212. }
  213. }