ReadWrite.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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;
  36. use Hoa\Stream;
  37. /**
  38. * Class \Hoa\File\ReadWrite.
  39. *
  40. * File handler.
  41. *
  42. * @copyright Copyright © 2007-2017 Hoa community
  43. * @license New BSD License
  44. */
  45. class ReadWrite
  46. extends File
  47. implements Stream\IStream\In,
  48. Stream\IStream\Out
  49. {
  50. /**
  51. * Open a file.
  52. *
  53. * @param string $streamName Stream name.
  54. * @param string $mode Open mode, see the self::MODE_* constants.
  55. * @param string $context Context ID (please, see the
  56. * \Hoa\Stream\Context class).
  57. * @param bool $wait Differ opening or not.
  58. */
  59. public function __construct(
  60. $streamName,
  61. $mode = parent::MODE_APPEND_READ_WRITE,
  62. $context = null,
  63. $wait = false
  64. ) {
  65. parent::__construct($streamName, $mode, $context, $wait);
  66. return;
  67. }
  68. /**
  69. * Open the stream and return the associated resource.
  70. *
  71. * @param string $streamName Stream name (e.g. path or URL).
  72. * @param \Hoa\Stream\Context $context Context.
  73. * @return resource
  74. * @throws \Hoa\File\Exception\FileDoesNotExist
  75. * @throws \Hoa\File\Exception
  76. */
  77. protected function &_open($streamName, Stream\Context $context = null)
  78. {
  79. static $createModes = [
  80. parent::MODE_READ_WRITE,
  81. parent::MODE_TRUNCATE_READ_WRITE,
  82. parent::MODE_APPEND_READ_WRITE,
  83. parent::MODE_CREATE_READ_WRITE
  84. ];
  85. if (!in_array($this->getMode(), $createModes)) {
  86. throw new Exception(
  87. 'Open mode are not supported; given %d. Only %s are supported.',
  88. 0,
  89. [$this->getMode(), implode(', ', $createModes)]
  90. );
  91. }
  92. preg_match('#^(\w+)://#', $streamName, $match);
  93. if (((isset($match[1]) && $match[1] == 'file') || !isset($match[1])) &&
  94. !file_exists($streamName) &&
  95. parent::MODE_READ_WRITE == $this->getMode()) {
  96. throw new Exception\FileDoesNotExist(
  97. 'File %s does not exist.',
  98. 1,
  99. $streamName
  100. );
  101. }
  102. $out = parent::_open($streamName, $context);
  103. return $out;
  104. }
  105. /**
  106. * Test for end-of-file.
  107. *
  108. * @return bool
  109. */
  110. public function eof()
  111. {
  112. return feof($this->getStream());
  113. }
  114. /**
  115. * Read n characters.
  116. *
  117. * @param int $length Length.
  118. * @return string
  119. * @throws \Hoa\File\Exception
  120. */
  121. public function read($length)
  122. {
  123. if (0 > $length) {
  124. throw new Exception(
  125. 'Length must be greater than 0, given %d.',
  126. 2,
  127. $length
  128. );
  129. }
  130. return fread($this->getStream(), $length);
  131. }
  132. /**
  133. * Alias of $this->read().
  134. *
  135. * @param int $length Length.
  136. * @return string
  137. */
  138. public function readString($length)
  139. {
  140. return $this->read($length);
  141. }
  142. /**
  143. * Read a character.
  144. *
  145. * @return string
  146. */
  147. public function readCharacter()
  148. {
  149. return fgetc($this->getStream());
  150. }
  151. /**
  152. * Read a boolean.
  153. *
  154. * @return bool
  155. */
  156. public function readBoolean()
  157. {
  158. return (bool) $this->read(1);
  159. }
  160. /**
  161. * Read an integer.
  162. *
  163. * @param int $length Length.
  164. * @return int
  165. */
  166. public function readInteger($length = 1)
  167. {
  168. return (int) $this->read($length);
  169. }
  170. /**
  171. * Read a float.
  172. *
  173. * @param int $length Length.
  174. * @return float
  175. */
  176. public function readFloat($length = 1)
  177. {
  178. return (float) $this->read($length);
  179. }
  180. /**
  181. * Read an array.
  182. * Alias of the $this->scanf() method.
  183. *
  184. * @param string $format Format (see printf's formats).
  185. * @return array
  186. */
  187. public function readArray($format = null)
  188. {
  189. return $this->scanf($format);
  190. }
  191. /**
  192. * Read a line.
  193. *
  194. * @return string
  195. */
  196. public function readLine()
  197. {
  198. return fgets($this->getStream());
  199. }
  200. /**
  201. * Read all, i.e. read as much as possible.
  202. *
  203. * @param int $offset Offset.
  204. * @return string
  205. */
  206. public function readAll($offset = 0)
  207. {
  208. return stream_get_contents($this->getStream(), -1, $offset);
  209. }
  210. /**
  211. * Parse input from a stream according to a format.
  212. *
  213. * @param string $format Format (see printf's formats).
  214. * @return array
  215. */
  216. public function scanf($format)
  217. {
  218. return fscanf($this->getStream(), $format);
  219. }
  220. /**
  221. * Write n characters.
  222. *
  223. * @param string $string String.
  224. * @param int $length Length.
  225. * @return mixed
  226. * @throws \Hoa\File\Exception
  227. */
  228. public function write($string, $length)
  229. {
  230. if (0 > $length) {
  231. throw new Exception(
  232. 'Length must be greater than 0, given %d.',
  233. 3,
  234. $length
  235. );
  236. }
  237. return fwrite($this->getStream(), $string, $length);
  238. }
  239. /**
  240. * Write a string.
  241. *
  242. * @param string $string String.
  243. * @return mixed
  244. */
  245. public function writeString($string)
  246. {
  247. $string = (string) $string;
  248. return $this->write($string, strlen($string));
  249. }
  250. /**
  251. * Write a character.
  252. *
  253. * @param string $char Character.
  254. * @return mixed
  255. */
  256. public function writeCharacter($char)
  257. {
  258. return $this->write((string) $char[0], 1);
  259. }
  260. /**
  261. * Write a boolean.
  262. *
  263. * @param bool $boolean Boolean.
  264. * @return mixed
  265. */
  266. public function writeBoolean($boolean)
  267. {
  268. return $this->write((string) (bool) $boolean, 1);
  269. }
  270. /**
  271. * Write an integer.
  272. *
  273. * @param int $integer Integer.
  274. * @return mixed
  275. */
  276. public function writeInteger($integer)
  277. {
  278. $integer = (string) (int) $integer;
  279. return $this->write($integer, strlen($integer));
  280. }
  281. /**
  282. * Write a float.
  283. *
  284. * @param float $float Float.
  285. * @return mixed
  286. */
  287. public function writeFloat($float)
  288. {
  289. $float = (string) (float) $float;
  290. return $this->write($float, strlen($float));
  291. }
  292. /**
  293. * Write an array.
  294. *
  295. * @param array $array Array.
  296. * @return mixed
  297. */
  298. public function writeArray(array $array)
  299. {
  300. $array = var_export($array, true);
  301. return $this->write($array, strlen($array));
  302. }
  303. /**
  304. * Write a line.
  305. *
  306. * @param string $line Line.
  307. * @return mixed
  308. */
  309. public function writeLine($line)
  310. {
  311. if (false === $n = strpos($line, "\n")) {
  312. return $this->write($line . "\n", strlen($line) + 1);
  313. }
  314. ++$n;
  315. return $this->write(substr($line, 0, $n), $n);
  316. }
  317. /**
  318. * Write all, i.e. as much as possible.
  319. *
  320. * @param string $string String.
  321. * @return mixed
  322. */
  323. public function writeAll($string)
  324. {
  325. return $this->write($string, strlen($string));
  326. }
  327. /**
  328. * Truncate a file to a given length.
  329. *
  330. * @param int $size Size.
  331. * @return bool
  332. */
  333. public function truncate($size)
  334. {
  335. return ftruncate($this->getStream(), $size);
  336. }
  337. }