Write.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. /**
  3. * Hoa
  4. *
  5. *
  6. * @license
  7. *
  8. * New BSD License
  9. *
  10. * Copyright © 2007-2017, Hoa community. All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * * Neither the name of the Hoa nor the names of its contributors may be
  20. * used to endorse or promote products derived from this software without
  21. * specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. namespace Hoa\File\Temporary;
  36. use Hoa\File;
  37. use Hoa\Stream;
  38. /**
  39. * Class \Hoa\File\Temporary\Write.
  40. *
  41. * Write into a temporary file.
  42. *
  43. * @copyright Copyright © 2007-2017 Hoa community
  44. * @license New BSD License
  45. */
  46. class Write extends Temporary implements Stream\IStream\Out
  47. {
  48. /**
  49. * Open a file.
  50. *
  51. * @param string $streamName Stream name.
  52. * @param string $mode Open mode, see the parent::MODE_* constants.
  53. * @param string $context Context ID (please, see the
  54. * \Hoa\Stream\Context class).
  55. * @param bool $wait Differ opening or not.
  56. */
  57. public function __construct(
  58. $streamName,
  59. $mode = parent::MODE_APPEND_WRITE,
  60. $context = null,
  61. $wait = false
  62. ) {
  63. parent::__construct($streamName, $mode, $context, $wait);
  64. return;
  65. }
  66. /**
  67. * Open the stream and return the associated resource.
  68. *
  69. * @param string $streamName Stream name (e.g. path or URL).
  70. * @param \Hoa\Stream\Context $context Context.
  71. * @return resource
  72. * @throws \Hoa\File\Exception\FileDoesNotExist
  73. * @throws \Hoa\File\Exception
  74. */
  75. protected function &_open($streamName, Stream\Context $context = null)
  76. {
  77. static $createModes = [
  78. parent::MODE_TRUNCATE_WRITE,
  79. parent::MODE_APPEND_WRITE,
  80. parent::MODE_CREATE_WRITE
  81. ];
  82. if (!in_array($this->getMode(), $createModes)) {
  83. throw new File\Exception(
  84. 'Open mode are not supported; given %d. Only %s are supported.',
  85. 0,
  86. [$this->getMode(), implode(', ', $createModes)]
  87. );
  88. }
  89. preg_match('#^(\w+)://#', $streamName, $match);
  90. if (((isset($match[1]) && $match[1] == 'file') || !isset($match[1])) &&
  91. !file_exists($streamName)) {
  92. throw new File\Exception\FileDoesNotExist(
  93. 'File %s does not exist.',
  94. 1,
  95. $streamName
  96. );
  97. }
  98. $out = parent::_open($streamName, $context);
  99. return $out;
  100. }
  101. /**
  102. * Write n characters.
  103. *
  104. * @param string $string String.
  105. * @param int $length Length.
  106. * @return mixed
  107. * @throws \Hoa\File\Exception
  108. */
  109. public function write($string, $length)
  110. {
  111. if (0 > $length) {
  112. throw new File\Exception(
  113. 'Length must be greater than 0, given %d.',
  114. 2,
  115. $length
  116. );
  117. }
  118. return fwrite($this->getStream(), $string, $length);
  119. }
  120. /**
  121. * Write a string.
  122. *
  123. * @param string $string String.
  124. * @return mixed
  125. */
  126. public function writeString($string)
  127. {
  128. $string = (string) $string;
  129. return $this->write($string, strlen($string));
  130. }
  131. /**
  132. * Write a character.
  133. *
  134. * @param string $char Character.
  135. * @return mixed
  136. */
  137. public function writeCharacter($char)
  138. {
  139. return $this->write((string) $char[0], 1);
  140. }
  141. /**
  142. * Write a boolean.
  143. *
  144. * @param bool $boolean Boolean.
  145. * @return mixed
  146. */
  147. public function writeBoolean($boolean)
  148. {
  149. return $this->write((string) (bool) $boolean, 1);
  150. }
  151. /**
  152. * Write an integer.
  153. *
  154. * @param int $integer Integer.
  155. * @return mixed
  156. */
  157. public function writeInteger($integer)
  158. {
  159. $integer = (string) (int) $integer;
  160. return $this->write($integer, strlen($integer));
  161. }
  162. /**
  163. * Write a float.
  164. *
  165. * @param float $float Float.
  166. * @return mixed
  167. */
  168. public function writeFloat($float)
  169. {
  170. $float = (string) (float) $float;
  171. return $this->write($float, strlen($float));
  172. }
  173. /**
  174. * Write an array.
  175. *
  176. * @param array $array Array.
  177. * @return mixed
  178. */
  179. public function writeArray(array $array)
  180. {
  181. $array = var_export($array, true);
  182. return $this->write($array, strlen($array));
  183. }
  184. /**
  185. * Write a line.
  186. *
  187. * @param string $line Line.
  188. * @return mixed
  189. */
  190. public function writeLine($line)
  191. {
  192. if (false === $n = strpos($line, "\n")) {
  193. return $this->write($line . "\n", strlen($line) + 1);
  194. }
  195. ++$n;
  196. return $this->write(substr($line, 0, $n), $n);
  197. }
  198. /**
  199. * Write all, i.e. as much as possible.
  200. *
  201. * @param string $string String.
  202. * @return mixed
  203. */
  204. public function writeAll($string)
  205. {
  206. return $this->write($string, strlen($string));
  207. }
  208. /**
  209. * Truncate a file to a given length.
  210. *
  211. * @param int $size Size.
  212. * @return bool
  213. */
  214. public function truncate($size)
  215. {
  216. return ftruncate($this->getStream(), $size);
  217. }
  218. }