Write.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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\Link;
  36. use Hoa\File;
  37. use Hoa\Stream;
  38. /**
  39. * Class \Hoa\File\Link\Write.
  40. *
  41. * File handler.
  42. *
  43. * @license New BSD License
  44. */
  45. class Write extends Link implements Stream\IStream\Out
  46. {
  47. /**
  48. * Open a file.
  49. *
  50. * @param string $streamName Stream name.
  51. * @param string $mode Open mode, see the parent::MODE_* constants.
  52. * @param string $context Context ID (please, see the
  53. * \Hoa\Stream\Context class).
  54. * @param bool $wait Differ opening or not.
  55. */
  56. public function __construct(
  57. $streamName,
  58. $mode = parent::MODE_APPEND_WRITE,
  59. $context = null,
  60. $wait = false
  61. ) {
  62. parent::__construct($streamName, $mode, $context, $wait);
  63. return;
  64. }
  65. /**
  66. * Open the stream and return the associated resource.
  67. *
  68. * @param string $streamName Stream name (e.g. path or URL).
  69. * @param \Hoa\Stream\Context $context Context.
  70. * @return resource
  71. * @throws \Hoa\File\Exception\FileDoesNotExist
  72. * @throws \Hoa\File\Exception
  73. */
  74. protected function &_open($streamName, Stream\Context $context = null)
  75. {
  76. static $createModes = [
  77. parent::MODE_TRUNCATE_WRITE,
  78. parent::MODE_APPEND_WRITE,
  79. parent::MODE_CREATE_WRITE
  80. ];
  81. if (!in_array($this->getMode(), $createModes)) {
  82. throw new File\Exception(
  83. 'Open mode are not supported; given %d. Only %s are supported.',
  84. 0,
  85. [$this->getMode(), implode(', ', $createModes)]
  86. );
  87. }
  88. preg_match('#^(\w+)://#', $streamName, $match);
  89. if (((isset($match[1]) && $match[1] == 'file') || !isset($match[1])) &&
  90. !file_exists($streamName)) {
  91. throw new File\Exception\FileDoesNotExist(
  92. 'File %s does not exist.',
  93. 1,
  94. $streamName
  95. );
  96. }
  97. $out = parent::_open($streamName, $context);
  98. return $out;
  99. }
  100. /**
  101. * Write n characters.
  102. *
  103. * @param string $string String.
  104. * @param int $length Length.
  105. * @return mixed
  106. * @throws \Hoa\File\Exception
  107. */
  108. public function write($string, $length)
  109. {
  110. if (0 > $length) {
  111. throw new File\Exception(
  112. 'Length must be greater than 0, given %d.',
  113. 2,
  114. $length
  115. );
  116. }
  117. return fwrite($this->getStream(), $string, $length);
  118. }
  119. /**
  120. * Write a string.
  121. *
  122. * @param string $string String.
  123. * @return mixed
  124. */
  125. public function writeString($string)
  126. {
  127. $string = (string) $string;
  128. return $this->write($string, strlen($string));
  129. }
  130. /**
  131. * Write a character.
  132. *
  133. * @param string $char Character.
  134. * @return mixed
  135. */
  136. public function writeCharacter($char)
  137. {
  138. return $this->write((string) $char[0], 1);
  139. }
  140. /**
  141. * Write a boolean.
  142. *
  143. * @param bool $boolean Boolean.
  144. * @return mixed
  145. */
  146. public function writeBoolean($boolean)
  147. {
  148. return $this->write((string) (bool) $boolean, 1);
  149. }
  150. /**
  151. * Write an integer.
  152. *
  153. * @param int $integer Integer.
  154. * @return mixed
  155. */
  156. public function writeInteger($integer)
  157. {
  158. $integer = (string) (int) $integer;
  159. return $this->write($integer, strlen($integer));
  160. }
  161. /**
  162. * Write a float.
  163. *
  164. * @param float $float Float.
  165. * @return mixed
  166. */
  167. public function writeFloat($float)
  168. {
  169. $float = (string) (float) $float;
  170. return $this->write($float, strlen($float));
  171. }
  172. /**
  173. * Write an array.
  174. *
  175. * @param array $array Array.
  176. * @return mixed
  177. */
  178. public function writeArray(array $array)
  179. {
  180. $array = var_export($array, true);
  181. return $this->write($array, strlen($array));
  182. }
  183. /**
  184. * Write a line.
  185. *
  186. * @param string $line Line.
  187. * @return mixed
  188. */
  189. public function writeLine($line)
  190. {
  191. if (false === $n = strpos($line, "\n")) {
  192. return $this->write($line . "\n", strlen($line) + 1);
  193. }
  194. ++$n;
  195. return $this->write(substr($line, 0, $n), $n);
  196. }
  197. /**
  198. * Write all, i.e. as much as possible.
  199. *
  200. * @param string $string String.
  201. * @return mixed
  202. */
  203. public function writeAll($string)
  204. {
  205. return $this->write($string, strlen($string));
  206. }
  207. /**
  208. * Truncate a file to a given length.
  209. *
  210. * @param int $size Size.
  211. * @return bool
  212. */
  213. public function truncate($size)
  214. {
  215. return ftruncate($this->getStream(), $size);
  216. }
  217. }