LibraryInstaller.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. protected $directory;
  25. protected $downloadManager;
  26. protected $repository;
  27. private $type;
  28. /**
  29. * Initializes library installer.
  30. *
  31. * @param string $dir relative path for packages home
  32. * @param DownloadManager $dm download manager
  33. * @param WritableRepositoryInterface $repository repository controller
  34. * @param string $type package type that this installer handles
  35. */
  36. public function __construct($directory, DownloadManager $dm, WritableRepositoryInterface $repository, $type = 'library')
  37. {
  38. $this->directory = $directory;
  39. $this->downloadManager = $dm;
  40. $this->type = $type;
  41. if (!is_dir($this->directory)) {
  42. if (file_exists($this->directory)) {
  43. throw new \UnexpectedValueException(
  44. $this->directory.' exists and is not a directory.'
  45. );
  46. }
  47. if (!mkdir($this->directory, 0777, true)) {
  48. throw new \UnexpectedValueException(
  49. $this->directory.' does not exist and could not be created.'
  50. );
  51. }
  52. }
  53. $this->repository = $repository;
  54. }
  55. /**
  56. * {@inheritDoc}
  57. */
  58. public function supports($packageType)
  59. {
  60. return $packageType === $this->type || null === $this->type;
  61. }
  62. /**
  63. * {@inheritDoc}
  64. */
  65. public function isInstalled(PackageInterface $package)
  66. {
  67. return $this->repository->hasPackage($package);
  68. }
  69. /**
  70. * {@inheritDoc}
  71. */
  72. public function install(PackageInterface $package)
  73. {
  74. $downloadPath = $this->getInstallPath($package);
  75. $this->downloadManager->download($package, $downloadPath);
  76. $this->repository->addPackage(clone $package);
  77. }
  78. /**
  79. * {@inheritDoc}
  80. */
  81. public function update(PackageInterface $initial, PackageInterface $target)
  82. {
  83. if (!$this->repository->hasPackage($initial)) {
  84. throw new \InvalidArgumentException('Package is not installed: '.$initial);
  85. }
  86. $downloadPath = $this->getInstallPath($initial);
  87. $this->downloadManager->update($initial, $target, $downloadPath);
  88. $this->repository->removePackage($initial);
  89. $this->repository->addPackage(clone $target);
  90. }
  91. /**
  92. * {@inheritDoc}
  93. */
  94. public function uninstall(PackageInterface $package)
  95. {
  96. if (!$this->repository->hasPackage($package)) {
  97. // TODO throw exception again here, when update is fixed and we don't have to remove+install (see #125)
  98. return;
  99. throw new \InvalidArgumentException('Package is not installed: '.$package);
  100. }
  101. $downloadPath = $this->getInstallPath($package);
  102. $this->downloadManager->remove($package, $downloadPath);
  103. $this->repository->removePackage($package);
  104. }
  105. /**
  106. * {@inheritDoc}
  107. */
  108. public function getInstallPath(PackageInterface $package)
  109. {
  110. $targetDir = $package->getTargetDir();
  111. return ($this->directory ? $this->directory.'/' : '') . $package->getName() . ($targetDir ? '/'.$targetDir : '');
  112. }
  113. }