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