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