GitArchiver.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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\Util\ProcessExecutor;
  13. /**
  14. * @author Till Klampaeckel <till@php.net>
  15. * @author Matthieu Moquet <matthieu@moquet.net>
  16. */
  17. class GitArchiver implements ArchiverInterface
  18. {
  19. protected $process;
  20. public function __construct($process = null)
  21. {
  22. $this->process = $process ?: new ProcessExecutor();
  23. }
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function archive($sources, $target, $format, $sourceRef = null)
  28. {
  29. if (null === $sourceRef) {
  30. $sourceRef = 'HEAD';
  31. }
  32. $command = sprintf(
  33. 'git archive --format %s --output %s %s',
  34. $format,
  35. escapeshellarg($target),
  36. escapeshellarg($sourceRef)
  37. );
  38. $exitCode = $this->process->execute($command, $output, $sources);
  39. if (0 !== $exitCode) {
  40. throw new \RuntimeException(
  41. sprintf('Impossible to build the archive: `%s` returned %s', $command, $exitCode)
  42. );
  43. }
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function supports($format, $sourceType)
  49. {
  50. return 'git' === $sourceType && in_array($format, array('zip', 'tar', 'tgz'));
  51. }
  52. }