comparison core/lib/Drupal/Component/Gettext/PoStreamWriter.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 1fec387a4317
children
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
10 /** 10 /**
11 * URI of the PO stream that is being written. 11 * URI of the PO stream that is being written.
12 * 12 *
13 * @var string 13 * @var string
14 */ 14 */
15 private $_uri; 15 protected $uri;
16 16
17 /** 17 /**
18 * The Gettext PO header. 18 * The Gettext PO header.
19 * 19 *
20 * @var \Drupal\Component\Gettext\PoHeader 20 * @var \Drupal\Component\Gettext\PoHeader
21 */ 21 */
22 private $_header; 22 protected $header;
23 23
24 /** 24 /**
25 * File handle of the current PO stream. 25 * File handle of the current PO stream.
26 * 26 *
27 * @var resource 27 * @var resource
28 */ 28 */
29 private $_fd; 29 protected $fd;
30
31 /**
32 * The language code of this writer.
33 *
34 * @var string
35 */
36 protected $langcode;
30 37
31 /** 38 /**
32 * Gets the PO header of the current stream. 39 * Gets the PO header of the current stream.
33 * 40 *
34 * @return \Drupal\Component\Gettext\PoHeader 41 * @return \Drupal\Component\Gettext\PoHeader
35 * The Gettext PO header. 42 * The Gettext PO header.
36 */ 43 */
37 public function getHeader() { 44 public function getHeader() {
38 return $this->_header; 45 return $this->header;
39 } 46 }
40 47
41 /** 48 /**
42 * Set the PO header for the current stream. 49 * Set the PO header for the current stream.
43 * 50 *
44 * @param \Drupal\Component\Gettext\PoHeader $header 51 * @param \Drupal\Component\Gettext\PoHeader $header
45 * The Gettext PO header to set. 52 * The Gettext PO header to set.
46 */ 53 */
47 public function setHeader(PoHeader $header) { 54 public function setHeader(PoHeader $header) {
48 $this->_header = $header; 55 $this->header = $header;
49 } 56 }
50 57
51 /** 58 /**
52 * Gets the current language code used. 59 * Gets the current language code used.
53 * 60 *
54 * @return string 61 * @return string
55 * The language code. 62 * The language code.
56 */ 63 */
57 public function getLangcode() { 64 public function getLangcode() {
58 return $this->_langcode; 65 return $this->langcode;
59 } 66 }
60 67
61 /** 68 /**
62 * Set the language code. 69 * Set the language code.
63 * 70 *
64 * @param string $langcode 71 * @param string $langcode
65 * The language code. 72 * The language code.
66 */ 73 */
67 public function setLangcode($langcode) { 74 public function setLangcode($langcode) {
68 $this->_langcode = $langcode; 75 $this->langcode = $langcode;
69 } 76 }
70 77
71 /** 78 /**
72 * {@inheritdoc} 79 * {@inheritdoc}
73 */ 80 */
74 public function open() { 81 public function open() {
75 // Open in write mode. Will overwrite the stream if it already exists. 82 // Open in write mode. Will overwrite the stream if it already exists.
76 $this->_fd = fopen($this->getURI(), 'w'); 83 $this->fd = fopen($this->getURI(), 'w');
77 // Write the header at the start. 84 // Write the header at the start.
78 $this->writeHeader(); 85 $this->writeHeader();
79 } 86 }
80 87
81 /** 88 /**
83 * 90 *
84 * @throws \Exception 91 * @throws \Exception
85 * If the stream is not open. 92 * If the stream is not open.
86 */ 93 */
87 public function close() { 94 public function close() {
88 if ($this->_fd) { 95 if ($this->fd) {
89 fclose($this->_fd); 96 fclose($this->fd);
90 } 97 }
91 else { 98 else {
92 throw new \Exception('Cannot close stream that is not open.'); 99 throw new \Exception('Cannot close stream that is not open.');
93 } 100 }
94 } 101 }
102 * 109 *
103 * @throws \Exception 110 * @throws \Exception
104 * If writing the data is not possible. 111 * If writing the data is not possible.
105 */ 112 */
106 private function write($data) { 113 private function write($data) {
107 $result = fwrite($this->_fd, $data); 114 $result = fwrite($this->fd, $data);
108 if ($result === FALSE || $result != strlen($data)) { 115 if ($result === FALSE || $result != strlen($data)) {
109 throw new \Exception('Unable to write data: ' . substr($data, 0, 20)); 116 throw new \Exception('Unable to write data: ' . substr($data, 0, 20));
110 } 117 }
111 } 118 }
112 119
113 /** 120 /**
114 * Write the PO header to the stream. 121 * Write the PO header to the stream.
115 */ 122 */
116 private function writeHeader() { 123 private function writeHeader() {
117 $this->write($this->_header); 124 $this->write($this->header);
118 } 125 }
119 126
120 /** 127 /**
121 * {@inheritdoc} 128 * {@inheritdoc}
122 */ 129 */
139 * 146 *
140 * @throws \Exception 147 * @throws \Exception
141 * If the URI is not set. 148 * If the URI is not set.
142 */ 149 */
143 public function getURI() { 150 public function getURI() {
144 if (empty($this->_uri)) { 151 if (empty($this->uri)) {
145 throw new \Exception('No URI set.'); 152 throw new \Exception('No URI set.');
146 } 153 }
147 return $this->_uri; 154 return $this->uri;
148 } 155 }
149 156
150 /** 157 /**
151 * {@inheritdoc} 158 * {@inheritdoc}
152 */ 159 */
153 public function setURI($uri) { 160 public function setURI($uri) {
154 $this->_uri = $uri; 161 $this->uri = $uri;
155 } 162 }
156 163
157 } 164 }