ReadWrite.php 9.1 KB

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