PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/org/bovigo/vfs/vfsStreamFile.php

https://bitbucket.org/ajalovec/aura.demo
PHP | 327 lines | 132 code | 28 blank | 167 comment | 6 complexity | 50d9cf7cbcdab831619e916193439099 MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of vfsStream.
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. *
  8. * @package org\bovigo\vfs
  9. */
  10. namespace org\bovigo\vfs;
  11. /**
  12. * File container.
  13. *
  14. * @api
  15. */
  16. class vfsStreamFile extends vfsStreamAbstractContent
  17. {
  18. /**
  19. * the real content of the file
  20. *
  21. * @type string
  22. */
  23. protected $content;
  24. /**
  25. * amount of read bytes
  26. *
  27. * @type int
  28. */
  29. protected $bytes_read = 0;
  30. /**
  31. * current lock status of file
  32. *
  33. * @type int
  34. */
  35. protected $lock = LOCK_UN;
  36. /**
  37. * constructor
  38. *
  39. * @param string $name
  40. * @param int $permissions optional
  41. */
  42. public function __construct($name, $permissions = null)
  43. {
  44. $this->type = vfsStreamContent::TYPE_FILE;
  45. parent::__construct($name, $permissions);
  46. }
  47. /**
  48. * returns default permissions for concrete implementation
  49. *
  50. * @return int
  51. * @since 0.8.0
  52. */
  53. protected function getDefaultPermissions()
  54. {
  55. return 0666;
  56. }
  57. /**
  58. * checks whether the container can be applied to given name
  59. *
  60. * @param string $name
  61. * @return bool
  62. */
  63. public function appliesTo($name)
  64. {
  65. return ($name === $this->name);
  66. }
  67. /**
  68. * alias for withContent()
  69. *
  70. * @param string $content
  71. * @return vfsStreamFile
  72. * @see withContent()
  73. */
  74. public function setContent($content)
  75. {
  76. return $this->withContent($content);
  77. }
  78. /**
  79. * sets the contents of the file
  80. *
  81. * Setting content with this method does not change the time when the file
  82. * was last modified.
  83. *
  84. * @param string $content
  85. * @return vfsStreamFile
  86. */
  87. public function withContent($content)
  88. {
  89. $this->content = $content;
  90. return $this;
  91. }
  92. /**
  93. * returns the contents of the file
  94. *
  95. * Getting content does not change the time when the file
  96. * was last accessed.
  97. *
  98. * @return string
  99. */
  100. public function getContent()
  101. {
  102. return $this->content;
  103. }
  104. /**
  105. * simply open the file
  106. *
  107. * @since 0.9
  108. */
  109. public function open()
  110. {
  111. $this->seek(0, SEEK_SET);
  112. $this->lastAccessed = time();
  113. }
  114. /**
  115. * open file and set pointer to end of file
  116. *
  117. * @since 0.9
  118. */
  119. public function openForAppend()
  120. {
  121. $this->seek(0, SEEK_END);
  122. $this->lastAccessed = time();
  123. }
  124. /**
  125. * open file and truncate content
  126. *
  127. * @since 0.9
  128. */
  129. public function openWithTruncate()
  130. {
  131. $this->open();
  132. $this->content = '';
  133. $time = time();
  134. $this->lastAccessed = $time;
  135. $this->lastModified = $time;
  136. }
  137. /**
  138. * reads the given amount of bytes from content
  139. *
  140. * Using this method changes the time when the file was last accessed.
  141. *
  142. * @param int $count
  143. * @return string
  144. */
  145. public function read($count)
  146. {
  147. $data = substr($this->content, $this->bytes_read, $count);
  148. $this->bytes_read += $count;
  149. $this->lastAccessed = time();
  150. return $data;
  151. }
  152. /**
  153. * returns the content until its end from current offset
  154. *
  155. * Using this method changes the time when the file was last accessed.
  156. *
  157. * @return string
  158. */
  159. public function readUntilEnd()
  160. {
  161. $this->lastAccessed = time();
  162. return substr($this->content, $this->bytes_read);
  163. }
  164. /**
  165. * writes an amount of data
  166. *
  167. * Using this method changes the time when the file was last modified.
  168. *
  169. * @param string $data
  170. * @return amount of written bytes
  171. */
  172. public function write($data)
  173. {
  174. $dataLen = strlen($data);
  175. $this->content = substr($this->content, 0, $this->bytes_read) . $data . substr($this->content, $this->bytes_read + $dataLen);
  176. $this->bytes_read += $dataLen;
  177. $this->lastModified = time();
  178. return $dataLen;
  179. }
  180. /**
  181. * Truncates a file to a given length
  182. *
  183. * @param int $size length to truncate file to
  184. * @return bool
  185. * @since 1.1.0
  186. */
  187. public function truncate($size) {
  188. if ($size > $this->size()) {
  189. // Pad with null-chars if we're "truncating up"
  190. $this->setContent($this->getContent() . str_repeat("\0", $size - $this->size()));
  191. } else {
  192. $this->setContent(substr($this->getContent(), 0, $size));
  193. }
  194. $this->lastModified = time();
  195. return true;
  196. }
  197. /**
  198. * checks whether pointer is at end of file
  199. *
  200. * @return bool
  201. */
  202. public function eof()
  203. {
  204. return $this->bytes_read >= strlen($this->content);
  205. }
  206. /**
  207. * returns the current position within the file
  208. *
  209. * @return int
  210. */
  211. public function getBytesRead()
  212. {
  213. return $this->bytes_read;
  214. }
  215. /**
  216. * seeks to the given offset
  217. *
  218. * @param int $offset
  219. * @param int $whence
  220. * @return bool
  221. */
  222. public function seek($offset, $whence)
  223. {
  224. switch ($whence) {
  225. case SEEK_CUR:
  226. $this->bytes_read += $offset;
  227. return true;
  228. case SEEK_END:
  229. $this->bytes_read = strlen($this->content) + $offset;
  230. return true;
  231. case SEEK_SET:
  232. $this->bytes_read = $offset;
  233. return true;
  234. default:
  235. return false;
  236. }
  237. return false;
  238. }
  239. /**
  240. * returns size of content
  241. *
  242. * @return int
  243. */
  244. public function size()
  245. {
  246. return strlen($this->content);
  247. }
  248. /**
  249. * locks file for
  250. *
  251. * @param int $operation
  252. * @return vfsStreamFile
  253. * @since 0.10.0
  254. * @see https://github.com/mikey179/vfsStream/issues/6
  255. */
  256. public function lock($operation)
  257. {
  258. if ((LOCK_NB & $operation) == LOCK_NB) {
  259. $this->lock = $operation - LOCK_NB;
  260. } else {
  261. $this->lock = $operation;
  262. }
  263. return $this;
  264. }
  265. /**
  266. * checks whether file is locked
  267. *
  268. * @return bool
  269. * @since 0.10.0
  270. * @see https://github.com/mikey179/vfsStream/issues/6
  271. */
  272. public function isLocked()
  273. {
  274. return (LOCK_UN !== $this->lock);
  275. }
  276. /**
  277. * checks whether file is locked in shared mode
  278. *
  279. * @return bool
  280. * @since 0.10.0
  281. * @see https://github.com/mikey179/vfsStream/issues/6
  282. */
  283. public function hasSharedLock()
  284. {
  285. return (LOCK_SH === $this->lock);
  286. }
  287. /**
  288. * checks whether file is locked in exclusive mode
  289. *
  290. * @return bool
  291. * @since 0.10.0
  292. * @see https://github.com/mikey179/vfsStream/issues/6
  293. */
  294. public function hasExclusiveLock()
  295. {
  296. return (LOCK_EX === $this->lock);
  297. }
  298. }
  299. ?>