1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Zippy. |
5
|
|
|
* |
6
|
|
|
* (c) Alchemy <[email protected]> |
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 Alchemy\Zippy\Parser; |
12
|
|
|
|
13
|
|
|
use Alchemy\Zippy\Exception\RuntimeException; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* This class is responsible of parsing GNUTar command line output |
17
|
|
|
*/ |
18
|
|
|
class GNUTarOutputParser implements ParserInterface |
19
|
|
|
{ |
20
|
|
|
const PERMISSIONS = '([ldrwx-]+)'; |
21
|
|
|
const OWNER = '([a-z][-a-z0-9]*)'; |
22
|
|
|
const GROUP = '([a-z][-a-z0-9]*)'; |
23
|
|
|
const FILESIZE = '(\d*)'; |
24
|
|
|
const ISO_DATE = '([0-9]+-[0-9]+-[0-9]+\s+[0-9]+:[0-9]+)'; |
25
|
|
|
const FILENAME = '(.*)'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @inheritdoc |
29
|
|
|
*/ |
30
|
|
|
public function parseFileListing($output) |
31
|
|
|
{ |
32
|
|
|
$lines = array_values(array_filter(explode("\n", $output))); |
33
|
|
|
$members = array(); |
34
|
|
|
|
35
|
|
|
foreach ($lines as $line) { |
36
|
|
|
$matches = array(); |
37
|
|
|
|
38
|
|
|
// -rw-r--r-- gray/staff 62373 2006-06-09 12:06 apple |
39
|
|
View Code Duplication |
if (!preg_match_all("#". |
|
|
|
|
40
|
|
|
self::PERMISSIONS . "\s+" . // match (-rw-r--r--) |
41
|
|
|
self::OWNER . "/" . // match (gray) |
42
|
|
|
self::GROUP . "\s+" . // match (staff) |
43
|
|
|
self::FILESIZE . "\s+" . // match (62373) |
44
|
|
|
self::ISO_DATE . "\s+" . // match (2006-06-09 12:06) |
45
|
|
|
self::FILENAME . // match (apple) |
46
|
|
|
"#", |
47
|
|
|
$line, $matches, PREG_SET_ORDER |
48
|
|
|
)) { |
49
|
|
|
continue; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$chunks = array_shift($matches); |
53
|
|
|
|
54
|
|
|
if (7 !== count($chunks)) { |
55
|
|
|
continue; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$date = \DateTime::createFromFormat("Y-m-d H:i", $chunks[5]); |
59
|
|
|
|
60
|
|
|
if (false === $date) { |
61
|
|
|
throw new RuntimeException(sprintf('Failed to parse mtime date from %s', $line)); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$members[] = array( |
65
|
|
|
'location' => $chunks[6], |
66
|
|
|
'size' => $chunks[4], |
67
|
|
|
'mtime' => $date, |
68
|
|
|
'is_dir' => 'd' === $chunks[1][0] |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $members; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @inheritdoc |
77
|
|
|
*/ |
78
|
|
View Code Duplication |
public function parseInflatorVersion($output) |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
$chunks = explode(' ', $output, 3); |
81
|
|
|
|
82
|
|
|
if (2 > count($chunks)) { |
83
|
|
|
return null; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
list(, $version) = $chunks; |
87
|
|
|
|
88
|
|
|
return $version; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @inheritdoc |
93
|
|
|
*/ |
94
|
|
|
public function parseDeflatorVersion($output) |
95
|
|
|
{ |
96
|
|
|
return $this->parseInflatorVersion($output); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.