GNUTarOutputParser.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /*
  3. * This file is part of Zippy.
  4. *
  5. * (c) Alchemy <info@alchemy.fr>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Alchemy\Zippy\Parser;
  11. use Alchemy\Zippy\Exception\RuntimeException;
  12. /**
  13. * This class is responsible of parsing GNUTar command line output
  14. */
  15. class GNUTarOutputParser implements ParserInterface
  16. {
  17. const PERMISSIONS = '([ldrwx-]+)';
  18. const OWNER = '([a-z][-a-z0-9]*)';
  19. const GROUP = '([a-z][-a-z0-9]*)';
  20. const FILESIZE = '(\d*)';
  21. const ISO_DATE = '([0-9]+-[0-9]+-[0-9]+\s+[0-9]+:[0-9]+)';
  22. const FILENAME = '(.*)';
  23. /**
  24. * @inheritdoc
  25. */
  26. public function parseFileListing($output)
  27. {
  28. $lines = array_values(array_filter(explode("\n", $output)));
  29. $members = array();
  30. foreach ($lines as $line) {
  31. $matches = array();
  32. // -rw-r--r-- gray/staff 62373 2006-06-09 12:06 apple
  33. if (!preg_match_all("#".
  34. self::PERMISSIONS . "\s+" . // match (-rw-r--r--)
  35. self::OWNER . "/" . // match (gray)
  36. self::GROUP . "\s+" . // match (staff)
  37. self::FILESIZE . "\s+" . // match (62373)
  38. self::ISO_DATE . "\s+" . // match (2006-06-09 12:06)
  39. self::FILENAME . // match (apple)
  40. "#",
  41. $line, $matches, PREG_SET_ORDER
  42. )) {
  43. continue;
  44. }
  45. $chunks = array_shift($matches);
  46. if (7 !== count($chunks)) {
  47. continue;
  48. }
  49. $date = \DateTime::createFromFormat("Y-m-d H:i", $chunks[5]);
  50. if (false === $date) {
  51. throw new RuntimeException(sprintf('Failed to parse mtime date from %s', $line));
  52. }
  53. $members[] = array(
  54. 'location' => $chunks[6],
  55. 'size' => $chunks[4],
  56. 'mtime' => $date,
  57. 'is_dir' => 'd' === $chunks[1][0]
  58. );
  59. }
  60. return $members;
  61. }
  62. /**
  63. * @inheritdoc
  64. */
  65. public function parseInflatorVersion($output)
  66. {
  67. $chunks = explode(' ', $output, 3);
  68. if (2 > count($chunks)) {
  69. return null;
  70. }
  71. list(, $version) = $chunks;
  72. return $version;
  73. }
  74. /**
  75. * @inheritdoc
  76. */
  77. public function parseDeflatorVersion($output)
  78. {
  79. return $this->parseInflatorVersion($output);
  80. }
  81. }