diff --git a/typo3/sysext/core/Classes/Cache/Frontend/PhpFrontend.php b/typo3/sysext/core/Classes/Cache/Frontend/PhpFrontend.php index 84d1a1cf9a0e..8c2cd7e6780d 100644 --- a/typo3/sysext/core/Classes/Cache/Frontend/PhpFrontend.php +++ b/typo3/sysext/core/Classes/Cache/Frontend/PhpFrontend.php @@ -23,7 +23,7 @@ * This file is a backport from FLOW3 * @api */ -class PhpFrontend extends StringFrontend +class PhpFrontend extends AbstractFrontend { /** * Constructs the cache @@ -64,6 +64,45 @@ public function set($entryIdentifier, $sourceCode, array $tags = [], $lifetime = $this->backend->set($entryIdentifier, $sourceCode, $tags, $lifetime); } + /** + * Finds and returns a variable value from the cache. + * + * @param string $entryIdentifier Identifier of the cache entry to fetch + * @return string The value + * @throws \InvalidArgumentException if the cache identifier is not valid + * @api + */ + public function get($entryIdentifier) + { + if (!$this->isValidEntryIdentifier($entryIdentifier)) { + throw new \InvalidArgumentException('"' . $entryIdentifier . '" is not a valid cache entry identifier.', 1233057753); + } + return $this->backend->get($entryIdentifier); + } + + /** + * Finds and returns all cache entries which are tagged by the specified tag. + * + * @param string $tag The tag to search for + * @return array An array with the content of all matching entries. An empty array if no entries matched + * @throws \InvalidArgumentException if the tag is not valid + * @api + * @deprecated since TYPO3 v9, Avoid using this method since it is not compliant to PSR-6 + */ + public function getByTag($tag) + { + trigger_error('This method will be removed in TYPO3 v10. Avoid using this method since it is not compliant to PSR-6.', E_USER_DEPRECATED); + if (!$this->isValidTag($tag)) { + throw new \InvalidArgumentException('"' . $tag . '" is not a valid tag for a cache entry.', 1233057773); + } + $entries = []; + $identifiers = $this->backend->findIdentifiersByTag($tag); + foreach ($identifiers as $identifier) { + $entries[] = $this->backend->get($identifier); + } + return $entries; + } + /** * Loads PHP code from the cache and require_onces it right away. * diff --git a/typo3/sysext/core/Classes/Cache/Frontend/StringFrontend.php b/typo3/sysext/core/Classes/Cache/Frontend/StringFrontend.php index 3fb376a36ff9..8d6803a11a59 100644 --- a/typo3/sysext/core/Classes/Cache/Frontend/StringFrontend.php +++ b/typo3/sysext/core/Classes/Cache/Frontend/StringFrontend.php @@ -14,6 +14,7 @@ * The TYPO3 project - inspiring people to share! */ +use TYPO3\CMS\Core\Cache\Backend\BackendInterface; use TYPO3\CMS\Core\Cache\Exception\InvalidDataException; /** @@ -21,9 +22,27 @@ * * This file is a backport from FLOW3 * @api + * @deprecated since TYPO3 v9, will be removed in TYPO3 v10 - use VariableFrontend instead. */ class StringFrontend extends AbstractFrontend { + /** + * @param string $identifier + * @param BackendInterface $backend + */ + public function __construct($identifier, BackendInterface $backend) + { + trigger_error( + sprintf( + 'Usage of class %s will be removed in TYPO3 v10.0, use %s instead', + static::class, + VariableFrontend::class + ), + E_USER_DEPRECATED + ); + parent::__construct($identifier, $backend); + } + /** * Saves the value of a PHP variable in the cache. * diff --git a/typo3/sysext/core/Documentation/Changelog/master/Deprecation-81434-StringCacheFrontendDeprecated.rst b/typo3/sysext/core/Documentation/Changelog/master/Deprecation-81434-StringCacheFrontendDeprecated.rst new file mode 100644 index 000000000000..d59abeb089d6 --- /dev/null +++ b/typo3/sysext/core/Documentation/Changelog/master/Deprecation-81434-StringCacheFrontendDeprecated.rst @@ -0,0 +1,32 @@ +.. include:: ../../Includes.txt + +====================================================== +Deprecation: #81434 - String Cache Frontend Deprecated +====================================================== + +See :issue:`81434` + +Description +=========== + +The ``StringFrontend`` cache frontend has been deprecated in favor of VariableFrontend. + + +Impact +====== + +The ``TYPO3\CMS\Core\Cache\Frontend\StringFrontend`` class is deprecated. + + +Affected Installations +====================== + +Any TYPO3 installation which defines any custom cache using ``StringFrontend``. + + +Migration +========= + +Replace ``TYPO3\CMS\Core\Cache\Frontend\StringFrontend`` occurrences in cache configurations with ``TYPO3\CMS\Core\Cache\Frontend\VariableFrontend``. + +.. index:: PHP-API, NotScanned \ No newline at end of file diff --git a/typo3/sysext/core/Tests/Unit/Cache/Frontend/AbstractFrontendTest.php b/typo3/sysext/core/Tests/Unit/Cache/Frontend/AbstractFrontendTest.php index 7a6839d85567..1ec7fae0e281 100644 --- a/typo3/sysext/core/Tests/Unit/Cache/Frontend/AbstractFrontendTest.php +++ b/typo3/sysext/core/Tests/Unit/Cache/Frontend/AbstractFrontendTest.php @@ -31,7 +31,7 @@ public function theConstructorAcceptsValidIdentifiers() ->disableOriginalConstructor() ->getMock(); foreach (['x', 'someValue', '123fivesixseveneight', 'some&', 'ab_cd%', rawurlencode('resource://some/äöü$&% sadf'), str_repeat('x', 250)] as $identifier) { - $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class) + $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\VariableFrontend::class) ->setMethods(['__construct', 'get', 'set', 'has', 'remove', 'getByTag', 'flush', 'flushByTag', 'collectGarbage']) ->setConstructorArgs([$identifier, $mockBackend]) ->getMock(); @@ -49,7 +49,7 @@ public function theConstructorRejectsInvalidIdentifiers() ->getMock(); foreach (['', 'abc def', 'foo!', 'bar:', 'some/', 'bla*', 'one+', 'äöü', str_repeat('x', 251), 'x$', '\\a', 'b#'] as $identifier) { try { - $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class) + $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\VariableFrontend::class) ->setMethods(['__construct', 'get', 'set', 'has', 'remove', 'getByTag', 'flush', 'flushByTag', 'collectGarbage']) ->setConstructorArgs([$identifier, $mockBackend]) ->getMock(); @@ -70,7 +70,7 @@ public function flushCallsBackend() ->disableOriginalConstructor() ->getMock(); $backend->expects($this->once())->method('flush'); - $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class) + $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\VariableFrontend::class) ->setMethods(['__construct', 'get', 'set', 'has', 'remove', 'getByTag']) ->setConstructorArgs([$identifier, $backend]) ->getMock(); @@ -88,7 +88,7 @@ public function flushByTagRejectsInvalidTags() $identifier = 'someCacheIdentifier'; $backend = $this->createMock(\TYPO3\CMS\Core\Cache\Backend\TaggableBackendInterface::class); $backend->expects($this->never())->method('flushByTag'); - $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class) + $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\VariableFrontend::class) ->setMethods(['__construct', 'get', 'set', 'has', 'remove', 'getByTag']) ->setConstructorArgs([$identifier, $backend]) ->getMock(); @@ -107,7 +107,7 @@ public function flushByTagCallsBackendIfItIsATaggableBackend() ->disableOriginalConstructor() ->getMock(); $backend->expects($this->once())->method('flushByTag')->with($tag); - $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class) + $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\VariableFrontend::class) ->setMethods(['__construct', 'get', 'set', 'has', 'remove', 'getByTag']) ->setConstructorArgs([$identifier, $backend]) ->getMock(); @@ -126,7 +126,7 @@ public function flushByTagsCallsBackendIfItIsATaggableBackend() ->disableOriginalConstructor() ->getMock(); $backend->expects($this->once())->method('flushByTags')->with([$tag]); - $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class) + $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\VariableFrontend::class) ->setMethods(['__construct', 'get', 'set', 'has', 'remove', 'getByTag']) ->setConstructorArgs([$identifier, $backend]) ->getMock(); @@ -144,7 +144,7 @@ public function collectGarbageCallsBackend() ->disableOriginalConstructor() ->getMock(); $backend->expects($this->once())->method('collectGarbage'); - $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class) + $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\VariableFrontend::class) ->setMethods(['__construct', 'get', 'set', 'has', 'remove', 'getByTag']) ->setConstructorArgs([$identifier, $backend]) ->getMock(); @@ -158,7 +158,7 @@ public function invalidEntryIdentifiersAreRecognizedAsInvalid() { $identifier = 'someCacheIdentifier'; $backend = $this->createMock(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class); - $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class) + $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\VariableFrontend::class) ->setMethods(['__construct', 'get', 'set', 'has', 'remove', 'getByTag']) ->setConstructorArgs([$identifier, $backend]) ->getMock(); @@ -174,7 +174,7 @@ public function validEntryIdentifiersAreRecognizedAsValid() { $identifier = 'someCacheIdentifier'; $backend = $this->createMock(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class); - $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class) + $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\VariableFrontend::class) ->setMethods(['__construct', 'get', 'set', 'has', 'remove', 'getByTag']) ->setConstructorArgs([$identifier, $backend]) ->getMock(); @@ -190,7 +190,7 @@ public function invalidTagsAreRecognizedAsInvalid() { $identifier = 'someCacheIdentifier'; $backend = $this->createMock(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class); - $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class) + $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\VariableFrontend::class) ->setMethods(['__construct', 'get', 'set', 'has', 'remove', 'getByTag']) ->setConstructorArgs([$identifier, $backend]) ->getMock(); @@ -206,7 +206,7 @@ public function validTagsAreRecognizedAsValid() { $identifier = 'someCacheIdentifier'; $backend = $this->createMock(\TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class); - $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class) + $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\VariableFrontend::class) ->setMethods(['__construct', 'get', 'set', 'has', 'remove', 'getByTag']) ->setConstructorArgs([$identifier, $backend]) ->getMock(); diff --git a/typo3/sysext/core/Tests/Unit/Cache/Frontend/PhpFrontendTest.php b/typo3/sysext/core/Tests/Unit/Cache/Frontend/PhpFrontendTest.php index f06b688cf9ed..73f45868b549 100644 --- a/typo3/sysext/core/Tests/Unit/Cache/Frontend/PhpFrontendTest.php +++ b/typo3/sysext/core/Tests/Unit/Cache/Frontend/PhpFrontendTest.php @@ -28,9 +28,9 @@ class PhpFrontendTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase public function setChecksIfTheIdentifierIsValid() { $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionCode(1233057566); + $this->expectExceptionCode(1264023823); - $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class) + $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\PhpFrontend::class) ->setMethods(['isValidEntryIdentifier']) ->disableOriginalConstructor() ->getMock(); diff --git a/typo3/sysext/core/Tests/Unit/Cache/Frontend/VariableFrontendTest.php b/typo3/sysext/core/Tests/Unit/Cache/Frontend/VariableFrontendTest.php index aa5396976568..83d2e618f1b7 100644 --- a/typo3/sysext/core/Tests/Unit/Cache/Frontend/VariableFrontendTest.php +++ b/typo3/sysext/core/Tests/Unit/Cache/Frontend/VariableFrontendTest.php @@ -14,12 +14,12 @@ * The TYPO3 project - inspiring people to share! */ +use TYPO3\TestingFramework\Core\Unit\UnitTestCase; + /** - * Testcase for the variable cache frontend - * - * This file is a backport from FLOW3 + * Test case */ -class VariableFrontendTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase +class VariableFrontendTest extends UnitTestCase { /** * @test @@ -27,9 +27,9 @@ class VariableFrontendTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCas public function setChecksIfTheIdentifierIsValid() { $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionCode(1233057566); + $this->expectExceptionCode(1233058264); - $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class) + $cache = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\VariableFrontend::class) ->setMethods(['isValidEntryIdentifier']) ->disableOriginalConstructor() ->getMock(); diff --git a/typo3/sysext/core/Tests/Unit/Localization/LocalizationFactoryTest.php b/typo3/sysext/core/Tests/Unit/Localization/LocalizationFactoryTest.php index 18f24472b7f5..f0edd747230e 100644 --- a/typo3/sysext/core/Tests/Unit/Localization/LocalizationFactoryTest.php +++ b/typo3/sysext/core/Tests/Unit/Localization/LocalizationFactoryTest.php @@ -71,7 +71,7 @@ public function getParsedDataCallsLocalizationOverrideIfFileNotFoundExceptionIsT $languageStore = $this->getMockBuilder(\TYPO3\CMS\Core\Localization\LanguageStore::class) ->setMethods(['hasData', 'setConfiguration', 'getData', 'setData']) ->getMock(); - $cacheInstance = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class) + $cacheInstance = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\VariableFrontend::class) ->setMethods(['get', 'set']) ->disableOriginalConstructor() ->getMock(); diff --git a/typo3/sysext/core/Tests/Unit/Cache/Frontend/StringFrontendTest.php b/typo3/sysext/core/Tests/UnitDeprecated/Cache/Frontend/StringFrontendTest.php similarity index 98% rename from typo3/sysext/core/Tests/Unit/Cache/Frontend/StringFrontendTest.php rename to typo3/sysext/core/Tests/UnitDeprecated/Cache/Frontend/StringFrontendTest.php index 44101410cbec..7cd7b2113dee 100644 --- a/typo3/sysext/core/Tests/Unit/Cache/Frontend/StringFrontendTest.php +++ b/typo3/sysext/core/Tests/UnitDeprecated/Cache/Frontend/StringFrontendTest.php @@ -1,5 +1,5 @@ createMock(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class); + $cache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\VariableFrontend::class); $cache->expects($this->any())->method('getIdentifier')->will($this->returnValue('cache')); $cache->expects($this->atLeastOnce())->method('collectGarbage'); $mockCacheManager = new \TYPO3\CMS\Core\Cache\CacheManager(); @@ -56,7 +56,7 @@ public function executeCallsCollectGarbageOfConfiguredBackend() GeneralUtility::setSingletonInstance(\TYPO3\CMS\Core\Cache\CacheManager::class, $mockCacheManager); $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'] = [ 'cache' => [ - 'frontend' => \TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class, + 'frontend' => \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend::class, 'backend' => \TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class, ] ]; @@ -74,7 +74,7 @@ public function executeCallsCollectGarbageOfConfiguredBackend() */ public function executeDoesNotCallCollectGarbageOfNotConfiguredBackend() { - $cache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class); + $cache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\VariableFrontend::class); $cache->expects($this->any())->method('getIdentifier')->will($this->returnValue('cache')); $cache->expects($this->never())->method('collectGarbage'); $mockCacheManager = new \TYPO3\CMS\Core\Cache\CacheManager(); @@ -82,7 +82,7 @@ public function executeDoesNotCallCollectGarbageOfNotConfiguredBackend() GeneralUtility::setSingletonInstance(\TYPO3\CMS\Core\Cache\CacheManager::class, $mockCacheManager); $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'] = [ 'cache' => [ - 'frontend' => \TYPO3\CMS\Core\Cache\Frontend\StringFrontend::class, + 'frontend' => \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend::class, 'backend' => \TYPO3\CMS\Core\Cache\Backend\AbstractBackend::class, ] ];