InstallationManager.php 3.8 KB

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