|
20 | 20 | */
|
21 | 21 | class CacheFileUtils
|
22 | 22 | {
|
23 |
| - |
24 | 23 | /**
|
25 |
| - * Make sure the given directory can be used as a cache directory, creating it if necessary. |
26 |
| - * @param string $dir The directory path |
27 |
| - * @param string $name An optional name to be used in the exception message in case of failure |
28 |
| - * @throws \RuntimeException if the directory cannot be created or written to. |
| 24 | + * Dumps content into a file, trying to make it atomically. The directory for the file must exist. |
| 25 | + * @param string $filename The file to be written to. |
| 26 | + * @param string $content The data to write into the file. |
| 27 | + * @throws \RuntimeException If the file cannot be written to. |
29 | 28 | */
|
30 |
| - public static function createCacheDir($dir, $name = null) |
| 29 | + public static function dumpInFile($filename, $content) |
31 | 30 | {
|
32 |
| - if ($name) { |
33 |
| - $name = " ($name)"; |
34 |
| - } |
| 31 | + $dir = dirname($filename); |
35 | 32 |
|
36 | 33 | if (!is_dir($dir)) {
|
37 | 34 | if (false === @mkdir($dir, 0777, true)) {
|
38 |
| - throw new \RuntimeException(sprintf('Unable to create the %s directory%s\n', $dir, $name)); |
| 35 | + throw new \RuntimeException(sprintf('Unable to create the %s directory\n', $dir)); |
39 | 36 | }
|
40 | 37 | } elseif (!is_writable($dir)) {
|
41 |
| - throw new \RuntimeException(sprintf('Unable to write in the %s directory%s\n', $dir, $name)); |
| 38 | + throw new \RuntimeException(sprintf('Unable to write in the %s directory\n', $dir)); |
42 | 39 | }
|
43 |
| - } |
44 |
| - |
45 |
| - /** |
46 |
| - * Tries to create a writeable directory that can contain a given file. |
47 |
| - * |
48 |
| - * @param string $filename The file the containing directory has to be created for |
49 |
| - * @return string The directory now available for caching |
50 |
| - * @throws \RuntimeException If the directory cannot be created or written to |
51 |
| - */ |
52 |
| - public static function createDirectoryForFile($filename) |
53 |
| - { |
54 |
| - $dir = dirname($filename); |
55 |
| - self::createCacheDir($dir); |
56 |
| - return $dir; |
57 |
| - } |
58 |
| - |
59 |
| - /** |
60 |
| - * Dumps content into a file, trying to make it atomically. The directory for the file must exist. |
61 |
| - * @param string $filename The file to be written to. |
62 |
| - * @param string $content The data to write into the file. |
63 |
| - * @throws \RuntimeException If the file cannot be written to. |
64 |
| - */ |
65 |
| - public static function dumpInFile($filename, $content) |
66 |
| - { |
67 |
| - $dir = self::createDirectoryForFile($filename); |
68 | 40 |
|
69 | 41 | $tmpFile = tempnam($dir, basename($filename));
|
| 42 | + |
70 | 43 | if (false !== @file_put_contents($tmpFile, $content) && @rename($tmpFile, $filename)) {
|
71 | 44 | @chmod($filename, 0666 & ~umask());
|
72 | 45 | } else {
|
73 | 46 | throw new \RuntimeException(sprintf('Failed to write cache file "%s".', $filename));
|
74 | 47 | }
|
75 |
| - |
76 | 48 | }
|
77 | 49 | }
|
0 commit comments