Skip to content

Commit

Permalink
[TASK] Deprecate StringFrontend cache frontend
Browse files Browse the repository at this point in the history
Can be drop-in replaced by VariableFrontend. There are no
occurrences in core code except for in tests and comments.
Configurations using this frontend can be automatically
migrated on-the-fly. Such auto-migration is not part of this
patch - this patch only touches the PHPDOC and documents
the deprecation.

Change-Id: I3b4b4979534857fa30b56d9967e8f919fc046ab9
Resolves: #81434
References: #81432
Releases: master
Reviewed-on: https://review.typo3.org/53084
Reviewed-by: Frank Naegler <frank.naegler@typo3.org>
Tested-by: Frank Naegler <frank.naegler@typo3.org>
Reviewed-by: Andreas Fernandez <a.fernandez@scripting-base.de>
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
  • Loading branch information
NamelessCoder authored and lolli42 committed Feb 12, 2018
1 parent 3e46361 commit 6c8a2df
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 26 deletions.
41 changes: 40 additions & 1 deletion typo3/sysext/core/Classes/Cache/Frontend/PhpFrontend.php
Expand Up @@ -23,7 +23,7 @@
* This file is a backport from FLOW3
* @api
*/
class PhpFrontend extends StringFrontend
class PhpFrontend extends AbstractFrontend
{
/**
* Constructs the cache
Expand Down Expand Up @@ -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.
*
Expand Down
19 changes: 19 additions & 0 deletions typo3/sysext/core/Classes/Cache/Frontend/StringFrontend.php
Expand Up @@ -14,16 +14,35 @@
* The TYPO3 project - inspiring people to share!
*/

use TYPO3\CMS\Core\Cache\Backend\BackendInterface;
use TYPO3\CMS\Core\Cache\Exception\InvalidDataException;

/**
* A cache frontend for strings. Nothing else.
*
* 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.
*
Expand Down
@@ -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
Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand Down
Expand Up @@ -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();
Expand Down
Expand Up @@ -14,22 +14,22 @@
* 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
*/
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();
Expand Down
Expand Up @@ -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();
Expand Down
@@ -1,5 +1,5 @@
<?php
namespace TYPO3\CMS\Core\Tests\Unit\Cache\Frontend;
namespace TYPO3\CMS\Core\Tests\UnitDeprecated\Cache\Frontend;

/*
* This file is part of the TYPO3 CMS project.
Expand Down
Expand Up @@ -48,15 +48,15 @@ protected function tearDown()
*/
public function executeCallsCollectGarbageOfConfiguredBackend()
{
$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->atLeastOnce())->method('collectGarbage');
$mockCacheManager = new \TYPO3\CMS\Core\Cache\CacheManager();
$mockCacheManager->registerCache($cache);
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,
]
];
Expand All @@ -74,15 +74,15 @@ 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();
$mockCacheManager->registerCache($cache);
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,
]
];
Expand Down

0 comments on commit 6c8a2df

Please sign in to comment.