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