PoStreamWriter.php 3.1 KB

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