diff options
author | Nicolas LÅ“uillet <nicolas.loeuillet@gmail.com> | 2013-08-03 19:26:54 +0200 |
---|---|---|
committer | Nicolas LÅ“uillet <nicolas.loeuillet@gmail.com> | 2013-08-03 19:26:54 +0200 |
commit | 4f5b44bd3bd490309eb2ba7b44df4769816ba729 (patch) | |
tree | 6cefe170dfe0a5a361cb1e2d1fc4d580a3316d02 /vendor/symfony/filesystem/Symfony/Component/Filesystem/Filesystem.php | |
parent | 2b840e0cfb63a453bea67a98541f3df9c273c5f5 (diff) | |
download | wallabag-4f5b44bd3bd490309eb2ba7b44df4769816ba729.tar.gz wallabag-4f5b44bd3bd490309eb2ba7b44df4769816ba729.tar.zst wallabag-4f5b44bd3bd490309eb2ba7b44df4769816ba729.zip |
twig implementation
Diffstat (limited to 'vendor/symfony/filesystem/Symfony/Component/Filesystem/Filesystem.php')
-rw-r--r-- | vendor/symfony/filesystem/Symfony/Component/Filesystem/Filesystem.php | 471 |
1 files changed, 471 insertions, 0 deletions
diff --git a/vendor/symfony/filesystem/Symfony/Component/Filesystem/Filesystem.php b/vendor/symfony/filesystem/Symfony/Component/Filesystem/Filesystem.php new file mode 100644 index 00000000..6e015b4b --- /dev/null +++ b/vendor/symfony/filesystem/Symfony/Component/Filesystem/Filesystem.php | |||
@@ -0,0 +1,471 @@ | |||
1 | <?php | ||
2 | |||
3 | /* | ||
4 | * This file is part of the Symfony package. | ||
5 | * | ||
6 | * (c) Fabien Potencier <fabien@symfony.com> | ||
7 | * | ||
8 | * For the full copyright and license information, please view the LICENSE | ||
9 | * file that was distributed with this source code. | ||
10 | */ | ||
11 | |||
12 | namespace Symfony\Component\Filesystem; | ||
13 | |||
14 | use Symfony\Component\Filesystem\Exception\IOException; | ||
15 | |||
16 | /** | ||
17 | * Provides basic utility to manipulate the file system. | ||
18 | * | ||
19 | * @author Fabien Potencier <fabien@symfony.com> | ||
20 | */ | ||
21 | class Filesystem | ||
22 | { | ||
23 | /** | ||
24 | * Copies a file. | ||
25 | * | ||
26 | * This method only copies the file if the origin file is newer than the target file. | ||
27 | * | ||
28 | * By default, if the target already exists, it is not overridden. | ||
29 | * | ||
30 | * @param string $originFile The original filename | ||
31 | * @param string $targetFile The target filename | ||
32 | * @param boolean $override Whether to override an existing file or not | ||
33 | * | ||
34 | * @throws IOException When copy fails | ||
35 | */ | ||
36 | public function copy($originFile, $targetFile, $override = false) | ||
37 | { | ||
38 | if (stream_is_local($originFile) && !is_file($originFile)) { | ||
39 | throw new IOException(sprintf('Failed to copy %s because file not exists', $originFile)); | ||
40 | } | ||
41 | |||
42 | $this->mkdir(dirname($targetFile)); | ||
43 | |||
44 | if (!$override && is_file($targetFile)) { | ||
45 | $doCopy = filemtime($originFile) > filemtime($targetFile); | ||
46 | } else { | ||
47 | $doCopy = true; | ||
48 | } | ||
49 | |||
50 | if ($doCopy) { | ||
51 | // https://bugs.php.net/bug.php?id=64634 | ||
52 | $source = fopen($originFile, 'r'); | ||
53 | $target = fopen($targetFile, 'w+'); | ||
54 | stream_copy_to_stream($source, $target); | ||
55 | fclose($source); | ||
56 | fclose($target); | ||
57 | unset($source, $target); | ||
58 | |||
59 | if (!is_file($targetFile)) { | ||
60 | throw new IOException(sprintf('Failed to copy %s to %s', $originFile, $targetFile)); | ||
61 | } | ||
62 | } | ||
63 | } | ||
64 | |||
65 | /** | ||
66 | * Creates a directory recursively. | ||
67 | * | ||
68 | * @param string|array|\Traversable $dirs The directory path | ||
69 | * @param integer $mode The directory mode | ||
70 | * | ||
71 | * @throws IOException On any directory creation failure | ||
72 | */ | ||
73 | public function mkdir($dirs, $mode = 0777) | ||
74 | { | ||
75 | foreach ($this->toIterator($dirs) as $dir) { | ||
76 | if (is_dir($dir)) { | ||
77 | continue; | ||
78 | } | ||
79 | |||
80 | if (true !== @mkdir($dir, $mode, true)) { | ||
81 | throw new IOException(sprintf('Failed to create %s', $dir)); | ||
82 | } | ||
83 | } | ||
84 | } | ||
85 | |||
86 | /** | ||
87 | * Checks the existence of files or directories. | ||
88 | * | ||
89 | * @param string|array|\Traversable $files A filename, an array of files, or a \Traversable instance to check | ||
90 | * | ||
91 | * @return Boolean true if the file exists, false otherwise | ||
92 | */ | ||
93 | public function exists($files) | ||
94 | { | ||
95 | foreach ($this->toIterator($files) as $file) { | ||
96 | if (!file_exists($file)) { | ||
97 | return false; | ||
98 | } | ||
99 | } | ||
100 | |||
101 | return true; | ||
102 | } | ||
103 | |||
104 | /** | ||
105 | * Sets access and modification time of file. | ||
106 | * | ||
107 | * @param string|array|\Traversable $files A filename, an array of files, or a \Traversable instance to create | ||
108 | * @param integer $time The touch time as a unix timestamp | ||
109 | * @param integer $atime The access time as a unix timestamp | ||
110 | * | ||
111 | * @throws IOException When touch fails | ||
112 | */ | ||
113 | public function touch($files, $time = null, $atime = null) | ||
114 | { | ||
115 | foreach ($this->toIterator($files) as $file) { | ||
116 | $touch = $time ? @touch($file, $time, $atime) : @touch($file); | ||
117 | if (true !== $touch) { | ||
118 | throw new IOException(sprintf('Failed to touch %s', $file)); | ||
119 | } | ||
120 | } | ||
121 | } | ||
122 | |||
123 | /** | ||
124 | * Removes files or directories. | ||
125 | * | ||
126 | * @param string|array|\Traversable $files A filename, an array of files, or a \Traversable instance to remove | ||
127 | * | ||
128 | * @throws IOException When removal fails | ||
129 | */ | ||
130 | public function remove($files) | ||
131 | { | ||
132 | $files = iterator_to_array($this->toIterator($files)); | ||
133 | $files = array_reverse($files); | ||
134 | foreach ($files as $file) { | ||
135 | if (!file_exists($file) && !is_link($file)) { | ||
136 | continue; | ||
137 | } | ||
138 | |||
139 | if (is_dir($file) && !is_link($file)) { | ||
140 | $this->remove(new \FilesystemIterator($file)); | ||
141 | |||
142 | if (true !== @rmdir($file)) { | ||
143 | throw new IOException(sprintf('Failed to remove directory %s', $file)); | ||
144 | } | ||
145 | } else { | ||
146 | // https://bugs.php.net/bug.php?id=52176 | ||
147 | if (defined('PHP_WINDOWS_VERSION_MAJOR') && is_dir($file)) { | ||
148 | if (true !== @rmdir($file)) { | ||
149 | throw new IOException(sprintf('Failed to remove file %s', $file)); | ||
150 | } | ||
151 | } else { | ||
152 | if (true !== @unlink($file)) { | ||
153 | throw new IOException(sprintf('Failed to remove file %s', $file)); | ||
154 | } | ||
155 | } | ||
156 | } | ||
157 | } | ||
158 | } | ||
159 | |||
160 | /** | ||
161 | * Change mode for an array of files or directories. | ||
162 | * | ||
163 | * @param string|array|\Traversable $files A filename, an array of files, or a \Traversable instance to change mode | ||
164 | * @param integer $mode The new mode (octal) | ||
165 | * @param integer $umask The mode mask (octal) | ||
166 | * @param Boolean $recursive Whether change the mod recursively or not | ||
167 | * | ||
168 | * @throws IOException When the change fail | ||
169 | */ | ||
170 | public function chmod($files, $mode, $umask = 0000, $recursive = false) | ||
171 | { | ||
172 | foreach ($this->toIterator($files) as $file) { | ||
173 | if ($recursive && is_dir($file) && !is_link($file)) { | ||
174 | $this->chmod(new \FilesystemIterator($file), $mode, $umask, true); | ||
175 | } | ||
176 | if (true !== @chmod($file, $mode & ~$umask)) { | ||
177 | throw new IOException(sprintf('Failed to chmod file %s', $file)); | ||
178 | } | ||
179 | } | ||
180 | } | ||
181 | |||
182 | /** | ||
183 | * Change the owner of an array of files or directories | ||
184 | * | ||
185 | * @param string|array|\Traversable $files A filename, an array of files, or a \Traversable instance to change owner | ||
186 | * @param string $user The new owner user name | ||
187 | * @param Boolean $recursive Whether change the owner recursively or not | ||
188 | * | ||
189 | * @throws IOException When the change fail | ||
190 | */ | ||
191 | public function chown($files, $user, $recursive = false) | ||
192 | { | ||
193 | foreach ($this->toIterator($files) as $file) { | ||
194 | if ($recursive && is_dir($file) && !is_link($file)) { | ||
195 | $this->chown(new \FilesystemIterator($file), $user, true); | ||
196 | } | ||
197 | if (is_link($file) && function_exists('lchown')) { | ||
198 | if (true !== @lchown($file, $user)) { | ||
199 | throw new IOException(sprintf('Failed to chown file %s', $file)); | ||
200 | } | ||
201 | } else { | ||
202 | if (true !== @chown($file, $user)) { | ||
203 | throw new IOException(sprintf('Failed to chown file %s', $file)); | ||
204 | } | ||
205 | } | ||
206 | } | ||
207 | } | ||
208 | |||
209 | /** | ||
210 | * Change the group of an array of files or directories | ||
211 | * | ||
212 | * @param string|array|\Traversable $files A filename, an array of files, or a \Traversable instance to change group | ||
213 | * @param string $group The group name | ||
214 | * @param Boolean $recursive Whether change the group recursively or not | ||
215 | * | ||
216 | * @throws IOException When the change fail | ||
217 | */ | ||
218 | public function chgrp($files, $group, $recursive = false) | ||
219 | { | ||
220 | foreach ($this->toIterator($files) as $file) { | ||
221 | if ($recursive && is_dir($file) && !is_link($file)) { | ||
222 | $this->chgrp(new \FilesystemIterator($file), $group, true); | ||
223 | } | ||
224 | if (is_link($file) && function_exists('lchgrp')) { | ||
225 | if (true !== @lchgrp($file, $group)) { | ||
226 | throw new IOException(sprintf('Failed to chgrp file %s', $file)); | ||
227 | } | ||
228 | } else { | ||
229 | if (true !== @chgrp($file, $group)) { | ||
230 | throw new IOException(sprintf('Failed to chgrp file %s', $file)); | ||
231 | } | ||
232 | } | ||
233 | } | ||
234 | } | ||
235 | |||
236 | /** | ||
237 | * Renames a file or a directory. | ||
238 | * | ||
239 | * @param string $origin The origin filename or directory | ||
240 | * @param string $target The new filename or directory | ||
241 | * @param Boolean $overwrite Whether to overwrite the target if it already exists | ||
242 | * | ||
243 | * @throws IOException When target file or directory already exists | ||
244 | * @throws IOException When origin cannot be renamed | ||
245 | */ | ||
246 | public function rename($origin, $target, $overwrite = false) | ||
247 | { | ||
248 | // we check that target does not exist | ||
249 | if (!$overwrite && is_readable($target)) { | ||
250 | throw new IOException(sprintf('Cannot rename because the target "%s" already exist.', $target)); | ||
251 | } | ||
252 | |||
253 | if (true !== @rename($origin, $target)) { | ||
254 | throw new IOException(sprintf('Cannot rename "%s" to "%s".', $origin, $target)); | ||
255 | } | ||
256 | } | ||
257 | |||
258 | /** | ||
259 | * Creates a symbolic link or copy a directory. | ||
260 | * | ||
261 | * @param string $originDir The origin directory path | ||
262 | * @param string $targetDir The symbolic link name | ||
263 | * @param Boolean $copyOnWindows Whether to copy files if on Windows | ||
264 | * | ||
265 | * @throws IOException When symlink fails | ||
266 | */ | ||
267 | public function symlink($originDir, $targetDir, $copyOnWindows = false) | ||
268 | { | ||
269 | if (!function_exists('symlink') && $copyOnWindows) { | ||
270 | $this->mirror($originDir, $targetDir); | ||
271 | |||
272 | return; | ||
273 | } | ||
274 | |||
275 | $this->mkdir(dirname($targetDir)); | ||
276 | |||
277 | $ok = false; | ||
278 | if (is_link($targetDir)) { | ||
279 | if (readlink($targetDir) != $originDir) { | ||
280 | $this->remove($targetDir); | ||
281 | } else { | ||
282 | $ok = true; | ||
283 | } | ||
284 | } | ||
285 | |||
286 | if (!$ok) { | ||
287 | if (true !== @symlink($originDir, $targetDir)) { | ||
288 | $report = error_get_last(); | ||
289 | if (is_array($report)) { | ||
290 | if (defined('PHP_WINDOWS_VERSION_MAJOR') && false !== strpos($report['message'], 'error code(1314)')) { | ||
291 | throw new IOException('Unable to create symlink due to error code 1314: \'A required privilege is not held by the client\'. Do you have the required Administrator-rights?'); | ||
292 | } | ||
293 | } | ||
294 | throw new IOException(sprintf('Failed to create symbolic link from %s to %s', $originDir, $targetDir)); | ||
295 | } | ||
296 | } | ||
297 | } | ||
298 | |||
299 | /** | ||
300 | * Given an existing path, convert it to a path relative to a given starting path | ||
301 | * | ||
302 | * @param string $endPath Absolute path of target | ||
303 | * @param string $startPath Absolute path where traversal begins | ||
304 | * | ||
305 | * @return string Path of target relative to starting path | ||
306 | */ | ||
307 | public function makePathRelative($endPath, $startPath) | ||
308 | { | ||
309 | // Normalize separators on windows | ||
310 | if (defined('PHP_WINDOWS_VERSION_MAJOR')) { | ||
311 | $endPath = strtr($endPath, '\\', '/'); | ||
312 | $startPath = strtr($startPath, '\\', '/'); | ||
313 | } | ||
314 | |||
315 | // Split the paths into arrays | ||
316 | $startPathArr = explode('/', trim($startPath, '/')); | ||
317 | $endPathArr = explode('/', trim($endPath, '/')); | ||
318 | |||
319 | // Find for which directory the common path stops | ||
320 | $index = 0; | ||
321 | while (isset($startPathArr[$index]) && isset($endPathArr[$index]) && $startPathArr[$index] === $endPathArr[$index]) { | ||
322 | $index++; | ||
323 | } | ||
324 | |||
325 | // Determine how deep the start path is relative to the common path (ie, "web/bundles" = 2 levels) | ||
326 | $depth = count($startPathArr) - $index; | ||
327 | |||
328 | // Repeated "../" for each level need to reach the common path | ||
329 | $traverser = str_repeat('../', $depth); | ||
330 | |||
331 | $endPathRemainder = implode('/', array_slice($endPathArr, $index)); | ||
332 | |||
333 | // Construct $endPath from traversing to the common path, then to the remaining $endPath | ||
334 | $relativePath = $traverser.(strlen($endPathRemainder) > 0 ? $endPathRemainder.'/' : ''); | ||
335 | |||
336 | return (strlen($relativePath) === 0) ? './' : $relativePath; | ||
337 | } | ||
338 | |||
339 | /** | ||
340 | * Mirrors a directory to another. | ||
341 | * | ||
342 | * @param string $originDir The origin directory | ||
343 | * @param string $targetDir The target directory | ||
344 | * @param \Traversable $iterator A Traversable instance | ||
345 | * @param array $options An array of boolean options | ||
346 | * Valid options are: | ||
347 | * - $options['override'] Whether to override an existing file on copy or not (see copy()) | ||
348 | * - $options['copy_on_windows'] Whether to copy files instead of links on Windows (see symlink()) | ||
349 | * - $options['delete'] Whether to delete files that are not in the source directory (defaults to false) | ||
350 | * | ||
351 | * @throws IOException When file type is unknown | ||
352 | */ | ||
353 | public function mirror($originDir, $targetDir, \Traversable $iterator = null, $options = array()) | ||
354 | { | ||
355 | $targetDir = rtrim($targetDir, '/\\'); | ||
356 | $originDir = rtrim($originDir, '/\\'); | ||
357 | |||
358 | // Iterate in destination folder to remove obsolete entries | ||
359 | if ($this->exists($targetDir) && isset($options['delete']) && $options['delete']) { | ||
360 | $deleteIterator = $iterator; | ||
361 | if (null === $deleteIterator) { | ||
362 | $flags = \FilesystemIterator::SKIP_DOTS; | ||
363 | $deleteIterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($targetDir, $flags), \RecursiveIteratorIterator::CHILD_FIRST); | ||
364 | } | ||
365 | foreach ($deleteIterator as $file) { | ||
366 | $origin = str_replace($targetDir, $originDir, $file->getPathname()); | ||
367 | if (!$this->exists($origin)) { | ||
368 | $this->remove($file); | ||
369 | } | ||
370 | } | ||
371 | } | ||
372 | |||
373 | $copyOnWindows = false; | ||
374 | if (isset($options['copy_on_windows']) && !function_exists('symlink')) { | ||
375 | $copyOnWindows = $options['copy_on_windows']; | ||
376 | } | ||
377 | |||
378 | if (null === $iterator) { | ||
379 | $flags = $copyOnWindows ? \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS : \FilesystemIterator::SKIP_DOTS; | ||
380 | $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($originDir, $flags), \RecursiveIteratorIterator::SELF_FIRST); | ||
381 | } | ||
382 | |||
383 | foreach ($iterator as $file) { | ||
384 | $target = str_replace($originDir, $targetDir, $file->getPathname()); | ||
385 | |||
386 | if ($copyOnWindows) { | ||
387 | if (is_link($file) || is_file($file)) { | ||
388 | $this->copy($file, $target, isset($options['override']) ? $options['override'] : false); | ||
389 | } elseif (is_dir($file)) { | ||
390 | $this->mkdir($target); | ||
391 | } else { | ||
392 | throw new IOException(sprintf('Unable to guess "%s" file type.', $file)); | ||
393 | } | ||
394 | } else { | ||
395 | if (is_link($file)) { | ||
396 | $this->symlink($file, $target); | ||
397 | } elseif (is_dir($file)) { | ||
398 | $this->mkdir($target); | ||
399 | } elseif (is_file($file)) { | ||
400 | $this->copy($file, $target, isset($options['override']) ? $options['override'] : false); | ||
401 | } else { | ||
402 | throw new IOException(sprintf('Unable to guess "%s" file type.', $file)); | ||
403 | } | ||
404 | } | ||
405 | } | ||
406 | } | ||
407 | |||
408 | /** | ||
409 | * Returns whether the file path is an absolute path. | ||
410 | * | ||
411 | * @param string $file A file path | ||
412 | * | ||
413 | * @return Boolean | ||
414 | */ | ||
415 | public function isAbsolutePath($file) | ||
416 | { | ||
417 | if (strspn($file, '/\\', 0, 1) | ||
418 | || (strlen($file) > 3 && ctype_alpha($file[0]) | ||
419 | && substr($file, 1, 1) === ':' | ||
420 | && (strspn($file, '/\\', 2, 1)) | ||
421 | ) | ||
422 | || null !== parse_url($file, PHP_URL_SCHEME) | ||
423 | ) { | ||
424 | return true; | ||
425 | } | ||
426 | |||
427 | return false; | ||
428 | } | ||
429 | |||
430 | /** | ||
431 | * @param mixed $files | ||
432 | * | ||
433 | * @return \Traversable | ||
434 | */ | ||
435 | private function toIterator($files) | ||
436 | { | ||
437 | if (!$files instanceof \Traversable) { | ||
438 | $files = new \ArrayObject(is_array($files) ? $files : array($files)); | ||
439 | } | ||
440 | |||
441 | return $files; | ||
442 | } | ||
443 | |||
444 | /** | ||
445 | * Atomically dumps content into a file. | ||
446 | * | ||
447 | * @param string $filename The file to be written to. | ||
448 | * @param string $content The data to write into the file. | ||
449 | * @param integer $mode The file mode (octal). | ||
450 | * @throws IOException If the file cannot be written to. | ||
451 | */ | ||
452 | public function dumpFile($filename, $content, $mode = 0666) | ||
453 | { | ||
454 | $dir = dirname($filename); | ||
455 | |||
456 | if (!is_dir($dir)) { | ||
457 | $this->mkdir($dir); | ||
458 | } elseif (!is_writable($dir)) { | ||
459 | throw new IOException(sprintf('Unable to write in the %s directory\n', $dir)); | ||
460 | } | ||
461 | |||
462 | $tmpFile = tempnam($dir, basename($filename)); | ||
463 | |||
464 | if (false === @file_put_contents($tmpFile, $content)) { | ||
465 | throw new IOException(sprintf('Failed to write file "%s".', $filename)); | ||
466 | } | ||
467 | |||
468 | $this->rename($tmpFile, $filename, true); | ||
469 | $this->chmod($filename, $mode); | ||
470 | } | ||
471 | } | ||