1
|
|
|
<?php namespace Sepia\PoParser\Handler; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright (c) 2012 Raúl Ferràs [email protected] |
5
|
|
|
* All rights reserved. |
6
|
|
|
* |
7
|
|
|
* Redistribution and use in source and binary forms, with or without |
8
|
|
|
* modification, are permitted provided that the following conditions |
9
|
|
|
* are met: |
10
|
|
|
* 1. Redistributions of source code must retain the above copyright |
11
|
|
|
* notice, this list of conditions and the following disclaimer. |
12
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright |
13
|
|
|
* notice, this list of conditions and the following disclaimer in the |
14
|
|
|
* documentation and/or other materials provided with the distribution. |
15
|
|
|
* 3. Neither the name of copyright holders nor the names of its |
16
|
|
|
* contributors may be used to endorse or promote products derived |
17
|
|
|
* from this software without specific prior written permission. |
18
|
|
|
* |
19
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
20
|
|
|
* ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
21
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
22
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS OR CONTRIBUTORS |
23
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
24
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
25
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
26
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
27
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
28
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
29
|
|
|
* POSSIBILITY OF SUCH DAMAGE. |
30
|
|
|
* |
31
|
|
|
* https://github.com/raulferras/PHP-po-parser |
32
|
|
|
*/ |
33
|
|
|
class FileHandler implements HandlerInterface |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @var resource |
37
|
|
|
*/ |
38
|
|
|
protected $fileHandle; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $filepath |
42
|
|
|
* |
43
|
|
|
* @throws \Exception |
44
|
|
|
*/ |
45
|
39 |
|
public function __construct($filepath) |
46
|
|
|
{ |
47
|
39 |
|
if (file_exists($filepath) === false) { |
48
|
|
|
throw new \Exception('PoParser: Input File does not exists: "' . htmlspecialchars($filepath) . '"'); |
49
|
39 |
|
} elseif (is_readable($filepath) === false) { |
50
|
|
|
throw new \Exception('PoParser: File is not readable: "' . htmlspecialchars($filepath) . '"'); |
51
|
|
|
} |
52
|
|
|
|
53
|
39 |
|
$this->fileHandle = @fopen($filepath, "r"); |
54
|
39 |
|
if ($this->fileHandle===false) { |
55
|
|
|
throw new \Exception('PoParser: Could not open file: "' . htmlspecialchars($filepath) . '"'); |
56
|
|
|
} |
57
|
39 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
39 |
|
public function getNextLine() |
63
|
|
|
{ |
64
|
39 |
|
return fgets($this->fileHandle); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
39 |
|
public function ended() |
71
|
|
|
{ |
72
|
39 |
|
return feof($this->fileHandle); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
39 |
|
public function close() |
79
|
|
|
{ |
80
|
39 |
|
return @fclose($this->fileHandle); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @inheritdoc |
85
|
|
|
* |
86
|
|
|
* @param string $output |
87
|
|
|
* @param array $params |
88
|
|
|
*/ |
89
|
15 |
|
public function save($output, $params) |
90
|
|
|
{ |
91
|
15 |
|
$outputFilePath = isset($params['filepath']) ? $params['filepath'] : null; |
92
|
|
|
|
93
|
15 |
|
if ($outputFilePath) { |
94
|
15 |
|
$fileHandle = @fopen($params['filepath'], 'w'); |
95
|
15 |
|
if ($fileHandle === false) { |
96
|
|
|
throw new \RuntimeException( |
97
|
|
|
sprintf( |
98
|
|
|
'Could not open filename "%s" for writing.', |
99
|
10 |
|
$params['filepath'] |
100
|
|
|
) |
101
|
|
|
); |
102
|
|
|
} |
103
|
5 |
|
} else { |
104
|
|
|
$fileHandle = $this->fileHandle; |
105
|
|
|
if (is_resource($fileHandle) === false) { |
106
|
|
|
throw new \RuntimeException( |
107
|
|
|
'No source file opened nor `filepath` parameter specified in FileHandler::save method.' |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
15 |
|
$bytesWritten = @fwrite($fileHandle, $output); |
113
|
15 |
|
if ($bytesWritten === false) { |
114
|
|
|
throw new \RuntimeException('Could not write data into file.'); |
115
|
|
|
} |
116
|
|
|
|
117
|
15 |
|
if (isset($params['filepath'])) { |
118
|
15 |
|
@fclose($fileHandle); |
|
|
|
|
119
|
5 |
|
} |
120
|
|
|
} |
121
|
|
|
} |
If you suppress an error, we recommend checking for the error condition explicitly: