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
|
|
|
* Replaces xml declaration with a fixed one. |
12
|
|
|
*/ |
13
|
|
|
class OverwriteXmlDeclarationTransformer implements ResourceTransformerInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $xmlDeclaration; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string regexp |
22
|
|
|
*/ |
23
|
|
|
protected $xmlDeclarationRegEx = '/^\<\?xml.*\?\>/i'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param string $xmlDeclaration |
27
|
|
|
*/ |
28
|
6 |
|
public function __construct($xmlDeclaration = '<?xml version="1.0" encoding="UTF-8"?>') |
29
|
|
|
{ |
30
|
6 |
|
$this->xmlDeclaration = $xmlDeclaration; |
31
|
6 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @inheritdoc |
35
|
|
|
*/ |
36
|
6 |
View Code Duplication |
public function transform(ResourceInterface $resource, ResourceCollection $collection) |
|
|
|
|
37
|
|
|
{ |
38
|
6 |
|
$file = $resource->getFile()->getPathname(); |
39
|
|
|
|
40
|
|
|
// the file could be big, so just read the |
41
|
6 |
|
$tmpFile = tempnam(sys_get_temp_dir(), $file); |
42
|
6 |
|
$old = fopen($file, 'r'); |
43
|
6 |
|
$new = fopen($tmpFile, 'w'); |
44
|
|
|
|
45
|
|
|
// write the beginning with the xml declaration replaced |
46
|
6 |
|
fwrite($new, preg_replace($this->xmlDeclarationRegEx, $this->xmlDeclaration, fread($old, 96))); |
47
|
|
|
|
48
|
|
|
// now copy the rest of the file |
49
|
6 |
|
while (!feof($old)) { |
50
|
|
|
fwrite($new, fread($old, 8192)); |
51
|
|
|
} |
52
|
|
|
|
53
|
6 |
|
fclose($old); |
54
|
6 |
|
fclose($new); |
55
|
|
|
|
56
|
|
|
// atomic write |
57
|
6 |
|
$this->rename($tmpFile, $file); |
58
|
|
|
|
59
|
6 |
|
$transport = FileTransport::create($file); |
60
|
|
|
|
61
|
6 |
|
if ($resource->getTransport()) { |
62
|
|
|
$transport->setDestinationDir($resource->getTransport()->getDestinationDir()); |
63
|
|
|
} |
64
|
|
|
|
65
|
6 |
|
return new FileResource($transport); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @inheritdoc |
70
|
|
|
*/ |
71
|
|
View Code Duplication |
public function needsTransforming(ResourceInterface $resource) |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
$file = $resource->getFile()->getPathname(); |
74
|
|
|
$handle = fopen($file, 'r'); |
75
|
|
|
|
76
|
|
|
$result = (bool) preg_match($this->xmlDeclarationRegEx, fread($handle, 96)); |
77
|
|
|
|
78
|
|
|
fclose($handle); |
79
|
|
|
|
80
|
|
|
return $result; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $old |
85
|
|
|
* @param string $new |
86
|
|
|
* |
87
|
|
|
* @throws \RuntimeException |
88
|
|
|
*/ |
89
|
6 |
|
protected function rename($old, $new) |
90
|
|
|
{ |
91
|
6 |
|
if (!rename($old, $new)) { |
92
|
|
|
throw new \RuntimeException(sprintf('Could not rename %s to %s', $old, $new)); |
93
|
|
|
} |
94
|
6 |
|
} |
95
|
|
|
} |
96
|
|
|
|
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.