Passed
Pull Request — master (#26)
by Dmitriy
05:03
created

FastRouteCache::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Router\FastRoute;
6
7
use Psr\SimpleCache\CacheInterface;
8
9
use function dirname;
10
use function file_exists;
11
use function file_put_contents;
12
use function is_array;
13
use function is_dir;
14
use function is_writable;
15
use function restore_error_handler;
16
use function set_error_handler;
17
use function sprintf;
18
use function var_export;
19
20
class FastRouteCache implements CacheInterface
21
{
22
    /**
23
     * Template used when generating the cache file.
24
     */
25
    public const CACHE_TEMPLATE = <<< 'EOT'
26
<?php
27
return %s;
28
EOT;
29
    /**
30
     * Cache file path relative to the project directory.
31
     *
32
     * @var string
33
     */
34
    private string $cacheFile;
35
36
    public function __construct($cacheFilePath)
37
    {
38
        $this->cacheFile = $cacheFilePath;
39
    }
40
41
    public function get($key, $default = null): array
42
    {
43
        set_error_handler(
44
            static function () {
45
            },
46
            E_WARNING
47
        ); // suppress php warnings
48
        $data = include $this->cacheFile;
49
        restore_error_handler();
50
51
        // Cache file does not exist
52
        if (false === $data) {
53
            throw new \RuntimeException(
54
                sprintf(
55
                    'File "%s"; doesn\'t exist',
56
                    $this->cacheFile
57
                )
58
            );
59
        }
60
61
        if (!is_array($data)) {
62
            throw new \RuntimeException(
63
                sprintf(
64
                    'Invalid cache file "%s"; cache file MUST return an array',
65
                    $this->cacheFile
66
                )
67
            );
68
        }
69
70
        return $data;
71
    }
72
73
    public function set($key, $value, $ttl = null): void
74
    {
75
        $cacheDir = dirname($this->cacheFile);
76
77
        if (!is_dir($cacheDir)) {
78
            throw new \RuntimeException(
79
                sprintf(
80
                    'The cache directory "%s" does not exist',
81
                    $cacheDir
82
                )
83
            );
84
        }
85
86
        if (!is_writable($cacheDir)) {
87
            throw new \RuntimeException(
88
                sprintf(
89
                    'The cache directory "%s" is not writable',
90
                    $cacheDir
91
                )
92
            );
93
        }
94
95
        if (file_exists($this->cacheFile) && !is_writable($this->cacheFile)) {
96
            throw new \RuntimeException(
97
                sprintf(
98
                    'The cache file %s is not writable',
99
                    $this->cacheFile
100
                )
101
            );
102
        }
103
104
        $result = file_put_contents(
105
            $this->cacheFile,
106
            sprintf(self::CACHE_TEMPLATE, var_export($value, true)),
107
            LOCK_EX
108
        );
109
110
        if ($result === false) {
111
            throw new \RuntimeException(
112
                sprintf(
113
                    'Can\'t write file "%s"',
114
                    $this->cacheFile
115
                )
116
            );
117
        }
118
    }
119
120
    public function delete($key): bool
121
    {
122
        return unlink($this->cacheFile);
123
    }
124
125
    public function clear(): bool
126
    {
127
        return unlink($this->cacheFile);
128
    }
129
130
    public function has($key): bool
131
    {
132
        return file_exists($this->cacheFile);
133
    }
134
135
    public function setMultiple($values, $ttl = null)
136
    {
137
        throw new \RuntimeException('Method is not implemented');
138
    }
139
140
    public function getMultiple($keys, $default = null)
141
    {
142
        throw new \RuntimeException('Method is not implemented');
143
    }
144
145
    public function deleteMultiple($keys)
146
    {
147
        throw new \RuntimeException('Method is not implemented');
148
    }
149
}
150