PathDownloader.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. $shortestPath = $this->filesystem->findShortestPath($path, $realUrl);
  71. $fileSystem->symlink($shortestPath, $path);
  72. $this->io->writeError(sprintf(' Symlinked from %s', $url));
  73. }
  74. } catch (IOException $e) {
  75. if (in_array(self::STRATEGY_MIRROR, $allowedStrategies)) {
  76. $this->io->writeError(' <error>Symlink failed, fallback to use mirroring!</error>');
  77. $currentStrategy = self::STRATEGY_MIRROR;
  78. } else {
  79. throw new \RuntimeException(sprintf('Symlink from "%s" to "%s" failed!', $realUrl, $path));
  80. }
  81. }
  82. }
  83. // Fallback if symlink failed or if symlink is not allowed for the package
  84. if (self::STRATEGY_MIRROR == $currentStrategy) {
  85. $fileSystem->mirror($realUrl, $path);
  86. $this->io->writeError(sprintf(' Mirrored from %s', $url));
  87. }
  88. $this->io->writeError('');
  89. }
  90. }