PathDownloader.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. $mirrorPathRepos = getenv('COMPOSER_MIRROR_PATH_REPOS');
  54. if ($mirrorPathRepos) {
  55. $currentStrategy = self::STRATEGY_MIRROR;
  56. }
  57. if (true === $transportOptions['symlink']) {
  58. $currentStrategy = self::STRATEGY_SYMLINK;
  59. $allowedStrategies = array(self::STRATEGY_SYMLINK);
  60. } elseif (false === $transportOptions['symlink']) {
  61. $currentStrategy = self::STRATEGY_MIRROR;
  62. $allowedStrategies = array(self::STRATEGY_MIRROR);
  63. }
  64. $fileSystem = new Filesystem();
  65. $this->filesystem->removeDirectory($path);
  66. $this->io->writeError(sprintf(
  67. ' - Installing <info>%s</info> (<comment>%s</comment>)',
  68. $package->getName(),
  69. $package->getFullPrettyVersion()
  70. ));
  71. if (self::STRATEGY_SYMLINK == $currentStrategy) {
  72. try {
  73. if (Platform::isWindows()) {
  74. // Implement symlinks as NTFS junctions on Windows
  75. $this->filesystem->junction($realUrl, $path);
  76. $this->io->writeError(sprintf(' Junctioned from %s', $url));
  77. } else {
  78. $absolutePath = $path;
  79. if (!$this->filesystem->isAbsolutePath($absolutePath)) {
  80. $absolutePath = getcwd() . DIRECTORY_SEPARATOR . $path;
  81. }
  82. $shortestPath = $this->filesystem->findShortestPath($absolutePath, $realUrl);
  83. $path = rtrim($path, "/");
  84. $fileSystem->symlink($shortestPath, $path);
  85. $this->io->writeError(sprintf(' Symlinked from %s', $url));
  86. }
  87. } catch (IOException $e) {
  88. if (in_array(self::STRATEGY_MIRROR, $allowedStrategies)) {
  89. $this->io->writeError(' <error>Symlink failed, fallback to use mirroring!</error>');
  90. $currentStrategy = self::STRATEGY_MIRROR;
  91. } else {
  92. throw new \RuntimeException(sprintf('Symlink from "%s" to "%s" failed!', $realUrl, $path));
  93. }
  94. }
  95. }
  96. // Fallback if symlink failed or if symlink is not allowed for the package
  97. if (self::STRATEGY_MIRROR == $currentStrategy) {
  98. $fileSystem->mirror($realUrl, $path);
  99. $this->io->writeError(sprintf(' Mirrored from %s', $url));
  100. }
  101. $this->io->writeError('');
  102. }
  103. /**
  104. * {@inheritDoc}
  105. */
  106. public function remove(PackageInterface $package, $path)
  107. {
  108. /**
  109. * For junctions don't blindly rely on Filesystem::removeDirectory as it may be overzealous. If a process
  110. * inadvertently locks the file the removal will fail, but it would fall back to recursive delete which
  111. * is disastrous within a junction. So in that case we have no other real choice but to fail hard.
  112. */
  113. if (Platform::isWindows() && $this->filesystem->isJunction($path)) {
  114. $this->io->writeError(" - Removing junction for <info>" . $package->getName() . "</info> (<comment>" . $package->getFullPrettyVersion() . "</comment>)");
  115. if (!$this->filesystem->removeJunction($path)) {
  116. $this->io->writeError("<warn>Could not remove junction at " . $path . " - is another process locking it?</warn>");
  117. throw new \RuntimeException('Could not reliably remove junction for package ' . $package->getName());
  118. }
  119. } else {
  120. parent::remove($package, $path);
  121. }
  122. }
  123. /**
  124. * {@inheritDoc}
  125. */
  126. public function getVcsReference(PackageInterface $package, $path)
  127. {
  128. $parser = new VersionParser;
  129. $guesser = new VersionGuesser($this->config, new ProcessExecutor($this->io), $parser);
  130. $dumper = new ArrayDumper;
  131. $packageConfig = $dumper->dump($package);
  132. if ($packageVersion = $guesser->guessVersion($packageConfig, $path)) {
  133. return $packageVersion['commit'];
  134. }
  135. }
  136. }