LibraryInstaller.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. * {@inheritDoc}
  54. */
  55. public function isInstalled(PackageInterface $package)
  56. {
  57. return $this->repository->hasPackage($package);
  58. }
  59. /**
  60. * {@inheritDoc}
  61. */
  62. public function install(PackageInterface $package)
  63. {
  64. $downloadPath = $this->getInstallPath($package);
  65. $this->downloadManager->download($package, $downloadPath);
  66. $this->repository->addPackage(clone $package);
  67. }
  68. /**
  69. * {@inheritDoc}
  70. */
  71. public function update(PackageInterface $initial, PackageInterface $target)
  72. {
  73. if (!$this->repository->hasPackage($initial)) {
  74. throw new \InvalidArgumentException('Package is not installed: '.$initial);
  75. }
  76. $downloadPath = $this->getInstallPath($initial);
  77. $this->downloadManager->update($initial, $target, $downloadPath);
  78. $this->repository->removePackage($initial);
  79. $this->repository->addPackage($target);
  80. }
  81. /**
  82. * {@inheritDoc}
  83. */
  84. public function uninstall(PackageInterface $package)
  85. {
  86. if (!$this->repository->hasPackage($package)) {
  87. throw new \InvalidArgumentException('Package is not installed: '.$package);
  88. }
  89. $downloadPath = $this->getInstallPath($package);
  90. $this->downloadManager->remove($package, $downloadPath);
  91. $this->repository->removePackage($package);
  92. }
  93. /**
  94. * {@inheritDoc}
  95. */
  96. public function getInstallPath(PackageInterface $package)
  97. {
  98. if (null === $package->getTargetDir()) {
  99. return ($this->directory ? $this->directory.'/' : '').$package->getName();
  100. }
  101. return ($this->directory ? $this->directory.'/' : '').$package->getTargetDir();
  102. }
  103. }