BaseArchiver.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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\Package\Archiver;
  12. use Composer\Package\PackageInterface;
  13. use Composer\Util\ProcessExecutor;
  14. use Composer\Downloader\GitDownloader;
  15. use Composer\Downloader\HgDownloader;
  16. use Composer\Downloader\SvnDownloader;
  17. use Composer\IO\NullIO;
  18. use Composer\Factory;
  19. /**
  20. * @author Till Klampaeckel <till@php.net>
  21. */
  22. abstract class BaseArchiver implements ArchiverInterface
  23. {
  24. /**
  25. * Format: zip or tar.
  26. * @var string
  27. */
  28. protected $format = '';
  29. /**
  30. * Path to where to dump the export to.
  31. * @var mixed|null
  32. */
  33. protected $path;
  34. /**
  35. * @var ProcessExecutor
  36. */
  37. protected $process;
  38. /**
  39. * Working directory.
  40. * @var string
  41. */
  42. protected $temp;
  43. /**
  44. * @param mixed $path
  45. * @param ProcessExecutor|null $process
  46. *
  47. * @throws \InvalidArgumentException
  48. */
  49. public function __construct($path = null, ProcessExecutor $process = null)
  50. {
  51. if (!empty($path)) {
  52. if (!is_writable($path)) {
  53. throw new \InvalidArgumentException("Not authorized to write to '{$path}'");
  54. }
  55. $this->path = $path;
  56. }
  57. $this->process = $process ?: new ProcessExecutor();
  58. $this->temp = sys_get_temp_dir();
  59. }
  60. /**
  61. * @return \Composer\Downloader\DownloadManager
  62. */
  63. public function getDownloadManager()
  64. {
  65. $factory = new Factory;
  66. $dm = $factory->createDownloadManager(new NullIO());
  67. return $dm;
  68. }
  69. /**
  70. * @param PackageInterface $package
  71. * @param string $extension
  72. *
  73. * @return string
  74. * @throws \InvalidArgumentException When unknown 'format' is encountered.
  75. */
  76. public function getFilename(PackageInterface $package, $extension)
  77. {
  78. $name = $package->getPrettyVersion();
  79. $fileName = sprintf('%s.%s', $name, $extension);
  80. return $fileName;
  81. }
  82. /**
  83. * @param PackageInterface $package
  84. *
  85. * @return string
  86. * @throws \RuntimeException
  87. */
  88. protected function getAndEnsureWorkDirectory(PackageInterface $package)
  89. {
  90. $workDir = sprintf('%s/%s/%s', $this->temp, $this->format, $package->getName());
  91. if (!file_exists($workDir)) {
  92. mkdir($workDir, 0777, true);
  93. }
  94. if (!file_exists($workDir)) {
  95. throw new \RuntimeException("Could not find '{$workDir}' directory.");
  96. }
  97. return $workDir;
  98. }
  99. /**
  100. * Package the given directory into an archive.
  101. *
  102. * The format is most likely \Phar::TAR or \Phar::ZIP.
  103. *
  104. * @param string $filename
  105. * @param string $workDir
  106. * @param int $format
  107. *
  108. * @throws \RuntimeException
  109. */
  110. protected function package($filename, $workDir, $format)
  111. {
  112. try {
  113. $phar = new \PharData($filename, null, null, $format);
  114. $phar->buildFromDirectory($workDir);
  115. } catch (\UnexpectedValueException $e) {
  116. $message = "Original PHAR exception: " . (string) $e;
  117. $message .= PHP_EOL . PHP_EOL;
  118. $message .= sprintf("Could not create archive '%s' from '%s'.", $filename, $workDir);
  119. throw new \RuntimeException($message);
  120. }
  121. }
  122. /**
  123. * @param string $fileName
  124. * @param string $sourceRef
  125. * @param string $workDir
  126. */
  127. protected function packageGit($fileName, $sourceRef, $workDir)
  128. {
  129. $command = sprintf(
  130. 'git archive --format %s --output %s %s',
  131. $this->format,
  132. escapeshellarg(sprintf('%s/%s', $this->path, $fileName)),
  133. $sourceRef
  134. );
  135. $this->process->execute($command, $output, $workDir);
  136. }
  137. /**
  138. * @param string $fileName
  139. * @param string $sourceRef
  140. * @param string $workDir
  141. */
  142. protected function packageHg($fileName, $sourceRef, $workDir)
  143. {
  144. $format = ($this->format == 'tarball')?'tar':$this->format;
  145. $command = sprintf(
  146. 'hg archive --rev %s --type %s %s',
  147. $sourceRef,
  148. $format,
  149. escapeshellarg(sprintf('%s/%s', $this->path, $fileName))
  150. );
  151. $this->process->execute($command, $output, $workDir);
  152. }
  153. }