Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 31 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
File | |
0.00% |
0 / 31 |
|
0.00% |
0 / 7 |
182 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setCacheDir | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
20 | |||
options2string | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
20 | |||
cacheName | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
isCached | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getCached | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setCache | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | /** |
4 | * Karla ImageMagick wrapper library |
5 | * |
6 | * PHP Version 8.0< |
7 | * |
8 | * @category Utility |
9 | * @author Johannes Skov Frandsen <jsf@greenoak.dk> |
10 | * @license http://www.opensource.org/licenses/mit-license.php MIT |
11 | * @link https://github.com/localgod/karla Karla |
12 | * @since 2012-04-05 |
13 | */ |
14 | |
15 | declare(strict_types=1); |
16 | |
17 | namespace Karla\Cache; |
18 | |
19 | /** |
20 | * Class for file caching |
21 | * |
22 | * @category Utility |
23 | * @author Johannes Skov Frandsen <jsf@greenoak.dk> |
24 | * @license http://www.opensource.org/licenses/mit-license.php MIT |
25 | * @link https://github.com/localgod/karla Karla |
26 | */ |
27 | class File implements \Karla\Cache |
28 | { |
29 | /** |
30 | * Cache directory |
31 | * |
32 | * @var string |
33 | */ |
34 | private string $cacheDir; |
35 | |
36 | /** |
37 | * Create a new file cache |
38 | * |
39 | * @param string $dirName |
40 | * Path to cach directory |
41 | * |
42 | * @throws \InvalidArgumentException |
43 | */ |
44 | public function __construct(string $dirName) |
45 | { |
46 | $this->setCacheDir($dirName); |
47 | } |
48 | |
49 | /** |
50 | * Set the cache directory |
51 | * |
52 | * @param string $dirName |
53 | * Path to the cache directory |
54 | * |
55 | * @return void |
56 | * @throws \InvalidArgumentException if path was not found |
57 | * @throws \InvalidArgumentException if path was not writeable |
58 | * @throws \InvalidArgumentException if path was a directoy |
59 | */ |
60 | private function setCacheDir(string $dirName): void |
61 | { |
62 | if (! file_exists($dirName)) { |
63 | throw new \InvalidArgumentException("Path not found", 0); |
64 | } |
65 | if (! is_writeable($dirName)) { |
66 | throw new \InvalidArgumentException("Path not writable", 1); |
67 | } |
68 | if (! is_dir($dirName)) { |
69 | throw new \InvalidArgumentException("Path not a directory", 2); |
70 | } |
71 | $this->cacheDir = $dirName; |
72 | } |
73 | |
74 | /** |
75 | * Create a string representation of the options used |
76 | * |
77 | * @param string[] $options |
78 | * Options |
79 | * |
80 | * @return string |
81 | */ |
82 | private function options2string(array $options): string |
83 | { |
84 | $output = array(); |
85 | foreach ($options as $option) { |
86 | if (strstr($option, 'resize')) { |
87 | $option = str_replace('\>', '', $option); |
88 | $option = str_replace('\<', '', $option); |
89 | } |
90 | if (strstr($option, 'crop')) { |
91 | $option = str_replace(' +repage', '', $option); |
92 | } |
93 | $option = trim($option); |
94 | $option = str_replace(' ', '_', $option); |
95 | $option = str_replace('-', '', $option); |
96 | $output[] = $option; |
97 | } |
98 | return implode('&', $output); |
99 | } |
100 | |
101 | /** |
102 | * Generate cache name |
103 | * |
104 | * @param string $inputFile |
105 | * Path to file |
106 | * @param string $outputFile |
107 | * Path to file |
108 | * @param string[] $options |
109 | * Options |
110 | * |
111 | * @return string |
112 | */ |
113 | private function cacheName(string $inputFile, string $outputFile, array $options): string |
114 | { |
115 | $inputFile = str_replace('"', '', $inputFile); |
116 | $outputFile = str_replace('"', '', $outputFile); |
117 | $ext = pathinfo(basename($outputFile), PATHINFO_EXTENSION); |
118 | $filename = $inputFile . $this->options2string($options); |
119 | return $this->cacheDir . '/' . md5($filename) . '.' . $ext; |
120 | } |
121 | |
122 | /** |
123 | * Check if there exists a cached version of the file |
124 | * |
125 | * @param string $inputFile |
126 | * Path to file |
127 | * @param string $outputFile |
128 | * Path to file |
129 | * @param string[] $options |
130 | * Options |
131 | * |
132 | * @return boolean |
133 | */ |
134 | public function isCached(string $inputFile, string $outputFile, array $options): bool |
135 | { |
136 | $filename = $this->cacheName($inputFile, $outputFile, $options); |
137 | return file_exists($filename); |
138 | } |
139 | |
140 | /** |
141 | * Get cached version of the file |
142 | * |
143 | * @param string $inputFile |
144 | * Path to file |
145 | * @param string $outputFile |
146 | * Path to file |
147 | * @param string[] $options |
148 | * Options |
149 | * |
150 | * @return string |
151 | */ |
152 | public function getCached(string $inputFile, string $outputFile, array $options): string |
153 | { |
154 | return $this->cacheName($inputFile, $outputFile, $options); |
155 | } |
156 | |
157 | /** |
158 | * Set cached version of the file |
159 | * |
160 | * @param string $inputFile |
161 | * Path to file |
162 | * @param string $outputFile |
163 | * Path to file |
164 | * @param string[] $options |
165 | * Options |
166 | * |
167 | * @return void |
168 | */ |
169 | public function setCache(string $inputFile, string $outputFile, string $options): void |
170 | { |
171 | $filename = $this->cacheName($inputFile, $outputFile, $options); |
172 | file_put_contents($filename, file_get_contents(str_replace('"', '', $outputFile))); |
173 | shell_exec('chmod 666 ' . $filename); |
174 | } |
175 | } |