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
|
|
|
/** |
14
|
|
|
* This class is responsible of parsing GNUTar command line output |
15
|
|
|
*/ |
16
|
|
|
class ZipOutputParser implements ParserInterface |
17
|
|
|
{ |
18
|
|
|
const LENGTH = '(\d*)'; |
19
|
|
|
const ISO_DATE = '([0-9]+-[0-9]+-[0-9]+\s+[0-9]+:[0-9]+)'; |
20
|
|
|
const FILENAME = '(.*)'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private $dateFormat; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param string $dateFormat |
29
|
|
|
*/ |
30
|
|
|
public function __construct($dateFormat = "Y-m-d H:i") |
31
|
|
|
{ |
32
|
|
|
$this->dateFormat = $dateFormat; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @inheritdoc |
37
|
|
|
*/ |
38
|
|
|
public function parseFileListing($output) |
39
|
|
|
{ |
40
|
|
|
$lines = array_values(array_filter(explode("\n", $output))); |
41
|
|
|
$members = array(); |
42
|
|
|
|
43
|
|
|
foreach ($lines as $line) { |
44
|
|
|
$matches = array(); |
45
|
|
|
|
46
|
|
|
// 785 2012-10-24 10:39 file |
47
|
|
|
if (!preg_match_all("#" . |
48
|
|
|
self::LENGTH . "\s+" . // match (785) |
49
|
|
|
self::ISO_DATE . "\s+" . // match (2012-10-24 10:39) |
50
|
|
|
self::FILENAME . // match (file) |
51
|
|
|
"#", |
52
|
|
|
$line, $matches, PREG_SET_ORDER |
53
|
|
|
)) { |
54
|
|
|
continue; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$chunks = array_shift($matches); |
58
|
|
|
|
59
|
|
|
if (4 !== count($chunks)) { |
60
|
|
|
continue; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$mtime = \DateTime::createFromFormat($this->dateFormat, $chunks[2]); |
64
|
|
|
|
65
|
|
|
if ($mtime === false) { |
66
|
|
|
// See https://github.com/alchemy-fr/Zippy/issues/111#issuecomment-251668427 |
67
|
|
|
$mtime = \DateTime::createFromFormat('H:i Y-m-d', $chunks[2]); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if ($mtime === false) { |
71
|
|
|
$mtime = new \DateTime($chunks[2]); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$members[] = array( |
75
|
|
|
'location' => $chunks[3], |
76
|
|
|
'size' => $chunks[1], |
77
|
|
|
'mtime' => $mtime, |
78
|
|
|
'is_dir' => '/' === substr($chunks[3], -1) |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $members; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @inheritdoc |
87
|
|
|
*/ |
88
|
|
View Code Duplication |
public function parseInflatorVersion($output) |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
$lines = array_values(array_filter(explode("\n", $output, 3))); |
91
|
|
|
|
92
|
|
|
$chunks = explode(' ', $lines[1], 3); |
93
|
|
|
|
94
|
|
|
if (2 > count($chunks)) { |
95
|
|
|
return null; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
list(, $version) = $chunks; |
99
|
|
|
|
100
|
|
|
return $version; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @inheritdoc |
105
|
|
|
*/ |
106
|
|
View Code Duplication |
public function parseDeflatorVersion($output) |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
$lines = array_values(array_filter(explode("\n", $output, 2))); |
109
|
|
|
$firstLine = array_shift($lines); |
110
|
|
|
$chunks = explode(' ', $firstLine, 3); |
111
|
|
|
|
112
|
|
|
if (2 > count($chunks)) { |
113
|
|
|
return null; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
list(, $version) = $chunks; |
117
|
|
|
|
118
|
|
|
return $version; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
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.