InstallationManager.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. /**
  28. * Adds installer
  29. *
  30. * @param InstallerInterface $installer installer instance
  31. */
  32. public function addInstaller(InstallerInterface $installer)
  33. {
  34. array_unshift($this->installers, $installer);
  35. $this->cache = array();
  36. }
  37. /**
  38. * Returns installer for a specific package type.
  39. *
  40. * @param string $type package type
  41. *
  42. * @return InstallerInterface
  43. *
  44. * @throws InvalidArgumentException if installer for provided type is not registered
  45. */
  46. public function getInstaller($type)
  47. {
  48. $type = strtolower($type);
  49. if (isset($this->cache[$type])) {
  50. return $this->cache[$type];
  51. }
  52. foreach ($this->installers as $installer) {
  53. if ($installer->supports($type)) {
  54. return $this->cache[$type] = $installer;
  55. }
  56. }
  57. throw new \InvalidArgumentException('Unknown installer type: '.$type);
  58. }
  59. /**
  60. * Checks whether provided package is installed in one of the registered installers.
  61. *
  62. * @param PackageInterface $package package instance
  63. *
  64. * @return Boolean
  65. */
  66. public function isPackageInstalled(PackageInterface $package)
  67. {
  68. foreach ($this->installers as $installer) {
  69. if ($installer->isInstalled($package)) {
  70. return true;
  71. }
  72. }
  73. return false;
  74. }
  75. /**
  76. * Executes solver operation.
  77. *
  78. * @param OperationInterface $operation operation instance
  79. */
  80. public function execute(OperationInterface $operation)
  81. {
  82. $method = $operation->getJobType();
  83. $this->$method($operation);
  84. }
  85. /**
  86. * Executes install operation.
  87. *
  88. * @param InstallOperation $operation operation instance
  89. */
  90. public function install(InstallOperation $operation)
  91. {
  92. $installer = $this->getInstaller($operation->getPackage()->getType());
  93. $installer->install($operation->getPackage());
  94. }
  95. /**
  96. * Executes update operation.
  97. *
  98. * @param InstallOperation $operation operation instance
  99. */
  100. public function update(UpdateOperation $operation)
  101. {
  102. $initial = $operation->getInitialPackage();
  103. $target = $operation->getTargetPackage();
  104. $initialType = $initial->getType();
  105. $targetType = $target->getType();
  106. if ($initialType === $targetType) {
  107. $installer = $this->getInstaller($initialType);
  108. $installer->update($initial, $target);
  109. } else {
  110. $this->getInstaller($initialType)->uninstall($initial);
  111. $this->getInstaller($targetType)->install($target);
  112. }
  113. }
  114. /**
  115. * Uninstalls package.
  116. *
  117. * @param UninstallOperation $operation operation instance
  118. */
  119. public function uninstall(UninstallOperation $operation)
  120. {
  121. $installer = $this->getInstaller($operation->getPackage()->getType());
  122. $installer->uninstall($operation->getPackage());
  123. }
  124. /**
  125. * Returns the installation path of a package
  126. *
  127. * @param PackageInterface $package
  128. * @return string path
  129. */
  130. public function getInstallPath(PackageInterface $package)
  131. {
  132. $installer = $this->getInstaller($package->getType());
  133. return $installer->getInstallPath($package);
  134. }
  135. }