-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPoStreamWriter.php
164 lines (145 loc) · 3.19 KB
/
PoStreamWriter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
namespace Drupal\Component\Gettext;
/**
* Defines a Gettext PO stream writer.
*/
class PoStreamWriter implements PoWriterInterface, PoStreamInterface {
/**
* URI of the PO stream that is being written.
*
* @var string
*/
protected $uri;
/**
* The Gettext PO header.
*
* @var \Drupal\Component\Gettext\PoHeader
*/
protected $header;
/**
* File handle of the current PO stream.
*
* @var resource
*/
protected $fd;
/**
* The language code of this writer.
*
* @var string
*/
protected $langcode;
/**
* Gets the PO header of the current stream.
*
* @return \Drupal\Component\Gettext\PoHeader
* The Gettext PO header.
*/
public function getHeader() {
return $this->header;
}
/**
* Set the PO header for the current stream.
*
* @param \Drupal\Component\Gettext\PoHeader $header
* The Gettext PO header to set.
*/
public function setHeader(PoHeader $header) {
$this->header = $header;
}
/**
* Gets the current language code used.
*
* @return string
* The language code.
*/
public function getLangcode() {
return $this->langcode;
}
/**
* Set the language code.
*
* @param string $langcode
* The language code.
*/
public function setLangcode($langcode) {
$this->langcode = $langcode;
}
/**
* {@inheritdoc}
*/
public function open() {
// Open in write mode. Will overwrite the stream if it already exists.
$this->fd = fopen($this->getURI(), 'w');
// Write the header at the start.
$this->writeHeader();
}
/**
* Implements Drupal\Component\Gettext\PoStreamInterface::close().
*
* @throws \Exception
* If the stream is not open.
*/
public function close() {
if ($this->fd) {
fclose($this->fd);
}
else {
throw new \Exception('Cannot close stream that is not open.');
}
}
/**
* Write data to the stream.
*
* @param string $data
* Piece of string to write to the stream. If the value is not directly a
* string, casting will happen in writing.
*
* @throws \Exception
* If writing the data is not possible.
*/
private function write($data) {
$result = fwrite($this->fd, $data);
if ($result === FALSE || $result != strlen($data)) {
throw new \Exception('Unable to write data: ' . substr($data, 0, 20));
}
}
/**
* Write the PO header to the stream.
*/
private function writeHeader() {
$this->write($this->header);
}
/**
* {@inheritdoc}
*/
public function writeItem(PoItem $item) {
$this->write($item);
}
/**
* {@inheritdoc}
*/
public function writeItems(PoReaderInterface $reader, $count = -1) {
$forever = $count == -1;
while (($count-- > 0 || $forever) && ($item = $reader->readItem())) {
$this->writeItem($item);
}
}
/**
* Implements Drupal\Component\Gettext\PoStreamInterface::getURI().
*
* @throws \Exception
* If the URI is not set.
*/
public function getURI() {
if (empty($this->uri)) {
throw new \Exception('No URI set.');
}
return $this->uri;
}
/**
* {@inheritdoc}
*/
public function setURI($uri) {
$this->uri = $uri;
}
}