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 | 168 | public function __construct($filePath) |
|
44 | |||
45 | /** |
||
46 | * Get a new File object for another file relative to this file. |
||
47 | * |
||
48 | * @param string $path |
||
49 | * |
||
50 | * @return File |
||
51 | */ |
||
52 | 11 | public function createFileForRelativePath($path) |
|
56 | |||
57 | /** |
||
58 | * Whether or not this file exists on the filesystem. |
||
59 | * |
||
60 | * @return bool |
||
61 | */ |
||
62 | 160 | public function exists() |
|
66 | |||
67 | /** |
||
68 | * Get the name of the file without an extension. |
||
69 | * |
||
70 | * @param null $suffix This value will be discarded and is only needed to be able to override the \SplFileInfo |
||
71 | * definition. |
||
72 | * |
||
73 | * @since 0.2.0 |
||
74 | * |
||
75 | * @return string |
||
76 | */ |
||
77 | 5 | public function getBasename($suffix = null) |
|
81 | |||
82 | /** |
||
83 | * Get the name of the with the extension. |
||
84 | * |
||
85 | * @since 0.2.0 |
||
86 | * |
||
87 | * @return string |
||
88 | */ |
||
89 | 1 | public function getFilename() |
|
93 | |||
94 | /** |
||
95 | * Get the absolute path to this file. |
||
96 | * |
||
97 | * @since 0.2.0 |
||
98 | * |
||
99 | * @return string |
||
100 | */ |
||
101 | 168 | public function getAbsolutePath() |
|
105 | |||
106 | /** |
||
107 | * Get the path to the parent folder of this file. |
||
108 | * |
||
109 | * @since 0.2.0 |
||
110 | * |
||
111 | * @return string |
||
112 | */ |
||
113 | 1 | public function getParentFolder() |
|
117 | |||
118 | /** |
||
119 | * Get the file path to this file, relative to where it was created; likely the current working directory. |
||
120 | * |
||
121 | * @since 0.2.0 |
||
122 | * |
||
123 | * @return string |
||
124 | */ |
||
125 | 138 | public function getRelativeFilePath() |
|
129 | |||
130 | /** |
||
131 | * Get the path to the parent folder this file, relative to where it was created; likely the current working directory. |
||
132 | * |
||
133 | * @since 0.2.0 |
||
134 | * |
||
135 | * @return string |
||
136 | */ |
||
137 | 2 | public function getRelativeParentFolder() |
|
141 | |||
142 | /** |
||
143 | * Get the contents of this file. |
||
144 | * |
||
145 | * @since 0.2.0 |
||
146 | * |
||
147 | * @throws \RuntimeException When the file could not be read. |
||
148 | * |
||
149 | * @return string |
||
150 | */ |
||
151 | 158 | public function getContents() |
|
152 | { |
||
153 | 158 | $this->isSafeToRead(); |
|
154 | |||
155 | 157 | $content = file_get_contents($this->getAbsolutePath()); |
|
156 | |||
157 | 157 | if ($content === false) |
|
158 | 157 | { |
|
159 | $error = error_get_last(); |
||
160 | throw new \RuntimeException($error['message']); |
||
161 | } |
||
162 | |||
163 | 157 | return $content; |
|
164 | } |
||
165 | |||
166 | /** |
||
167 | * Check if a file is safe to read. |
||
168 | */ |
||
169 | 158 | private function isSafeToRead() |
|
170 | { |
||
171 | 158 | $e = new FileNotFoundException( |
|
172 | 158 | sprintf('The given path "%s" does not exist or is outside the website working directory', $this->rawPath), |
|
173 | 158 | 0, |
|
174 | 158 | null, |
|
175 | 158 | $this->rawPath |
|
176 | 158 | ); |
|
177 | |||
178 | 158 | if (!$this->exists()) |
|
179 | 158 | { |
|
180 | 2 | throw $e; |
|
181 | } |
||
182 | |||
183 | 157 | if (self::isVFS($this->getAbsolutePath())) |
|
184 | 157 | { |
|
185 | 119 | return; |
|
186 | } |
||
187 | |||
188 | 43 | if (strpos($this->getAbsolutePath(), Service::getWorkingDirectory()) !== 0) |
|
189 | 43 | { |
|
190 | throw $e; |
||
191 | } |
||
192 | 43 | } |
|
193 | |||
194 | /** |
||
195 | * A vfsStream friendly way of getting the realpath() of something. |
||
196 | * |
||
197 | * @param string $path |
||
198 | * |
||
199 | * @return string |
||
200 | */ |
||
201 | 168 | public static function realpath($path) |
|
205 | |||
206 | /** |
||
207 | * Check whether a given path is on the virtual filesystem. |
||
208 | * |
||
209 | * @param string $path |
||
210 | * |
||
211 | * @return bool |
||
212 | */ |
||
213 | 168 | private static function isVFS($path) |
|
217 | } |
||
218 |
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.