• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Cecilapp / Cecil / 14620241211

23 Apr 2025 02:06PM UTC coverage: 83.787%. First build
14620241211

Pull #2148

github

web-flow
Merge 12fc09dec into 6d7ba8f0a
Pull Request #2148: refactor: configuration and cache

361 of 423 new or added lines in 26 files covered. (85.34%)

3049 of 3639 relevant lines covered (83.79%)

0.84 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

60.17
/src/Assets/Cache.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of Cecil.
7
 *
8
 * Copyright (c) Arnaud Ligny <arnaud@ligny.fr>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13

14
namespace Cecil\Assets;
15

16
use Cecil\Builder;
17
use Cecil\Collection\Page\Page;
18
use Cecil\Exception\RuntimeException;
19
use Cecil\Util;
20
use Psr\SimpleCache\CacheInterface;
21

22
class Cache implements CacheInterface
23
{
24
    /** @var Builder */
25
    protected $builder;
26

27
    /** @var string */
28
    protected $cacheDir;
29

30
    /** @var int */
31
    protected $duration;
32

33
    public function __construct(Builder $builder, string $pool = '')
34
    {
35
        $this->builder = $builder;
1✔
36
        $this->cacheDir = Util::joinFile($builder->getConfig()->getCachePath(), $pool);
1✔
37
        $this->duration = 31536000; // 1 year
1✔
38
    }
39

40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function set($key, $value, $ttl = null): bool
44
    {
45
        try {
46
            $key = self::sanitizeKey($key);
1✔
47
            $this->prune($key);
1✔
48
            // put file content in a dedicated file
49
            if (\is_array($value) && !empty($value['content']) && !empty($value['path'])) {
1✔
50
                Util\File::getFS()->dumpFile($this->getContentFilePathname($value['path']), $value['content']);
1✔
51
                unset($value['content']);
1✔
52
            }
53
            // serialize data
54
            $data = serialize([
1✔
55
                'value'      => $value,
1✔
56
                'expiration' => time() + $this->duration($ttl),
1✔
57
            ]);
1✔
58
            Util\File::getFS()->dumpFile($this->getFilePathname($key), $data);
1✔
59
            $this->builder->getLogger()->debug(\sprintf('Cache created: "%s"', Util\File::getFS()->makePathRelative($this->getFilePathname($key), $this->builder->getConfig()->getCachePath())));
1✔
NEW
60
        } catch (\Exception $e) {
×
NEW
61
            $this->builder->getLogger()->error($e->getMessage());
×
62

NEW
63
            return false;
×
64
        }
65

66
        return true;
1✔
67
    }
68

69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function has($key): bool
73
    {
74
        $key = self::sanitizeKey($key);
1✔
75
        if (!Util\File::getFS()->exists($this->getFilePathname($key))) {
1✔
76
            return false;
1✔
77
        }
78

79
        return true;
1✔
80
    }
81

82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function get($key, $default = null): mixed
86
    {
87
        try {
88
            $key = self::sanitizeKey($key);
1✔
89
            // return default value if file doesn't exists
90
            if (false === $content = Util\File::fileGetContents($this->getFilePathname($key))) {
1✔
91
                return $default;
×
92
            }
93
            // unserialize data
94
            $data = unserialize($content);
1✔
95
            // check expiration
96
            if ($data['expiration'] <= time()) {
1✔
97
                $this->delete($key);
×
98

99
                return $default;
×
100
            }
101
            // get content from dedicated file
102
            if (\is_array($data['value']) && isset($data['value']['path'])) {
1✔
103
                if (false !== $content = Util\File::fileGetContents($this->getContentFilePathname($data['value']['path']))) {
1✔
104
                    $data['value']['content'] = $content;
1✔
105
                }
106
            }
107
        } catch (\Exception $e) {
×
108
            $this->builder->getLogger()->error($e->getMessage());
×
109

110
            return $default;
×
111
        }
112

113
        return $data['value'];
1✔
114
    }
115

116
    /**
117
     * {@inheritdoc}
118
     */
119
    public function delete($key): bool
120
    {
121
        try {
NEW
122
            $key = self::sanitizeKey($key);
×
123
            Util\File::getFS()->remove($this->getFilePathname($key));
×
124
            $this->prune($key);
×
125
        } catch (\Exception $e) {
×
126
            $this->builder->getLogger()->error($e->getMessage());
×
127

128
            return false;
×
129
        }
130

131
        return true;
×
132
    }
133

134
    /**
135
     * {@inheritdoc}
136
     */
137
    public function clear(): bool
138
    {
139
        try {
140
            Util\File::getFS()->remove($this->cacheDir);
×
141
        } catch (\Exception $e) {
×
142
            $this->builder->getLogger()->error($e->getMessage());
×
143

144
            return false;
×
145
        }
146

147
        return true;
×
148
    }
149

150
    /**
151
     * {@inheritdoc}
152
     */
153
    public function getMultiple($keys, $default = null): iterable
154
    {
155
        throw new \Exception(\sprintf('%s::%s not yet implemented.', __CLASS__, __FUNCTION__));
×
156
    }
157

158
    /**
159
     * {@inheritdoc}
160
     */
161
    public function setMultiple($values, $ttl = null): bool
162
    {
163
        throw new \Exception(\sprintf('%s::%s not yet implemented.', __CLASS__, __FUNCTION__));
×
164
    }
165

166
    /**
167
     * {@inheritdoc}
168
     */
169
    public function deleteMultiple($keys): bool
170
    {
171
        throw new \Exception(\sprintf('%s::%s not yet implemented.', __CLASS__, __FUNCTION__));
×
172
    }
173

174
    /**
175
     * Creates key from a string: "$name|uniqid__HASH__VERSION".
176
     * $name is optional to add a human readable name to the key.
177
     */
178
    public function createKey(?string $name, string $value): string
179
    {
180
        $hash = hash('md5', $value);
1✔
181
        $name = $name ? self::sanitizeKey($name) : $hash;
1✔
182

183
        return \sprintf('%s__%s__%s', $name, $hash, $this->builder->getVersion());
1✔
184
    }
185

186
    /**
187
     * Creates key from an Asset: "$path_$ext_$tags__HASH__VERSION".
188
     */
189
    public function createKeyFromAsset(Asset $asset, ?array $tags = null): string
190
    {
191
        $t = $tags;
1✔
192
        $tags = [];
1✔
193

194
        if ($t !== null) {
1✔
195
            ksort($t);
1✔
196
            foreach ($t as $key => $value) {
1✔
197
                switch (\gettype($value)) {
1✔
198
                    case 'boolean':
1✔
199
                        if ($value === true) {
1✔
200
                            $tags[] = $key;
1✔
201
                        }
202
                        break;
1✔
203
                    case 'string':
1✔
204
                    case 'integer':
1✔
205
                        if (!empty($value)) {
1✔
206
                            $tags[] = substr($key, 0, 1) . $value;
1✔
207
                        }
208
                        break;
1✔
209
                }
210
            }
211
        }
212

213
        $tagsInline = implode('_', $tags);
1✔
214
        $name = "{$asset['_path']}_{$asset['ext']}_$tagsInline";
1✔
215

216
        return $this->createKey($name, $asset['content'] ?? '');
1✔
217
    }
218

219
    /**
220
     * Creates key from a file: "RelativePathname__MD5".
221
     *
222
     * @throws RuntimeException
223
     */
224
    public function createKeyFromFile(\Symfony\Component\Finder\SplFileInfo $file): string
225
    {
226
        if (false === $content = Util\File::fileGetContents($file->getRealPath())) {
1✔
NEW
227
            throw new RuntimeException(\sprintf('Can\'t create cache key for "%s".', $file));
×
228
        }
229

230
        return $this->createKey($file->getRelativePathname(), $content);
1✔
231
    }
232

233
    /**
234
     * Clear cache by pattern.
235
     */
236
    public function clearByPattern(string $pattern): int
237
    {
238
        try {
239
            if (!Util\File::getFS()->exists($this->cacheDir)) {
×
240
                throw new RuntimeException(\sprintf('The cache directory "%s" does not exists.', $this->cacheDir));
×
241
            }
242
            $fileCount = 0;
×
243
            $iterator = new \RecursiveIteratorIterator(
×
244
                new \RecursiveDirectoryIterator($this->cacheDir),
×
245
                \RecursiveIteratorIterator::SELF_FIRST
×
246
            );
×
247
            foreach ($iterator as $file) {
×
248
                if ($file->isFile()) {
×
249
                    if (preg_match('/' . $pattern . '/i', $file->getPathname())) {
×
250
                        Util\File::getFS()->remove($file->getPathname());
×
251
                        $fileCount++;
×
NEW
252
                        $this->builder->getLogger()->debug(\sprintf('Cache removed: "%s"', Util\File::getFS()->makePathRelative($file->getPathname(), $this->builder->getConfig()->getCachePath())));
×
253
                    }
254
                }
255
            }
256
        } catch (\Exception $e) {
×
257
            $this->builder->getLogger()->error($e->getMessage());
×
258

259
            return 0;
×
260
        }
261

262
        return $fileCount;
×
263
    }
264

265
    /**
266
     * Returns cache content file pathname from path.
267
     */
268
    public function getContentFilePathname(string $path): string
269
    {
270
        $path = str_replace(['https://', 'http://'], '', $path); // remove protocol (if URL)
1✔
271

272
        return Util::joinFile($this->cacheDir, 'files', $path);
1✔
273
    }
274

275
    /**
276
     * Returns cache file pathname from key.
277
     */
278
    private function getFilePathname(string $key): string
279
    {
280
        return Util::joinFile($this->cacheDir, "$key.ser");
1✔
281
    }
282

283
    /**
284
     * Prepares and validate $key.
285
     */
286
    public static function sanitizeKey(string $key): string
287
    {
288
        $key = str_replace(['https://', 'http://'], '', $key); // remove protocol (if URL)
1✔
289
        $key = Page::slugify($key);                            // slugify
1✔
290
        $key = trim($key, '/');                                // remove leading/trailing slashes
1✔
291
        $key = str_replace(['\\', '/'], ['-', '-'], $key);     // replace slashes by hyphens
1✔
292
        $key = substr($key, 0, 200);                           // truncate to 200 characters (NTFS filename length limit is 255 characters)
1✔
293

294
        return $key;
1✔
295
    }
296

297
    /**
298
     * Removes previous cache files.
299
     */
300
    private function prune(string $key): bool
301
    {
302
        try {
303
            $keyAsArray = explode('__', self::sanitizeKey($key));
1✔
304
            // if 2 or more parts (with hash), remove all files with the same first part
305
            // pattern: `name__hash__version`
306
            if (!empty($keyAsArray[0]) && \count($keyAsArray) >= 2) {
1✔
307
                $pattern = Util::joinFile($this->cacheDir, $keyAsArray[0]) . '*';
1✔
308
                foreach (glob($pattern) ?: [] as $filename) {
1✔
309
                    Util\File::getFS()->remove($filename);
1✔
310
                    $this->builder->getLogger()->debug(\sprintf('Cache removed: "%s"', Util\File::getFS()->makePathRelative($filename, $this->builder->getConfig()->getCachePath())));
1✔
311
                }
312
            }
313
        } catch (\Exception $e) {
×
314
            $this->builder->getLogger()->error($e->getMessage());
×
315

316
            return false;
×
317
        }
318

319
        return true;
1✔
320
    }
321

322
    /**
323
     * Convert the various expressions of a TTL value into duration in seconds.
324
     */
325
    protected function duration(\DateInterval|int|null $ttl): int
326
    {
327
        if ($ttl === null) {
1✔
328
            return $this->duration;
1✔
329
        }
330
        if (\is_int($ttl)) {
1✔
331
            return $ttl;
×
332
        }
333
        if ($ttl instanceof \DateInterval) {
1✔
334
            return (int) $ttl->d * 86400 + $ttl->h * 3600 + $ttl->i * 60 + $ttl->s;
1✔
335
        }
336

337
        throw new \InvalidArgumentException('TTL values must be one of null, int, \DateInterval');
×
338
    }
339
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc