Chris@0: header; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Set the PO header for the current stream. Chris@0: * Chris@0: * @param \Drupal\Component\Gettext\PoHeader $header Chris@0: * The Gettext PO header to set. Chris@0: */ Chris@0: public function setHeader(PoHeader $header) { Chris@17: $this->header = $header; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the current language code used. Chris@0: * Chris@0: * @return string Chris@0: * The language code. Chris@0: */ Chris@0: public function getLangcode() { Chris@17: return $this->langcode; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Set the language code. Chris@0: * Chris@0: * @param string $langcode Chris@0: * The language code. Chris@0: */ Chris@0: public function setLangcode($langcode) { Chris@17: $this->langcode = $langcode; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function open() { Chris@0: // Open in write mode. Will overwrite the stream if it already exists. Chris@17: $this->fd = fopen($this->getURI(), 'w'); Chris@0: // Write the header at the start. Chris@0: $this->writeHeader(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements Drupal\Component\Gettext\PoStreamInterface::close(). Chris@0: * Chris@14: * @throws \Exception Chris@0: * If the stream is not open. Chris@0: */ Chris@0: public function close() { Chris@17: if ($this->fd) { Chris@17: fclose($this->fd); Chris@0: } Chris@0: else { Chris@14: throw new \Exception('Cannot close stream that is not open.'); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Write data to the stream. Chris@0: * Chris@0: * @param string $data Chris@0: * Piece of string to write to the stream. If the value is not directly a Chris@0: * string, casting will happen in writing. Chris@0: * Chris@14: * @throws \Exception Chris@0: * If writing the data is not possible. Chris@0: */ Chris@0: private function write($data) { Chris@17: $result = fwrite($this->fd, $data); Chris@14: if ($result === FALSE || $result != strlen($data)) { Chris@14: throw new \Exception('Unable to write data: ' . substr($data, 0, 20)); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Write the PO header to the stream. Chris@0: */ Chris@0: private function writeHeader() { Chris@17: $this->write($this->header); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function writeItem(PoItem $item) { Chris@0: $this->write($item); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function writeItems(PoReaderInterface $reader, $count = -1) { Chris@0: $forever = $count == -1; Chris@0: while (($count-- > 0 || $forever) && ($item = $reader->readItem())) { Chris@0: $this->writeItem($item); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements Drupal\Component\Gettext\PoStreamInterface::getURI(). Chris@0: * Chris@14: * @throws \Exception Chris@0: * If the URI is not set. Chris@0: */ Chris@0: public function getURI() { Chris@17: if (empty($this->uri)) { Chris@14: throw new \Exception('No URI set.'); Chris@0: } Chris@17: return $this->uri; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function setURI($uri) { Chris@17: $this->uri = $uri; Chris@0: } Chris@0: Chris@0: }