PharArchiver.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. // Phar would otherwise load the file which we don't want
  32. if (file_exists($target)) {
  33. unlink($target);
  34. }
  35. try {
  36. $phar = new \PharData($target, null, null, static::$formats[$format]);
  37. $files = new ArchivableFilesFinder($sources, $excludes);
  38. $phar->buildFromIterator($files, $sources);
  39. return $target;
  40. } catch (\UnexpectedValueException $e) {
  41. $message = sprintf("Could not create archive '%s' from '%s': %s",
  42. $target,
  43. $sources,
  44. $e->getMessage()
  45. );
  46. throw new \RuntimeException($message, $e->getCode(), $e);
  47. }
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function supports($format, $sourceType)
  53. {
  54. return isset(static::$formats[$format]);
  55. }
  56. }