InstallationManager.php 6.8 KB

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