1 | <?php |
||
14 | class Locator |
||
15 | { |
||
16 | /** |
||
17 | * Location of the local repository of cached files |
||
18 | * @var string |
||
19 | */ |
||
20 | protected $repository; |
||
21 | |||
22 | /** |
||
23 | * Seconds to timeout when a file is required |
||
24 | * @var int |
||
25 | */ |
||
26 | protected $timeout; |
||
27 | |||
28 | /** |
||
29 | * Seconds to know if a cached file is expired |
||
30 | * @var int |
||
31 | */ |
||
32 | protected $expire; |
||
33 | |||
34 | /** |
||
35 | * Registered urls and file location |
||
36 | * @var array |
||
37 | */ |
||
38 | protected $register = []; |
||
39 | |||
40 | /** |
||
41 | * @var FileMimeChecker |
||
42 | */ |
||
43 | protected $mimeChecker; |
||
44 | |||
45 | /** @var DownloaderInterface */ |
||
46 | protected $downloader; |
||
47 | |||
48 | /** |
||
49 | * @param string $repository Location for place to store the cached files |
||
50 | * @param int $timeout Seconds to timeout when get a file when download is needed |
||
51 | * @param int $expire Seconds to wait to expire cache, a value of 0 means never expires |
||
52 | * @param DownloaderInterface $downloader Downloader object, if null a Downloader\PhpDownloader will be used. |
||
53 | */ |
||
54 | public function __construct($repository = '', $timeout = 20, $expire = 0, DownloaderInterface $downloader = null) |
||
65 | |||
66 | /** |
||
67 | * Location of the local repository of cached files |
||
68 | * @return string |
||
69 | */ |
||
70 | public function getRepository() |
||
74 | |||
75 | /** |
||
76 | * Seconds to timeout when a file is required |
||
77 | * @return int |
||
78 | */ |
||
79 | public function getTimeout() |
||
83 | |||
84 | /** |
||
85 | * Seconds to know if a cached file is expired |
||
86 | * @return int |
||
87 | */ |
||
88 | public function getExpire() |
||
92 | |||
93 | /** |
||
94 | * @return DownloaderInterface |
||
95 | */ |
||
96 | public function getDownloader() |
||
100 | |||
101 | /** |
||
102 | * Return a filename for a given URL based on the registry. |
||
103 | * If the file is not registered then it is downloaded and stored in the repository location |
||
104 | * @param string $url |
||
105 | * @return string |
||
106 | */ |
||
107 | public function get($url) |
||
119 | |||
120 | /** |
||
121 | * Build a unique name for a url including the repository |
||
122 | * @param string $url |
||
123 | * @return string |
||
124 | */ |
||
125 | public function cacheFileName($url) |
||
130 | |||
131 | /** |
||
132 | * Register a url with a file without download it. However the file must exists and be readable. |
||
133 | * @param string $url |
||
134 | * @param string $filename |
||
135 | */ |
||
136 | public function register($url, $filename) |
||
148 | |||
149 | /** |
||
150 | * Unregister a url from the cache |
||
151 | * @param string $url |
||
152 | */ |
||
153 | public function unregister($url) |
||
157 | |||
158 | /** |
||
159 | * Return a copy of the registry |
||
160 | * @return array |
||
161 | */ |
||
162 | public function registry() |
||
166 | |||
167 | /** |
||
168 | * Return if a given url exists in the registry |
||
169 | * @param string $url |
||
170 | * @return bool |
||
171 | */ |
||
172 | public function registered($url) |
||
176 | |||
177 | /** |
||
178 | * Return the filename of an url, of needs to download a new copy then |
||
179 | * it try to download, validate the mime of the downloaded file and place the file on the repository |
||
180 | * @param string $url |
||
181 | * @return string |
||
182 | */ |
||
183 | protected function cache($url) |
||
206 | |||
207 | /** |
||
208 | * append a mime to the list of mimes allowed |
||
209 | * @param string $mime |
||
210 | */ |
||
211 | public function mimeAllow($mime) |
||
215 | |||
216 | /** |
||
217 | * Remove a mime to the list of mimes allowed |
||
218 | * NOTE: This method does not affect previously registered urls |
||
219 | * @param string $mime |
||
220 | */ |
||
221 | public function mimeDisallow($mime) |
||
225 | |||
226 | /** |
||
227 | * return the list of allowed mimes |
||
228 | * @return bool |
||
229 | */ |
||
230 | public function mimeList() |
||
234 | |||
235 | /** |
||
236 | * check if a the mime of a file is allowed |
||
237 | * |
||
238 | * @param string $filename path to the file |
||
239 | * @return bool |
||
240 | */ |
||
241 | public function mimeIsAllowed($filename) |
||
245 | |||
246 | /** |
||
247 | * Internal function to assert if URL is valid, if not throw an exception |
||
248 | * @param string $url |
||
249 | * @throws \RuntimeException |
||
250 | */ |
||
251 | private function assertUrlIsValid($url) |
||
260 | |||
261 | /** |
||
262 | * Rules to determine if the file needs to be downloaded: |
||
263 | * 1. file does not exists |
||
264 | * 2. files do not expire |
||
265 | * 3. file is expired |
||
266 | * @param string $filename |
||
267 | * @return bool |
||
268 | */ |
||
269 | protected function needToDownload($filename) |
||
287 | } |
||
288 |