Completed
Push — github-actions ( 673aea...a81297 )
by Samuel
01:18
created

OpcacheProxy   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 140
ccs 37
cts 37
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getFunctions() 0 12 1
A setAdapter() 0 4 1
A opcache_compile_file() 0 10 1
A opcache_get_configuration() 0 7 1
A opcache_get_status() 0 10 1
A opcache_invalidate() 0 11 1
A opcache_reset() 0 10 1
A opcache_version() 0 7 1
1
<?php
2
3
/*
4
 * This file is part of CacheTool.
5
 *
6
 * (c) Samuel Gordalina <[email protected]>
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 CacheTool\Proxy;
13
14
use CacheTool\Adapter\AbstractAdapter;
15
use CacheTool\Code;
16
17
class OpcacheProxy implements ProxyInterface
18
{
19
    /**
20
     * @var AbstractAdapter
21
     */
22
    protected $adapter;
23
24
    /**
25
     * {@inheritdoc}
26
     */
27 15
    public function getFunctions()
28
    {
29
        return [
30 15
            'opcache_compile_file',
31
            'opcache_get_configuration',
32
            'opcache_get_status',
33
            'opcache_invalidate',
34
            'opcache_reset',
35
36
            'opcache_version'
37
        ];
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 15
    public function setAdapter(AbstractAdapter $adapter)
44
    {
45 15
        $this->adapter = $adapter;
46 15
    }
47
48
    /**
49
     * Compiles and caches a PHP script without executing it
50
     *
51
     * This function compiles a PHP script and adds it to the opcode cache without executing it. This can be used to
52
     * prime the cache after a Web server restart by pre-caching files that will be included in later requests.
53
     *
54
     * @since  5.5.5
55
     * @since  7.0.2
56
     * @param  string $file The path to the PHP script to be compiled.
57
     * @return boolean      Returns TRUE if file was compiled successfully or FALSE on failure.
58
     */
59 1
    public function opcache_compile_file($file)
60
    {
61 1
        $code = new Code();
62 1
        $code->addStatement(sprintf(
63 1
            'return opcache_compile_file(%s);',
64 1
            var_export($file, true)
65
        ));
66
67 1
        return $this->adapter->run($code);
68
    }
69
70
    /**
71
     * Get configuration information about the cache
72
     *
73
     * @since  5.5.5
74
     * @since  7.0.2
75
     * @return array Returns an array of information, including ini, blacklist and version
76
     */
77 2
    public function opcache_get_configuration()
78
    {
79 2
        $code = new Code();
80 2
        $code->addStatement('return opcache_get_configuration();');
81
82 2
        return $this->adapter->run($code);
83
    }
84
85
    /**
86
     * Get status information about the cache
87
     *
88
     * @since  5.5.5
89
     * @since  7.0.2
90
     * @param  boolean $get_scripts Include script specific state information
91
     * @return array                Returns an array of information, optionally containing script specific state
92
     *                              information
93
     */
94 3
    public function opcache_get_status($get_scripts = true)
95
    {
96 3
        $code = new Code();
97 3
        $code->addStatement(sprintf(
98 3
            'return opcache_get_status(%s);',
99 3
            var_export($get_scripts, true)
100
        ));
101
102 3
        return $this->adapter->run($code);
103
    }
104
105
    /**
106
     * Get status information about the cache
107
     *
108
     * @since  5.5.0
109
     * @since  7.0.0
110
     * @param  string  $script The path to the script being invalidated.
111
     * @param  boolean $force  If set to TRUE, the script will be invalidated regardless of whether invalidation is
112
     *                         necessary.
113
     * @return boolean         Returns TRUE if the opcode cache for script was invalidated or if there was nothing to
114
     *                         invalidate, or FALSE if the opcode cache is disabled.
115
     */
116 1
    public function opcache_invalidate($script, $force = false)
117
    {
118 1
        $code = new Code();
119 1
        $code->addStatement(sprintf(
120 1
            'return opcache_invalidate(%s, %s);',
121 1
            var_export($script, true),
122 1
            var_export($force, true)
123
        ));
124
125 1
        return $this->adapter->run($code);
126
    }
127
128
    /**
129
     * Resets the contents of the opcode cache
130
     *
131
     * @since  5.5.0
132
     * @since  7.0.0
133
     * @return boolean Returns TRUE if the opcode cache was reset, or FALSE if the opcode cache is disabled.
134
     */
135 2
    public function opcache_reset()
136
    {
137
        // Avoid using the opcache_reset() return value to workaround PHP bug
138
        // https://bugs.php.net/bug.php?id=71621
139 2
        $code = new Code();
140 2
        $code->addStatement('opcache_reset();');
141 2
        $code->addStatement('return true;');
142
143 2
        return $this->adapter->run($code);
144
    }
145
146
    /**
147
     * @return string
148
     */
149 1
    public function opcache_version()
150
    {
151 1
        $code = new Code();
152 1
        $code->addStatement('return phpversion("Zend OPcache");');
153
154 1
        return $this->adapter->run($code);
155
    }
156
}
157