[CLEANUP] The correct case must be used for standard PHP types in phpdoc
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Service / OpcodeCacheService.php
1 <?php
2 namespace TYPO3\CMS\Core\Service;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * Class with helper functions for clearing the PHP opcache.
19 * It auto detects the opcache system and invalidates/resets it.
20 * http://forge.typo3.org/issues/55252
21 * Supported opcaches are: OPcache >= 7.0 (PHP 5.5)
22 */
23 class OpcodeCacheService
24 {
25 /**
26 * Returns all supported and active opcaches
27 *
28 * @return array Array filled with supported and active opcaches
29 */
30 public function getAllActive()
31 {
32 $supportedCaches = [
33 // The ZendOpcache aka OPcache since PHP 5.5
34 // http://php.net/manual/de/book.opcache.php
35 'OPcache' => [
36 'active' => extension_loaded('Zend OPcache') && ini_get('opcache.enable') === '1',
37 'version' => phpversion('Zend OPcache'),
38 'canReset' => true, // opcache_reset() ... it seems that it doesn't reset for current run.
39 // From documentation this function exists since first version (7.0.0) but from Changelog
40 // this function exists since OPcache 7.0.2
41 // http://pecl.php.net/package-changelog.php?package=ZendOpcache&release=7.0.2
42 // PHP 7.0 onward is delivered minimum OPcache 7.0.6-dev
43 'canInvalidate' => true,
44 'error' => false,
45 'clearCallback' => function ($fileAbsPath) {
46 if ($fileAbsPath !== null) {
47 opcache_invalidate($fileAbsPath);
48 } else {
49 opcache_reset();
50 }
51 }
52 ],
53 ];
54
55 $activeCaches = [];
56 foreach ($supportedCaches as $opcodeCache => $properties) {
57 if ($properties['active']) {
58 $activeCaches[$opcodeCache] = $properties;
59 }
60 }
61 return $activeCaches;
62 }
63
64 /**
65 * Clears a file from an opcache, if one exists.
66 *
67 * @param string|null $fileAbsPath The file as absolute path to be cleared or NULL to clear completely.
68 */
69 public function clearAllActive($fileAbsPath = null)
70 {
71 foreach ($this->getAllActive() as $properties) {
72 $callback = $properties['clearCallback'];
73 $callback($fileAbsPath);
74 }
75 }
76 }