Passed
Push — master ( e5ef53...8f2fe5 )
by
unknown
17:09
created

OpcodeCacheService::clearAllActive()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace TYPO3\CMS\Core\Service;
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
/**
19
 * Class with helper functions for clearing the PHP opcache.
20
 * It auto detects the opcache system and invalidates/resets it.
21
 * http://forge.typo3.org/issues/55252
22
 * Supported opcaches are: OPcache >= 7.0 (PHP 5.5)
23
 */
24
class OpcodeCacheService
25
{
26
    /**
27
     * Returns all supported and active opcaches
28
     *
29
     * @return array Array filled with supported and active opcaches
30
     */
31
    public function getAllActive()
32
    {
33
        $supportedCaches = [
34
            'OPcache' => [
35
                'active' => extension_loaded('Zend OPcache') && ini_get('opcache.enable') === '1',
36
                'version' => phpversion('Zend OPcache'),
37
                'warning' => self::isClearable() ? false : 'Either opcache_invalidate or opcache_reset are disabled in this installation. Clearing will not work.',
38
                'clearCallback' => static function ($fileAbsPath) {
39
                    if (self::isClearable()) {
40
                        if ($fileAbsPath !== null) {
41
                            opcache_invalidate($fileAbsPath);
42
                        } else {
43
                            opcache_reset();
44
                        }
45
                    }
46
                },
47
            ],
48
        ];
49
50
        $activeCaches = [];
51
        foreach ($supportedCaches as $opcodeCache => $properties) {
52
            if ($properties['active']) {
53
                $activeCaches[$opcodeCache] = $properties;
54
            }
55
        }
56
        return $activeCaches;
57
    }
58
59
    /**
60
     * Clears a file from an opcache, if one exists.
61
     *
62
     * @param string|null $fileAbsPath The file as absolute path to be cleared or NULL to clear completely.
63
     */
64
    public function clearAllActive($fileAbsPath = null)
65
    {
66
        foreach ($this->getAllActive() as $properties) {
67
            $callback = $properties['clearCallback'];
68
            $callback($fileAbsPath);
69
        }
70
    }
71
72
    /**
73
     * @return bool
74
     */
75
    protected static function isClearable(): bool
76
    {
77
        $disabled = explode(',', ini_get('disable_functions'));
78
        return !(in_array('opcache_invalidate', $disabled, true) || in_array('opcache_reset', $disabled, true));
79
    }
80
}
81