InstallationManager.php 5.9 KB

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