VcsDownloader.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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\Downloader;
  12. use Composer\Package\PackageInterface;
  13. use Composer\Util\ProcessExecutor;
  14. use Composer\IO\IOInterface;
  15. use Composer\Util\Filesystem;
  16. /**
  17. * @author Jordi Boggiano <j.boggiano@seld.be>
  18. */
  19. abstract class VcsDownloader implements DownloaderInterface
  20. {
  21. protected $io;
  22. protected $process;
  23. protected $filesystem;
  24. public function __construct(IOInterface $io, ProcessExecutor $process = null, Filesystem $fs = null)
  25. {
  26. $this->io = $io;
  27. $this->process = $process ?: new ProcessExecutor;
  28. $this->filesystem = $fs ?: new Filesystem;
  29. }
  30. /**
  31. * {@inheritDoc}
  32. */
  33. public function getInstallationSource()
  34. {
  35. return 'source';
  36. }
  37. /**
  38. * {@inheritDoc}
  39. */
  40. public function download(PackageInterface $package, $path)
  41. {
  42. if (!$package->getSourceReference()) {
  43. throw new \InvalidArgumentException('Package '.$package->getPrettyName().' is missing reference information');
  44. }
  45. $this->io->write(" - Installing <info>" . $package->getName() . "</info> (<comment>" . $package->getPrettyVersion() . "</comment>)");
  46. $this->filesystem->removeDirectory($path);
  47. $this->doDownload($package, $path);
  48. $this->io->write('');
  49. }
  50. /**
  51. * {@inheritDoc}
  52. */
  53. public function update(PackageInterface $initial, PackageInterface $target, $path)
  54. {
  55. if (!$target->getSourceReference()) {
  56. throw new \InvalidArgumentException('Package '.$target->getPrettyName().' is missing reference information');
  57. }
  58. $this->io->write(" - Updating <info>" . $target->getName() . "</info> (<comment>" . $target->getPrettyVersion() . "</comment>)");
  59. $this->enforceCleanDirectory($path);
  60. $this->doUpdate($initial, $target, $path);
  61. $this->io->write('');
  62. }
  63. /**
  64. * {@inheritDoc}
  65. */
  66. public function remove(PackageInterface $package, $path)
  67. {
  68. $this->enforceCleanDirectory($path);
  69. $this->io->write(" - Removing <info>" . $package->getName() . "</info> (<comment>" . $package->getPrettyVersion() . "</comment>)");
  70. if (!$this->filesystem->removeDirectory($path)) {
  71. throw new \RuntimeException('Could not completely delete '.$path.', aborting.');
  72. }
  73. }
  74. /**
  75. * Downloads specific package into specific folder.
  76. *
  77. * @param PackageInterface $package package instance
  78. * @param string $path download path
  79. */
  80. abstract protected function doDownload(PackageInterface $package, $path);
  81. /**
  82. * Updates specific package in specific folder from initial to target version.
  83. *
  84. * @param PackageInterface $initial initial package
  85. * @param PackageInterface $target updated package
  86. * @param string $path download path
  87. */
  88. abstract protected function doUpdate(PackageInterface $initial, PackageInterface $target, $path);
  89. /**
  90. * Checks that no changes have been made to the local copy
  91. *
  92. * @throws \RuntimeException if the directory is not clean
  93. */
  94. abstract protected function enforceCleanDirectory($path);
  95. }