PathDownloader.php 5.2 KB

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