comparison core/lib/Drupal/Component/Gettext/PoStreamWriter.php @ 0:4c8ae668cc8c

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