Completed
Push — master ( 17cc54...e14e07 )
by Loban
01:13
created

PhpFileStorage::forceScriptCache()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * @link https://github.com/LAV45/yii2-settings
4
 * @copyright Copyright (c) 2016 LAV45
5
 * @author Alexey Loban <[email protected]>
6
 * @license http://opensource.org/licenses/BSD-3-Clause
7
 */
8
9
namespace lav45\settings\storage;
10
11
use yii\helpers\VarDumper;
12
13
/**
14
 * Class PhpFileStorage
15
 * @package lav45\settings\storage
16
 */
17
class PhpFileStorage extends FileStorage
18
{
19
    /**
20
     * @var string settings file suffix.
21
     */
22
    public $fileSuffix = '.php';
23
24
    /**
25
     * @param string $key
26
     * @return mixed
27
     */
28 3
    public function getValue($key)
29
    {
30 3
        $fileName = $this->getFile($key);
31 3
        if (file_exists($fileName)) {
32 3
            return include $fileName;
33
        }
34 3
        return false;
35
    }
36
37
    /**
38
     * @param string $key
39
     * @param mixed $value
40
     * @return bool
41
     */
42 3
    public function setValue($key, $value)
43
    {
44 3
        $value = is_string($value) ? "'{$value}'" : VarDumper::export($value);
45 3
        $value = "<?php\nreturn {$value};\n";
46
47 3
        $result = parent::setValue($key, $value);
48
49 3
        if ($result === true) {
50 3
            $fileName = $this->getFile($key);
51 3
            $this->forceScriptCache($fileName);
52
        }
53
54 3
        return $result;
55
    }
56
57
    /**
58
     * Forcibly caches data of this file in OPCache or APC.
59
     * @param string $fileName file name.
60
     * @since 1.0.4
61
     */
62 3
    protected function forceScriptCache($fileName)
63
    {
64 3
        if (function_exists('opcache_invalidate')) {
65
            opcache_invalidate($fileName, true); // @codeCoverageIgnore
66
        }
67 3
    }
68
}
69