9d7f55784a65f4faf7ac7f58d28e64bd1d86da9f
[friendica-addons.git/.git] / advancedcontentfilter / vendor / symfony / cache / Traits / FilesystemTrait.php
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\Cache\Traits;
13
14 use Symfony\Component\Cache\Exception\CacheException;
15
16 /**
17  * @author Nicolas Grekas <p@tchwork.com>
18  * @author Rob Frawley 2nd <rmf@src.run>
19  *
20  * @internal
21  */
22 trait FilesystemTrait
23 {
24     use FilesystemCommonTrait;
25
26     /**
27      * @return bool
28      */
29     public function prune()
30     {
31         $time = time();
32         $pruned = true;
33
34         foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->directory, \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
35             if (!$h = @fopen($file, 'rb')) {
36                 continue;
37             }
38
39             if (($expiresAt = (int) fgets($h)) && $time >= $expiresAt) {
40                 fclose($h);
41                 $pruned = @unlink($file) && !file_exists($file) && $pruned;
42             } else {
43                 fclose($h);
44             }
45         }
46
47         return $pruned;
48     }
49
50     /**
51      * {@inheritdoc}
52      */
53     protected function doFetch(array $ids)
54     {
55         $values = [];
56         $now = time();
57
58         foreach ($ids as $id) {
59             $file = $this->getFile($id);
60             if (!file_exists($file) || !$h = @fopen($file, 'rb')) {
61                 continue;
62             }
63             if (($expiresAt = (int) fgets($h)) && $now >= $expiresAt) {
64                 fclose($h);
65                 @unlink($file);
66             } else {
67                 $i = rawurldecode(rtrim(fgets($h)));
68                 $value = stream_get_contents($h);
69                 fclose($h);
70                 if ($i === $id) {
71                     $values[$id] = parent::unserialize($value);
72                 }
73             }
74         }
75
76         return $values;
77     }
78
79     /**
80      * {@inheritdoc}
81      */
82     protected function doHave($id)
83     {
84         $file = $this->getFile($id);
85
86         return file_exists($file) && (@filemtime($file) > time() || $this->doFetch([$id]));
87     }
88
89     /**
90      * {@inheritdoc}
91      */
92     protected function doSave(array $values, $lifetime)
93     {
94         $ok = true;
95         $expiresAt = $lifetime ? (time() + $lifetime) : 0;
96
97         foreach ($values as $id => $value) {
98             $ok = $this->write($this->getFile($id, true), $expiresAt."\n".rawurlencode($id)."\n".serialize($value), $expiresAt) && $ok;
99         }
100
101         if (!$ok && !is_writable($this->directory)) {
102             throw new CacheException(sprintf('Cache directory is not writable (%s).', $this->directory));
103         }
104
105         return $ok;
106     }
107 }