1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TreeHouse\Feeder\Resource\Transformer; |
4
|
|
|
|
5
|
|
|
use TreeHouse\Feeder\Resource\FileResource; |
6
|
|
|
use TreeHouse\Feeder\Resource\ResourceCollection; |
7
|
|
|
use TreeHouse\Feeder\Resource\ResourceInterface; |
8
|
|
|
use TreeHouse\Feeder\Transport\FileTransport; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Strips the BOM from the beginning of the resource. |
12
|
|
|
*/ |
13
|
|
|
class RemoveByteOrderMarkTransformer implements ResourceTransformerInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
protected $boms; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Constructor. |
22
|
|
|
*/ |
23
|
10 |
|
public function __construct() |
24
|
|
|
{ |
25
|
10 |
|
$this->boms = [ |
26
|
10 |
|
'\x00\x00\xFE\xFF', // UTF-32 (BE) |
|
|
|
|
27
|
10 |
|
'\xFF\xFE\x00\x00', // UTF-32 (LE) |
|
|
|
|
28
|
10 |
|
'\xFE\xFF', // UTF-16 (BE) |
|
|
|
|
29
|
10 |
|
'\xFF\xFE', // UTF-16 (LE) |
|
|
|
|
30
|
10 |
|
'\xEF\xBB\xBF', // UTF-8 |
31
|
|
|
]; |
32
|
10 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @inheritdoc |
36
|
|
|
*/ |
37
|
10 |
View Code Duplication |
public function transform(ResourceInterface $resource, ResourceCollection $collection) |
|
|
|
|
38
|
|
|
{ |
39
|
10 |
|
$file = $resource->getFile()->getPathname(); |
40
|
|
|
|
41
|
|
|
// the file could be big, so just read the |
42
|
10 |
|
$tmpFile = tempnam(sys_get_temp_dir(), $file); |
43
|
10 |
|
$old = fopen($file, 'r'); |
44
|
10 |
|
$new = fopen($tmpFile, 'w'); |
45
|
|
|
|
46
|
|
|
// write the beginning with the BOM stripped |
47
|
10 |
|
fwrite($new, preg_replace($this->getBomRegex(), '', fread($old, 16))); |
48
|
|
|
|
49
|
|
|
// now copy the rest of the file |
50
|
10 |
|
while (!feof($old)) { |
51
|
10 |
|
fwrite($new, fread($old, 8192)); |
52
|
10 |
|
} |
53
|
|
|
|
54
|
10 |
|
fclose($old); |
55
|
10 |
|
fclose($new); |
56
|
|
|
|
57
|
|
|
// atomic write |
58
|
10 |
|
$this->rename($tmpFile, $file); |
59
|
|
|
|
60
|
10 |
|
$transport = FileTransport::create($file); |
61
|
|
|
|
62
|
10 |
|
if ($resource->getTransport()) { |
63
|
|
|
$transport->setDestinationDir($resource->getTransport()->getDestinationDir()); |
64
|
|
|
} |
65
|
|
|
|
66
|
10 |
|
return new FileResource($transport); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @inheritdoc |
71
|
|
|
*/ |
72
|
|
View Code Duplication |
public function needsTransforming(ResourceInterface $resource) |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
$file = $resource->getFile()->getPathname(); |
75
|
|
|
$handle = fopen($file, 'r'); |
76
|
|
|
|
77
|
|
|
$result = (bool) preg_match($this->getBomRegex(), fread($handle, 16)); |
78
|
|
|
|
79
|
|
|
fclose($handle); |
80
|
|
|
|
81
|
|
|
return $result; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $old |
86
|
|
|
* @param string $new |
87
|
|
|
* |
88
|
|
|
* @throws \RuntimeException |
89
|
|
|
*/ |
90
|
10 |
|
protected function rename($old, $new) |
91
|
|
|
{ |
92
|
10 |
|
if (!rename($old, $new)) { |
93
|
|
|
throw new \RuntimeException(sprintf('Could not rename %s to %s', $old, $new)); |
94
|
|
|
} |
95
|
10 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
10 |
|
protected function getBomRegex() |
101
|
|
|
{ |
102
|
10 |
|
return sprintf('/^(%s)/', implode('|', $this->boms)); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.