PathDownloader.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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\Dumper\ArrayDumper;
  13. use Composer\Package\PackageInterface;
  14. use Composer\Package\Version\VersionGuesser;
  15. use Composer\Package\Version\VersionParser;
  16. use Composer\Util\Platform;
  17. use Composer\Util\ProcessExecutor;
  18. use Symfony\Component\Filesystem\Exception\IOException;
  19. use Symfony\Component\Filesystem\Filesystem;
  20. /**
  21. * Download a package from a local path.
  22. *
  23. * @author Samuel Roze <samuel.roze@gmail.com>
  24. * @author Johann Reinke <johann.reinke@gmail.com>
  25. */
  26. class PathDownloader extends FileDownloader implements VcsCapableDownloaderInterface
  27. {
  28. const STRATEGY_SYMLINK = 10;
  29. const STRATEGY_MIRROR = 20;
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function download(PackageInterface $package, $path)
  34. {
  35. $url = $package->getDistUrl();
  36. $realUrl = realpath($url);
  37. if (false === $realUrl || !file_exists($realUrl) || !is_dir($realUrl)) {
  38. throw new \RuntimeException(sprintf(
  39. 'Source path "%s" is not found for package %s', $url, $package->getName()
  40. ));
  41. }
  42. if (strpos(realpath($path) . DIRECTORY_SEPARATOR, $realUrl . DIRECTORY_SEPARATOR) === 0) {
  43. throw new \RuntimeException(sprintf(
  44. 'Package %s cannot install to "%s" inside its source at "%s"',
  45. $package->getName(), realpath($path), $realUrl
  46. ));
  47. }
  48. // Get the transport options with default values
  49. $transportOptions = $package->getTransportOptions() + array('symlink' => null);
  50. // When symlink transport option is null, both symlink and mirror are allowed
  51. $currentStrategy = self::STRATEGY_SYMLINK;
  52. $allowedStrategies = array(self::STRATEGY_SYMLINK, self::STRATEGY_MIRROR);
  53. if (true === $transportOptions['symlink']) {
  54. $currentStrategy = self::STRATEGY_SYMLINK;
  55. $allowedStrategies = array(self::STRATEGY_SYMLINK);
  56. } elseif (false === $transportOptions['symlink']) {
  57. $currentStrategy = self::STRATEGY_MIRROR;
  58. $allowedStrategies = array(self::STRATEGY_MIRROR);
  59. }
  60. $fileSystem = new Filesystem();
  61. $this->filesystem->removeDirectory($path);
  62. $this->io->writeError(sprintf(
  63. ' - Installing <info>%s</info> (<comment>%s</comment>)',
  64. $package->getName(),
  65. $package->getFullPrettyVersion()
  66. ));
  67. if (self::STRATEGY_SYMLINK == $currentStrategy) {
  68. try {
  69. if (Platform::isWindows()) {
  70. // Implement symlinks as NTFS junctions on Windows
  71. $this->filesystem->junction($realUrl, $path);
  72. $this->io->writeError(sprintf(' Junctioned from %s', $url));
  73. } else {
  74. $absolutePath = $path;
  75. if (!$this->filesystem->isAbsolutePath($absolutePath)) {
  76. $absolutePath = getcwd() . DIRECTORY_SEPARATOR . $path;
  77. }
  78. $shortestPath = $this->filesystem->findShortestPath($absolutePath, $realUrl);
  79. $path = rtrim($path, "/");
  80. $fileSystem->symlink($shortestPath, $path);
  81. $this->io->writeError(sprintf(' Symlinked from %s', $url));
  82. }
  83. } catch (IOException $e) {
  84. if (in_array(self::STRATEGY_MIRROR, $allowedStrategies)) {
  85. $this->io->writeError(' <error>Symlink failed, fallback to use mirroring!</error>');
  86. $currentStrategy = self::STRATEGY_MIRROR;
  87. } else {
  88. throw new \RuntimeException(sprintf('Symlink from "%s" to "%s" failed!', $realUrl, $path));
  89. }
  90. }
  91. }
  92. // Fallback if symlink failed or if symlink is not allowed for the package
  93. if (self::STRATEGY_MIRROR == $currentStrategy) {
  94. $fileSystem->mirror($realUrl, $path);
  95. $this->io->writeError(sprintf(' Mirrored from %s', $url));
  96. }
  97. $this->io->writeError('');
  98. }
  99. /**
  100. * {@inheritDoc}
  101. */
  102. public function remove(PackageInterface $package, $path)
  103. {
  104. /**
  105. * For junctions don't blindly rely on Filesystem::removeDirectory as it may be overzealous. If a process
  106. * inadvertently locks the file the removal will fail, but it would fall back to recursive delete which
  107. * is disastrous within a junction. So in that case we have no other real choice but to fail hard.
  108. */
  109. if (Platform::isWindows() && $this->filesystem->isJunction($path)) {
  110. $this->io->writeError(" - Removing junction for <info>" . $package->getName() . "</info> (<comment>" . $package->getFullPrettyVersion() . "</comment>)");
  111. if (!$this->filesystem->removeJunction($path)) {
  112. $this->io->writeError("<warn>Could not remove junction at " . $path . " - is another process locking it?</warn>");
  113. throw new \RuntimeException('Could not reliably remove junction for package ' . $package->getName());
  114. }
  115. } else {
  116. parent::remove($package, $path);
  117. }
  118. }
  119. /**
  120. * {@inheritDoc}
  121. */
  122. public function getVcsReference(PackageInterface $package, $path)
  123. {
  124. $parser = new VersionParser;
  125. $guesser = new VersionGuesser($this->config, new ProcessExecutor($this->io), $parser);
  126. $dumper = new ArrayDumper;
  127. $packageConfig = $dumper->dump($package);
  128. if ($packageVersion = $guesser->guessVersion($packageConfig, $path)) {
  129. return $packageVersion['commit'];
  130. }
  131. }
  132. }