InstallationManager.php 5.3 KB

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