1 | <?php |
||
21 | final class File extends \SplFileInfo |
||
22 | { |
||
23 | /** @var string The path relative to the site's working directory. */ |
||
24 | private $relativePath; |
||
25 | |||
26 | /** @var string The original raw path given to the constructor. */ |
||
27 | private $rawPath; |
||
28 | |||
29 | /** |
||
30 | * File Constructor. |
||
31 | * |
||
32 | * @param string $filePath an absolute file path or a path relative to the current working directory |
||
33 | * |
||
34 | * @since 0.2.0 |
||
35 | * |
||
36 | * @throws FileNotFoundException |
||
37 | */ |
||
38 | 167 | public function __construct($filePath) |
|
54 | |||
55 | /** |
||
56 | * Get a new File object for another file relative to this file. |
||
57 | * |
||
58 | * @param string $path |
||
59 | * |
||
60 | * @return File |
||
61 | */ |
||
62 | 11 | public function createFileForRelativePath($path) |
|
66 | |||
67 | /** |
||
68 | * Whether or not this file exists on the filesystem. |
||
69 | * |
||
70 | * @return bool |
||
71 | */ |
||
72 | 158 | public function exists() |
|
76 | |||
77 | /** |
||
78 | * Get the name of the file without an extension. |
||
79 | * |
||
80 | * @param null $suffix this value will be discarded and is only needed to be able to override the \SplFileInfo |
||
81 | * definition |
||
82 | * |
||
83 | * @since 0.2.0 |
||
84 | * |
||
85 | * @return string |
||
86 | */ |
||
87 | 97 | public function getBasename($suffix = null) |
|
91 | |||
92 | /** |
||
93 | * Get the name of the with the extension. |
||
94 | * |
||
95 | * @since 0.2.0 |
||
96 | * |
||
97 | * @return string |
||
98 | */ |
||
99 | 93 | public function getFilename() |
|
103 | |||
104 | /** |
||
105 | * Get the absolute path to this file. |
||
106 | * |
||
107 | * @since 0.2.0 |
||
108 | * |
||
109 | * @return string |
||
110 | */ |
||
111 | 165 | public function getAbsolutePath() |
|
115 | |||
116 | /** |
||
117 | * Get the path to the parent folder of this file. |
||
118 | * |
||
119 | * @since 0.2.0 |
||
120 | * |
||
121 | * @return string |
||
122 | */ |
||
123 | 1 | public function getParentFolder() |
|
127 | |||
128 | /** |
||
129 | * Get the file path to this file, relative to where it was created; likely the current working directory. |
||
130 | * |
||
131 | * @since 0.2.0 |
||
132 | * |
||
133 | * @return string |
||
134 | */ |
||
135 | 138 | public function getRelativeFilePath() |
|
139 | |||
140 | /** |
||
141 | * Get the path to the parent folder this file, relative to where it was created; likely the current working directory. |
||
142 | * |
||
143 | * @since 0.2.0 |
||
144 | * |
||
145 | * @return string |
||
146 | */ |
||
147 | 2 | public function getRelativeParentFolder() |
|
151 | |||
152 | /** |
||
153 | * Gets the last modified time. |
||
154 | * |
||
155 | * @return int The last modified time for the file, in a Unix timestamp |
||
156 | */ |
||
157 | public function getLastModified() |
||
161 | |||
162 | /** |
||
163 | * Get the contents of this file. |
||
164 | * |
||
165 | * @since 0.2.0 |
||
166 | * |
||
167 | * @throws \RuntimeException when the file could not be read |
||
168 | * |
||
169 | * @return string |
||
170 | */ |
||
171 | 157 | public function getContents() |
|
172 | { |
||
173 | 157 | if (!$this->exists()) |
|
174 | { |
||
175 | 1 | throw $this->buildNotFoundException(); |
|
176 | } |
||
177 | |||
178 | 157 | $content = file_get_contents($this->getAbsolutePath()); |
|
179 | |||
180 | 157 | if ($content === false) |
|
181 | { |
||
182 | $error = error_get_last(); |
||
183 | throw new \RuntimeException($error['message']); |
||
184 | } |
||
185 | |||
186 | 157 | return $content; |
|
187 | } |
||
188 | |||
189 | /** |
||
190 | * Check if a file is safe to read. |
||
191 | * |
||
192 | * @throws FileNotFoundException |
||
193 | */ |
||
194 | 165 | private function isSafeToRead() |
|
195 | { |
||
196 | 165 | if (self::isVFS($this->getAbsolutePath())) |
|
197 | { |
||
198 | 119 | return; |
|
199 | } |
||
200 | |||
201 | 51 | if (strpos($this->getAbsolutePath(), Service::getWorkingDirectory()) !== 0) |
|
202 | { |
||
203 | throw $this->buildNotFoundException(); |
||
204 | } |
||
205 | 51 | } |
|
206 | |||
207 | 4 | private function buildNotFoundException() |
|
208 | { |
||
209 | 4 | return new FileNotFoundException( |
|
210 | 4 | sprintf('The given path "%s" does not exist or is outside the website working directory', $this->rawPath), |
|
211 | 4 | 0, |
|
212 | 4 | null, |
|
213 | 4 | $this->rawPath |
|
214 | ); |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * A vfsStream friendly way of getting the realpath() of something. |
||
219 | * |
||
220 | * @param string $path |
||
221 | * |
||
222 | * @return string |
||
223 | */ |
||
224 | 167 | public static function realpath($path) |
|
228 | |||
229 | /** |
||
230 | * Check whether a given path is on the virtual filesystem. |
||
231 | * |
||
232 | * @param string $path |
||
233 | * |
||
234 | * @return bool |
||
235 | */ |
||
236 | 167 | private static function isVFS($path) |
|
240 | } |
||
241 |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.