summaryrefslogtreecommitdiffstats
path: root/core/lib/Drupal/Component/Gettext/PoStreamWriter.php
blob: d066307d83f7d23d80d492c6561d8bb9e3127e4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php

/**
 * @file
 * Definition of Drupal\Component\Gettext\PoStreamWriter.
 */

namespace Drupal\Component\Gettext;

use Drupal\Component\Gettext\PoHeader;
use Drupal\Component\Gettext\PoItem;
use Drupal\Component\Gettext\PoReaderInterface;
use Drupal\Component\Gettext\PoWriterInterface;
use Drupal\Component\Gettext\PoStreamInterface;

/**
 * Defines a Gettext PO stream writer.
 */
class PoStreamWriter implements PoWriterInterface, PoStreamInterface {

  /**
   * URI of the PO stream that is being written.
   *
   * @var string
   */
  private $_uri;

  /**
   * The Gettext PO header.
   *
   * @var Drupal\Component\Gettext\PoHeader
   */
  private $_header;

  /**
   * File handle of the current PO stream.
   *
   * @var resource
   */
  private $_fd;

  /**
   * Get the PO header of the current stream.
   *
   * @return Drupal\Component\Gettext\PoHeader
   *   The Gettext PO header.
   */
  public function getHeader() {
    return $this->_header;
  }

  /**
   * Set the PO header for the current stream.
   *
   * @param Drupal\Component\Gettext\PoHeader $header
   *   The Gettext PO header to set.
   */
  public function setHeader(PoHeader $header) {
    $this->_header = $header;
  }

  /**
   * Get the current language code used.
   *
   * @return string
   *   The language code.
   */
  public function getLangcode() {
    return $this->_langcode;
  }

  /**
   * Set the language code.
   *
   * @param string $langcode
   *   The language code.
   */
  public function setLangcode($langcode) {
    $this->_langcode = $langcode;
  }

  /**
   * Implements Drupal\Component\Gettext\PoStreamInterface::open().
   */
  public function open() {
    // Open in write mode. Will overwrite the stream if it already exists.
    $this->_fd = fopen($this->getURI(), 'w');
    // Write the header at the start.
    $this->writeHeader();
  }

  /**
   * Implements Drupal\Component\Gettext\PoStreamInterface::close().
   *
   * @throws Exception
   *   If the stream is not open.
   */
  public function close() {
    if ($this->_fd) {
      fclose($this->_fd);
    }
    else {
      throw new Exception('Cannot close stream that is not open.');
    }
  }

  /**
   * Write data to the stream.
   *
   * @param string $data
   *   Piece of string to write to the stream. If the value is not directly a
   *   string, casting will happen in writing.
   *
   * @throws Exception
   *   If writing the data is not possible.
   */
  private function write($data) {
    $result = fputs($this->_fd, $data);
    if ($result === FALSE) {
      throw new Exception('Unable to write data: ' . substr($data, 0, 20));
    }
  }

  /**
   * Write the PO header to the stream.
   */
  private function writeHeader() {
    $this->write($this->_header);
  }

  /**
   * Implements Drupal\Component\Gettext\PoWriterInterface::writeItem().
   */
  public function writeItem(PoItem $item) {
    $this->write($item);
  }

  /**
   * Implements Drupal\Component\Gettext\PoWriterInterface::writeItems().
   */
  public function writeItems(PoReaderInterface $reader, $count = -1) {
    $forever = $count == -1;
    while (($count-- > 0 || $forever) && ($item = $reader->readItem())) {
      $this->writeItem($item);
    }
  }

  /**
   * Implements Drupal\Component\Gettext\PoStreamInterface::getURI().
   *
   * @throws Exception
   *   If the URI is not set.
   */
  public function getURI() {
    if (empty($this->_uri)) {
      throw new Exception('No URI set.');
    }
    return $this->_uri;
  }

  /**
   * Implements Drupal\Component\Gettext\PoStreamInterface::setURI().
   */
  public function setURI($uri) {
    $this->_uri = $uri;
  }

}