LibraryInstaller.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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\Downloader\DownloadManager;
  13. use Composer\Repository\WritableRepositoryInterface;
  14. use Composer\DependencyResolver\Operation\OperationInterface;
  15. use Composer\Package\PackageInterface;
  16. /**
  17. * Package installation manager.
  18. *
  19. * @author Jordi Boggiano <j.boggiano@seld.be>
  20. * @author Konstantin Kudryashov <ever.zet@gmail.com>
  21. */
  22. class LibraryInstaller implements InstallerInterface
  23. {
  24. private $directory;
  25. private $downloadManager;
  26. private $repository;
  27. /**
  28. * Initializes library installer.
  29. *
  30. * @param string $dir relative path for packages home
  31. * @param DownloadManager $dm download manager
  32. * @param WritableRepositoryInterface $repository repository controller
  33. */
  34. public function __construct($directory, DownloadManager $dm, WritableRepositoryInterface $repository)
  35. {
  36. $this->directory = $directory;
  37. $this->downloadManager = $dm;
  38. if (!is_dir($this->directory)) {
  39. if (file_exists($this->directory)) {
  40. throw new \UnexpectedValueException(
  41. $this->directory.' exists and is not a directory.'
  42. );
  43. }
  44. if (!mkdir($this->directory, 0777, true)) {
  45. throw new \UnexpectedValueException(
  46. $this->directory.' does not exist and could not be created.'
  47. );
  48. }
  49. }
  50. $this->repository = $repository;
  51. }
  52. /**
  53. * Checks that specific package is installed.
  54. *
  55. * @param PackageInterface $package package instance
  56. *
  57. * @return Boolean
  58. */
  59. public function isInstalled(PackageInterface $package)
  60. {
  61. return $this->repository->hasPackage($package);
  62. }
  63. /**
  64. * Installs specific package.
  65. *
  66. * @param PackageInterface $package package instance
  67. *
  68. * @throws InvalidArgumentException if provided package have no urls to download from
  69. */
  70. public function install(PackageInterface $package)
  71. {
  72. $downloadPath = $this->getInstallPath($package);
  73. $this->downloadManager->download($package, $downloadPath);
  74. $this->repository->addPackage(clone $package);
  75. }
  76. /**
  77. * Updates specific package.
  78. *
  79. * @param PackageInterface $initial already installed package version
  80. * @param PackageInterface $target updated version
  81. *
  82. * @throws InvalidArgumentException if $from package is not installed
  83. */
  84. public function update(PackageInterface $initial, PackageInterface $target)
  85. {
  86. if (!$this->repository->hasPackage($initial)) {
  87. throw new \InvalidArgumentException('Package is not installed: '.$initial);
  88. }
  89. $downloadPath = $this->getInstallPath($initial);
  90. $this->downloadManager->update($initial, $target, $downloadPath);
  91. $this->repository->removePackage($initial);
  92. $this->repository->addPackage($target);
  93. }
  94. /**
  95. * Uninstalls specific package.
  96. *
  97. * @param PackageInterface $package package instance
  98. *
  99. * @throws InvalidArgumentException if package is not installed
  100. */
  101. public function uninstall(PackageInterface $package)
  102. {
  103. if (!$this->repository->hasPackage($package)) {
  104. throw new \InvalidArgumentException('Package is not installed: '.$package);
  105. }
  106. $downloadPath = $this->getInstallPath($package);
  107. $this->downloadManager->remove($package, $downloadPath);
  108. $this->repository->removePackage($package);
  109. }
  110. public function getInstallPath(PackageInterface $package)
  111. {
  112. if (null === $package->getInstallAs()) {
  113. return ($this->directory ? $this->directory.'/' : '').$package->getName();
  114. } else {
  115. return ($this->directory ? $this->directory.'/' : '').$package->getInstallAs();
  116. }
  117. }
  118. }