[!!!][TASK] Migrate OpcodeCacheUtility to a service class 66/40766/4
authorMarkus Klein <markus.klein@typo3.org>
Tue, 30 Jun 2015 10:42:50 +0000 (12:42 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Fri, 3 Jul 2015 11:44:48 +0000 (13:44 +0200)
There is no good reason for this internal class to be a
static utility.
Move the code to a simple service class.

Resolves: #63838
Releases: master
Change-Id: Iff4d538b9ae01e64e9c3b4aa9b5a913d9fabd851
Reviewed-on: http://review.typo3.org/40766
Reviewed-by: Mathias Brodala <mbrodala@pagemachine.de>
Reviewed-by: Alexander Opitz <opitz.alexander@googlemail.com>
Tested-by: Alexander Opitz <opitz.alexander@googlemail.com>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
typo3/sysext/core/Classes/Cache/Backend/FileBackend.php
typo3/sysext/core/Classes/Cache/Backend/SimpleFileBackend.php
typo3/sysext/core/Classes/Configuration/ConfigurationManager.php
typo3/sysext/core/Classes/Package/PackageManager.php
typo3/sysext/core/Classes/Service/OpcodeCacheService.php [new file with mode: 0644]
typo3/sysext/core/Classes/Utility/GeneralUtility.php
typo3/sysext/core/Classes/Utility/OpcodeCacheUtility.php [deleted file]
typo3/sysext/core/Documentation/Changelog/master/Breaking-63838-ChangedOpcodeCacheUtilityBeingAServiceClass.rst [new file with mode: 0644]
typo3/sysext/install/Classes/Controller/Action/Tool/ImportantActions.php
typo3/sysext/install/Classes/Service/CoreUpdateService.php
typo3/sysext/install/Classes/SystemEnvironment/Check.php

index 3eaee7f..3afac38 100644 (file)
@@ -13,6 +13,8 @@ namespace TYPO3\CMS\Core\Cache\Backend;
  *
  * The TYPO3 project - inspiring people to share!
  */
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Core\Service\OpcodeCacheService;
 
 /**
  * A caching backend which stores cache entries in files
@@ -166,7 +168,7 @@ class FileBackend extends \TYPO3\CMS\Core\Cache\Backend\SimpleFileBackend implem
                        throw new \TYPO3\CMS\Core\Cache\Exception('The cache file "' . $cacheEntryPathAndFilename . '" could not be written.', 1222361632);
                }
                if ($this->cacheEntryFileExtension === '.php') {
-                       \TYPO3\CMS\Core\Utility\OpcodeCacheUtility::clearAllActive($cacheEntryPathAndFilename);
+                       GeneralUtility::makeInstance(OpcodeCacheService::class)->clearAllActive($cacheEntryPathAndFilename);
                }
        }
 
index 0cd23a1..cd8236e 100644 (file)
@@ -10,6 +10,9 @@ namespace TYPO3\CMS\Core\Cache\Backend;
  *                                                                        *
  * The TYPO3 project - inspiring people to share!                         *
  *                                                                        */
+use TYPO3\CMS\Core\Service\OpcodeCacheService;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+
 /**
  * A caching backend which stores cache entries in files, but does not support or
  * care about expiry times and tags.
@@ -243,7 +246,7 @@ class SimpleFileBackend extends \TYPO3\CMS\Core\Cache\Backend\AbstractBackend im
                $cacheEntryPathAndFilename = $this->cacheDirectory . $entryIdentifier . $this->cacheEntryFileExtension;
                rename($temporaryCacheEntryPathAndFilename, $cacheEntryPathAndFilename);
                if ($this->cacheEntryFileExtension === '.php') {
-                       \TYPO3\CMS\Core\Utility\OpcodeCacheUtility::clearAllActive($cacheEntryPathAndFilename);
+                       GeneralUtility::makeInstance(OpcodeCacheService::class)->clearAllActive($cacheEntryPathAndFilename);
                }
        }
 
index 4083dcc..539029e 100644 (file)
@@ -16,7 +16,7 @@ namespace TYPO3\CMS\Core\Configuration;
 
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Core\Utility\ArrayUtility;
-use TYPO3\CMS\Core\Utility\OpcodeCacheUtility;
+use TYPO3\CMS\Core\Service\OpcodeCacheService;
 
 /**
  * Handle loading and writing of global and local (instance specific)
@@ -315,7 +315,7 @@ class ConfigurationManager {
                        TRUE
                );
 
-               OpcodeCacheUtility::clearAllActive($localConfigurationFile);
+               GeneralUtility::makeInstance(OpcodeCacheService::class)->clearAllActive($localConfigurationFile);
 
                return $result;
        }
index f597320..437acba 100644 (file)
@@ -16,6 +16,7 @@ namespace TYPO3\CMS\Core\Package;
 
 use TYPO3\CMS\Core\Compatibility\LoadedExtensionArrayElement;
 use TYPO3\CMS\Core\Core\ClassLoadingInformation;
+use TYPO3\CMS\Core\Service\OpcodeCacheService;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Core\Utility\PathUtility;
 
@@ -819,7 +820,8 @@ class PackageManager implements \TYPO3\CMS\Core\SingletonInterface {
                GeneralUtility::writeFile($this->packageStatesPathAndFilename, $packageStatesCode, TRUE);
 
                $this->initializeCompatibilityLoadedExtArray();
-               \TYPO3\CMS\Core\Utility\OpcodeCacheUtility::clearAllActive($this->packageStatesPathAndFilename);
+
+               GeneralUtility::makeInstance(OpcodeCacheService::class)->clearAllActive($this->packageStatesPathAndFilename);
        }
 
        /**
diff --git a/typo3/sysext/core/Classes/Service/OpcodeCacheService.php b/typo3/sysext/core/Classes/Service/OpcodeCacheService.php
new file mode 100644 (file)
index 0000000..61418a8
--- /dev/null
@@ -0,0 +1,111 @@
+<?php
+namespace TYPO3\CMS\Core\Service;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+/**
+ * Class with helper functions for clearing the PHP opcache.
+ * It auto detects the opcache system and invalidates/resets it.
+ * http://forge.typo3.org/issues/55252
+ * Supported opcaches are: OPcache >= 7.0 (PHP 5.5), WinCache, XCache >= 3.0.1
+ *
+ * @author Alexander Opitz <opitz@pluspol-interactive.de>
+ */
+class OpcodeCacheService {
+
+       /**
+        * Returns all supported and active opcaches
+        *
+        * @return array Array filled with supported and active opcaches
+        */
+       public function getAllActive() {
+               $xcVersion = phpversion('xcache');
+
+               $supportedCaches = array(
+                       // The ZendOpcache aka OPcache since PHP 5.5
+                       // http://php.net/manual/de/book.opcache.php
+                       'OPcache' => array(
+                               'active' => extension_loaded('Zend OPcache') && ini_get('opcache.enable') === '1',
+                               'version' => phpversion('Zend OPcache'),
+                               'canReset' => TRUE, // opcache_reset() ... it seems that it doesn't reset for current run.
+                               // From documentation this function exists since first version (7.0.0) but from Changelog
+                               // this function exists since 7.0.2
+                               // http://pecl.php.net/package-changelog.php?package=ZendOpcache&release=7.0.2
+                               'canInvalidate' => function_exists('opcache_invalidate'),
+                               'error' => FALSE,
+                               'clearCallback' => function ($fileAbsPath) {
+                                       if ($fileAbsPath !== NULL && function_exists('opcache_invalidate')) {
+                                               opcache_invalidate($fileAbsPath);
+                                       } else {
+                                               opcache_reset();
+                                       }
+                               }
+                       ),
+
+                       // http://www.php.net/manual/de/book.wincache.php
+                       'WinCache' => array(
+                               'active' => extension_loaded('wincache') && ini_get('wincache.ocenabled') === '1',
+                               'version' => phpversion('wincache'),
+                               'canReset' => TRUE,
+                               'canInvalidate' => TRUE, // wincache_refresh_if_changed()
+                               'error' => FALSE,
+                               'clearCallback' => function ($fileAbsPath) {
+                                       if ($fileAbsPath !== NULL) {
+                                               wincache_refresh_if_changed(array($fileAbsPath));
+                                       } else {
+                                               // No argument means refreshing all.
+                                               wincache_refresh_if_changed();
+                                       }
+                               }
+                       ),
+
+                       // http://xcache.lighttpd.net/
+                       'XCache' => array(
+                               'active' => extension_loaded('xcache'),
+                               'version' => $xcVersion,
+                               'canReset' => !ini_get('xcache.admin.enable_auth'), // xcache_clear_cache()
+                               'canInvalidate' => FALSE,
+                               'error' => FALSE,
+                               'clearCallback' => function ($fileAbsPath) {
+                                       if (!ini_get('xcache.admin.enable_auth')) {
+                                               xcache_clear_cache(XC_TYPE_PHP);
+                                       }
+                               }
+                       ),
+               );
+
+               $activeCaches = array();
+               foreach ($supportedCaches as $opcodeCache => $properties) {
+                       if ($properties['active']) {
+                               $activeCaches[$opcodeCache] = $properties;
+                       }
+               }
+               return $activeCaches;
+       }
+
+       /**
+        * Clears a file from an opcache, if one exists.
+        *
+        * @param string|NULL $fileAbsPath The file as absolute path to be cleared or NULL to clear completely.
+        *
+        * @return void
+        */
+       public function clearAllActive($fileAbsPath = NULL) {
+               foreach ($this->getAllActive() as $properties) {
+                       $callback = $properties['clearCallback'];
+                       $callback($fileAbsPath);
+               }
+       }
+
+}
index dcf88f7..fc42a6a 100755 (executable)
@@ -17,6 +17,7 @@ namespace TYPO3\CMS\Core\Utility;
 use TYPO3\CMS\Core\Core\ApplicationContext;
 use TYPO3\CMS\Core\Core\ClassLoadingInformation;
 use TYPO3\CMS\Core\Imaging\GraphicalFunctions;
+use TYPO3\CMS\Core\Service\OpcodeCacheService;
 use TYPO3\CMS\Core\SingletonInterface;
 use TYPO3\CMS\Frontend\Page\PageRepository;
 
@@ -2819,7 +2820,9 @@ Connection: close
                if (is_dir($directory)) {
                        $temporaryDirectory = rtrim($directory, '/') . '.' . uniqid('remove', TRUE) . '/';
                        if (rename($directory, $temporaryDirectory)) {
-                               $flushOpcodeCache && OpcodeCacheUtility::clearAllActive($directory);
+                               if ($flushOpcodeCache) {
+                                       GeneralUtility::makeInstance(OpcodeCacheService::class)->clearAllActive($directory);
+                               }
                                if ($keepOriginalDirectory) {
                                        self::mkdir($directory);
                                }
diff --git a/typo3/sysext/core/Classes/Utility/OpcodeCacheUtility.php b/typo3/sysext/core/Classes/Utility/OpcodeCacheUtility.php
deleted file mode 100644 (file)
index 64bc6e2..0000000
+++ /dev/null
@@ -1,133 +0,0 @@
-<?php
-namespace TYPO3\CMS\Core\Utility;
-
-/*
- * This file is part of the TYPO3 CMS project.
- *
- * It is free software; you can redistribute it and/or modify it under
- * the terms of the GNU General Public License, either version 2
- * of the License, or any later version.
- *
- * For the full copyright and license information, please read the
- * LICENSE.txt file that was distributed with this source code.
- *
- * The TYPO3 project - inspiring people to share!
- */
-
-/**
- * Class with helper functions for clearing the PHP opcache.
- * It auto detects the opcache system and invalidates/resets it.
- * http://forge.typo3.org/issues/55252
- * Supported opcaches are: OPcache >= 7.0 (PHP 5.5), WinCache, XCache >= 3.0.1
- *
- * @author Alexander Opitz <opitz@pluspol-interactive.de>
- */
-class OpcodeCacheUtility {
-
-       /**
-        * All supported cache types
-        * @var array|null
-        */
-       static protected $supportedCaches = NULL;
-
-       /**
-        * Holds all currently active caches
-        * @var array|null
-        */
-       static protected $activeCaches = NULL;
-
-       /**
-        * Initialize the cache properties
-        */
-       static protected function initialize() {
-               $xcVersion = phpversion('xcache');
-
-               static::$supportedCaches = array(
-                       // The ZendOpcache aka OPcache since PHP 5.5
-                       // http://php.net/manual/de/book.opcache.php
-                       'OPcache' => array(
-                               'active' => extension_loaded('Zend OPcache') && ini_get('opcache.enable') === '1',
-                               'version' => phpversion('Zend OPcache'),
-                               'canReset' => TRUE, // opcache_reset() ... it seems that it doesn't reset for current run.
-                               // From documentation this function exists since first version (7.0.0) but from Changelog
-                               // this function exists since 7.0.2
-                               // http://pecl.php.net/package-changelog.php?package=ZendOpcache&release=7.0.2
-                               'canInvalidate' => function_exists('opcache_invalidate'),
-                               'error' => FALSE,
-                               'clearCallback' => function ($fileAbsPath) {
-                                       if ($fileAbsPath !== NULL && function_exists('opcache_invalidate')) {
-                                               opcache_invalidate($fileAbsPath);
-                                       } else {
-                                               opcache_reset();
-                                       }
-                               }
-                       ),
-
-                       // http://www.php.net/manual/de/book.wincache.php
-                       'WinCache' => array(
-                               'active' => extension_loaded('wincache') && ini_get('wincache.ocenabled') === '1',
-                               'version' => phpversion('wincache'),
-                               'canReset' => TRUE,
-                               'canInvalidate' => TRUE, // wincache_refresh_if_changed()
-                               'error' => FALSE,
-                               'clearCallback' => function ($fileAbsPath) {
-                                       if ($fileAbsPath !== NULL) {
-                                               wincache_refresh_if_changed(array($fileAbsPath));
-                                       } else {
-                                               // No argument means refreshing all.
-                                               wincache_refresh_if_changed();
-                                       }
-                               }
-                       ),
-
-                       // http://xcache.lighttpd.net/
-                       'XCache' => array(
-                               'active' => extension_loaded('xcache'),
-                               'version' => $xcVersion,
-                               'canReset' => !ini_get('xcache.admin.enable_auth'), // xcache_clear_cache()
-                               'canInvalidate' => FALSE,
-                               'error' => FALSE,
-                               'clearCallback' => function ($fileAbsPath) {
-                                       if (!ini_get('xcache.admin.enable_auth')) {
-                                               xcache_clear_cache(XC_TYPE_PHP);
-                                       }
-                               }
-                       ),
-               );
-
-               static::$activeCaches = array();
-               // Cache the active ones
-               foreach (static::$supportedCaches as $opcodeCache => $properties) {
-                       if ($properties['active']) {
-                               static::$activeCaches[$opcodeCache] = $properties;
-                       }
-               }
-       }
-
-       /**
-        * Clears a file from an opcache, if one exists.
-        *
-        * @param string|NULL $fileAbsPath The file as absolute path to be cleared or NULL to clear completely.
-        *
-        * @return void
-        */
-       static public function clearAllActive($fileAbsPath = NULL) {
-               foreach (static::getAllActive() as $properties) {
-                       $callback = $properties['clearCallback'];
-                       $callback($fileAbsPath);
-               }
-       }
-
-       /**
-        * Returns all supported and active opcaches
-        *
-        * @return array Array filled with supported and active opcaches
-        */
-       static public function getAllActive() {
-               if (static::$activeCaches === NULL) {
-                       static::initialize();
-               }
-               return static::$activeCaches;
-       }
-
-}
diff --git a/typo3/sysext/core/Documentation/Changelog/master/Breaking-63838-ChangedOpcodeCacheUtilityBeingAServiceClass.rst b/typo3/sysext/core/Documentation/Changelog/master/Breaking-63838-ChangedOpcodeCacheUtilityBeingAServiceClass.rst
new file mode 100644 (file)
index 0000000..079cf95
--- /dev/null
@@ -0,0 +1,32 @@
+===================================================================
+Breaking: #63838 - Changed OpcodeCacheUtility being a service class
+===================================================================
+
+Description
+===========
+
+The ``OpcodeCacheUtility`` has been migrated to a service class called ``OpcodeCacheService``, all methods are not static anymore.
+
+
+Impact
+======
+
+Calling ``OpcodeCacheUtility`` will throw a fatal error.
+
+
+Affected Installations
+======================
+
+All third-party extensions using the utility class will be affected.
+
+
+Migration
+=========
+
+Create an instance of ``OpcodeCacheService`` and call its method by the object operator ``->``.
+
+Example:
+
+.. code-block:: [php]
+
+       GeneralUtility::makeInstance(OpcodeCacheService::class)->clearAllActive($cacheEntryPathAndFilename);
index 2c77e26..20e8603 100644 (file)
@@ -16,7 +16,7 @@ namespace TYPO3\CMS\Install\Controller\Action\Tool;
 
 use TYPO3\CMS\Install\Controller\Action;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
-use TYPO3\CMS\Core\Utility\OpcodeCacheUtility;
+use TYPO3\CMS\Core\Service\OpcodeCacheService;
 
 /**
  * Handle important actions
@@ -67,6 +67,8 @@ class ImportantActions extends Action\AbstractAction {
 
                $operatingSystem = TYPO3_OS === 'WIN' ? 'Windows' : 'Unix';
 
+               $opcodeCacheService = GeneralUtility::makeInstance(OpcodeCacheService::class);
+
                /** @var \TYPO3\CMS\Install\Service\CoreUpdateService $coreUpdateService */
                $coreUpdateService = $this->objectManager->get(\TYPO3\CMS\Install\Service\CoreUpdateService::class);
                $this->view
@@ -82,7 +84,7 @@ class ImportantActions extends Action\AbstractAction {
                        ->assign('extensionCompatibilityTesterProtocolFile', GeneralUtility::getIndpEnv('TYPO3_SITE_URL') . 'typo3temp/ExtensionCompatibilityTester.txt')
                        ->assign('extensionCompatibilityTesterErrorProtocolFile', GeneralUtility::getIndpEnv('TYPO3_SITE_URL') . 'typo3temp/ExtensionCompatibilityTesterErrors.json')
                        ->assign('extensionCompatibilityTesterMessages', $this->getExtensionCompatibilityTesterMessages())
-                       ->assign('listOfOpcodeCaches', OpcodeCacheUtility::getAllActive());
+                       ->assign('listOfOpcodeCaches', $opcodeCacheService->getAllActive());
 
                return $this->view->render();
        }
@@ -162,8 +164,7 @@ class ImportantActions extends Action\AbstractAction {
         * @return \TYPO3\CMS\Install\Status\StatusInterface
         */
        protected function clearOpcodeCache() {
-               /** @var \TYPO3\CMS\Install\Service\ClearCacheService $clearCacheService */
-               OpcodeCacheUtility::clearAllActive();
+               GeneralUtility::makeInstance(OpcodeCacheService::class)->clearAllActive();
                $message = $this->objectManager->get(\TYPO3\CMS\Install\Status\OkStatus::class);
                $message->setTitle('Successfully cleared all available opcode caches');
                return $message;
index 2dbce96..6d82ad5 100644 (file)
@@ -15,7 +15,7 @@ namespace TYPO3\CMS\Install\Service;
  */
 
 use TYPO3\CMS\Core\Utility\GeneralUtility;
-use TYPO3\CMS\Core\Utility\OpcodeCacheUtility;
+use TYPO3\CMS\Core\Service\OpcodeCacheService;
 use TYPO3\CMS\Core\Utility\PathUtility;
 use TYPO3\CMS\Install\FolderStructure\DefaultFactory;
 use TYPO3\CMS\Install\Service\Exception\RemoteFetchException;
@@ -484,7 +484,7 @@ class CoreUpdateService {
                                }
                                $symlinkResult = symlink($newCoreLocation, $this->symlinkToCoreFiles);
                                if ($symlinkResult) {
-                                       OpcodeCacheUtility::clearAllActive();
+                                       GeneralUtility::makeInstance(OpcodeCacheService::class)->clearAllActive();
                                } else {
                                        $success = FALSE;
                                        /** @var $message StatusInterface */
index 3d6f6b7..b2e0af9 100644 (file)
@@ -14,6 +14,8 @@ namespace TYPO3\CMS\Install\SystemEnvironment;
  * The TYPO3 project - inspiring people to share!
  */
 
+use TYPO3\CMS\Core\Service\OpcodeCacheService;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Install\Status;
 
 /**
@@ -873,7 +875,7 @@ class Check {
        protected function checkSomePhpOpcodeCacheIsLoaded() {
                // Link to our wiki page, so we can update opcode cache issue information independent of TYPO3 CMS releases.
                $wikiLink = 'For more information take a look in our wiki ' . TYPO3_URL_WIKI_OPCODECACHE . '.';
-               $opcodeCaches = \TYPO3\CMS\Core\Utility\OpcodeCacheUtility::getAllActive();
+               $opcodeCaches = GeneralUtility::makeInstance(OpcodeCacheService::class)->getAllActive();
                if (count($opcodeCaches) === 0) {
                        // Set status to notice. It needs to be notice so email won't be triggered.
                        $status = new Status\NoticeStatus();