PhpFileCache::includeFileForId()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 3
b 0
f 0
nc 2
nop 1
dl 0
loc 16
ccs 8
cts 8
cp 1
crap 2
rs 10
1
<?php
2
3
namespace Doctrine\Common\Cache;
4
5
use function is_object;
6
use function method_exists;
7
use function restore_error_handler;
8
use function serialize;
9
use function set_error_handler;
10
use function sprintf;
11
use function time;
12
use function var_export;
13
14
/**
15
 * Php file cache driver.
16
 */
17
class PhpFileCache extends FileCache
18
{
19
    public const EXTENSION = '.doctrinecache.php';
20
21
    /**
22
     * @var callable
23
     *
24
     * This is cached in a local static variable to avoid instantiating a closure each time we need an empty handler
25
     */
26
    private static $emptyErrorHandler;
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 80
    public function __construct($directory, $extension = self::EXTENSION, $umask = 0002)
32
    {
33 80
        parent::__construct($directory, $extension, $umask);
34
35
        self::$emptyErrorHandler = static function () {
36 80
        };
37 80
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 78
    protected function doFetch($id)
43
    {
44 78
        $value = $this->includeFileForId($id);
45
46 78
        if ($value === null) {
47 78
            return false;
48
        }
49
50 66
        if ($value['lifetime'] !== 0 && $value['lifetime'] < time()) {
51
            return false;
52
        }
53
54 66
        return $value['data'];
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 69
    protected function doContains($id)
61
    {
62 69
        $value = $this->includeFileForId($id);
63
64 69
        if ($value === null) {
65 48
            return false;
66
        }
67
68 65
        return $value['lifetime'] === 0 || $value['lifetime'] > time();
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74 76
    protected function doSave($id, $data, $lifeTime = 0)
75
    {
76 76
        if ($lifeTime > 0) {
77 3
            $lifeTime = time() + $lifeTime;
78
        }
79
80 76
        $filename = $this->getFilename($id);
81
82
        $value = [
83 76
            'lifetime'  => $lifeTime,
84 76
            'data'      => $data,
85
        ];
86
87 76
        if (is_object($data) && method_exists($data, '__set_state')) {
88 1
            $value = var_export($value, true);
89 1
            $code  = sprintf('<?php return %s;', $value);
90
        } else {
91 75
            $value = var_export(serialize($value), true);
92 75
            $code  = sprintf('<?php return unserialize(%s);', $value);
93
        }
94
95 76
        return $this->writeFile($filename, $code);
96
    }
97
98
    /**
99
     * @return array|null
100
     */
101 78
    private function includeFileForId(string $id) : ?array
102
    {
103 78
        $fileName = $this->getFilename($id);
104
105
        // note: error suppression is still faster than `file_exists`, `is_file` and `is_readable`
106 78
        set_error_handler(self::$emptyErrorHandler);
107
108 78
        $value = include $fileName;
109
110 78
        restore_error_handler();
111
112 78
        if (! isset($value['lifetime'])) {
113 78
            return null;
114
        }
115
116 71
        return $value;
117
    }
118
}
119