1 : <?php
2 : /**
3 : * File container.
4 : *
5 : * @package bovigo_vfs
6 : * @version $Id: vfsStreamFile.php 211 2010-10-06 16:33:05Z google@frankkleine.de $
7 : */
8 : /**
9 : * @ignore
10 : */
11 : require_once dirname(__FILE__) . '/vfsStreamAbstractContent.php';
12 : /**
13 : * File container.
14 : *
15 : * @package bovigo_vfs
16 : */
17 : class vfsStreamFile extends vfsStreamAbstractContent
18 : {
19 : /**
20 : * the real content of the file
21 : *
22 : * @var string
23 : */
24 : protected $content;
25 : /**
26 : * amount of read bytes
27 : *
28 : * @var int
29 : */
30 : protected $bytes_read = 0;
31 :
32 : /**
33 : * constructor
34 : *
35 : * @param string $name
36 : * @param int $permissions optional
37 : */
38 : public function __construct($name, $permissions = null)
39 : {
40 100 : $this->type = vfsStreamContent::TYPE_FILE;
41 100 : parent::__construct($name, $permissions);
42 100 : }
43 :
44 : /**
45 : * returns default permissions for concrete implementation
46 : *
47 : * @return int
48 : * @since 0.8.0
49 : */
50 : protected function getDefaultPermissions()
51 : {
52 99 : return 0666;
53 : }
54 :
55 : /**
56 : * checks whether the container can be applied to given name
57 : *
58 : * @param string $name
59 : * @return bool
60 : */
61 : public function appliesTo($name)
62 : {
63 34 : return ($name === $this->name);
64 : }
65 :
66 : /**
67 : * alias for withContent()
68 : *
69 : * @param string $content
70 : * @return vfsStreamFile
71 : * @see withContent()
72 : */
73 : public function setContent($content)
74 : {
75 8 : return $this->withContent($content);
76 : }
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 : * @see setContent()
87 : */
88 : public function withContent($content)
89 : {
90 85 : $this->content = $content;
91 85 : return $this;
92 : }
93 :
94 : /**
95 : * returns the contents of the file
96 : *
97 : * @return string
98 : */
99 : public function getContent()
100 : {
101 4 : return $this->content;
102 : }
103 :
104 : /**
105 : * reads the given amount of bytes from content
106 : *
107 : * @param int $count
108 : * @return string
109 : */
110 : public function read($count)
111 : {
112 16 : $data = substr($this->content, $this->bytes_read, $count);
113 16 : $this->bytes_read += $count;
114 16 : return $data;
115 : }
116 :
117 : /**
118 : * returns the content until its end from current offset
119 : *
120 : * @return string
121 : */
122 : public function readUntilEnd()
123 : {
124 1 : return substr($this->content, $this->bytes_read);
125 : }
126 :
127 : /**
128 : * writes an amount of data
129 : *
130 : * Using this method changes the time when the file was last modified.
131 : *
132 : * @param string $data
133 : * @return amount of written bytes
134 : */
135 : public function write($data)
136 : {
137 14 : $dataLen = strlen($data);
138 14 : $this->content = substr($this->content, 0, $this->bytes_read) . $data . substr($this->content, $this->bytes_read + $dataLen);
139 14 : $this->bytes_read += $dataLen;
140 14 : $this->filemtime = time();
141 14 : return $dataLen;
142 : }
143 :
144 : /**
145 : * checks whether pointer is at end of file
146 : *
147 : * @return bool
148 : */
149 : public function eof()
150 : {
151 17 : return $this->bytes_read >= strlen($this->content);
152 : }
153 :
154 : /**
155 : * returns the current position within the file
156 : *
157 : * @return int
158 : */
159 : public function getBytesRead()
160 : {
161 9 : return $this->bytes_read;
162 : }
163 :
164 : /**
165 : * seeks to the given offset
166 : *
167 : * @param int $offset
168 : * @param int $whence
169 : * @return bool
170 : */
171 : public function seek($offset, $whence)
172 : {
173 : switch ($whence) {
174 20 : case SEEK_CUR:
175 4 : $this->bytes_read += $offset;
176 4 : return true;
177 :
178 20 : case SEEK_END:
179 5 : $this->bytes_read = strlen($this->content) + $offset;
180 5 : return true;
181 :
182 20 : case SEEK_SET:
183 20 : $this->bytes_read = $offset;
184 20 : return true;
185 :
186 2 : default:
187 2 : return false;
188 2 : }
189 :
190 : return false;
191 : }
192 :
193 : /**
194 : * returns size of content
195 : *
196 : * @return int
197 : */
198 : public function size()
199 : {
200 37 : return strlen($this->content);
201 : }
202 : }
|