PharArchiver.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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\BasePackage;
  13. use Composer\Package\PackageInterface;
  14. /**
  15. * @author Till Klampaeckel <till@php.net>
  16. * @author Nils Adermann <naderman@naderman.de>
  17. * @author Matthieu Moquet <matthieu@moquet.net>
  18. */
  19. class PharArchiver implements ArchiverInterface
  20. {
  21. protected static $formats = array(
  22. 'zip' => \Phar::ZIP,
  23. 'tar' => \Phar::TAR,
  24. );
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public function archive($sources, $target, $format, array $excludes = array())
  29. {
  30. $sources = realpath($sources);
  31. try {
  32. $phar = new \PharData($target, null, null, static::$formats[$format]);
  33. $files = new ArchivableFilesFinder($sources, $excludes);
  34. $phar->buildFromIterator($files, $sources);
  35. return $target;
  36. } catch (\UnexpectedValueException $e) {
  37. $message = sprintf("Could not create archive '%s' from '%s': %s",
  38. $target,
  39. $sources,
  40. $e->getMessage()
  41. );
  42. throw new \RuntimeException($message, $e->getCode(), $e);
  43. }
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function supports($format, $sourceType)
  49. {
  50. return isset(static::$formats[$format]);
  51. }
  52. }