PoStreamWriter.php 3.2 KB

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