TYPO3 v15 dev-main snapshot ()
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* 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!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\Cache\Frontend;
|
||||
|
||||
use TYPO3\CMS\Core\Cache\Backend\BackendInterface;
|
||||
use TYPO3\CMS\Core\Cache\Backend\TaggableBackendInterface;
|
||||
|
||||
abstract class AbstractFrontend implements FrontendInterface
|
||||
{
|
||||
public function __construct(
|
||||
protected string $identifier,
|
||||
protected BackendInterface $backend
|
||||
) {
|
||||
if (preg_match(self::PATTERN_ENTRYIDENTIFIER, $identifier) !== 1) {
|
||||
throw new \InvalidArgumentException('"' . $identifier . '" is not a valid cache identifier.', 1203584729);
|
||||
}
|
||||
$this->identifier = $identifier;
|
||||
$this->backend->setCache($this);
|
||||
}
|
||||
|
||||
public function getIdentifier(): string
|
||||
{
|
||||
return $this->identifier;
|
||||
}
|
||||
|
||||
public function getBackend(): BackendInterface
|
||||
{
|
||||
return $this->backend;
|
||||
}
|
||||
|
||||
public function has(string $entryIdentifier): bool
|
||||
{
|
||||
if (!$this->isValidEntryIdentifier($entryIdentifier)) {
|
||||
throw new \InvalidArgumentException('"' . $entryIdentifier . '" is not a valid cache entry identifier.', 1233058486);
|
||||
}
|
||||
return $this->backend->has($entryIdentifier);
|
||||
}
|
||||
|
||||
public function remove(string $entryIdentifier): bool
|
||||
{
|
||||
if (!$this->isValidEntryIdentifier($entryIdentifier)) {
|
||||
throw new \InvalidArgumentException('"' . $entryIdentifier . '" is not a valid cache entry identifier.', 1233058495);
|
||||
}
|
||||
return $this->backend->remove($entryIdentifier);
|
||||
}
|
||||
|
||||
public function flush(): void
|
||||
{
|
||||
$this->backend->flush();
|
||||
}
|
||||
|
||||
public function flushByTags(array $tags): void
|
||||
{
|
||||
if (!$this->backend instanceof TaggableBackendInterface) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($tags as $tag) {
|
||||
if (!$this->isValidTag($tag)) {
|
||||
throw new \InvalidArgumentException('"' . $tag . '" is not a valid tag for a cache entry.', 1233057360);
|
||||
}
|
||||
}
|
||||
|
||||
$this->backend->flushByTags($tags);
|
||||
}
|
||||
|
||||
public function flushByTag(string $tag): void
|
||||
{
|
||||
if (!$this->backend instanceof TaggableBackendInterface) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!$this->isValidTag($tag)) {
|
||||
throw new \InvalidArgumentException('"' . $tag . '" is not a valid tag for a cache entry.', 1233057359);
|
||||
}
|
||||
|
||||
$this->backend->flushByTag($tag);
|
||||
}
|
||||
|
||||
public function collectGarbage(): void
|
||||
{
|
||||
$this->backend->collectGarbage();
|
||||
}
|
||||
|
||||
public function isValidEntryIdentifier(string $identifier): bool
|
||||
{
|
||||
return preg_match(self::PATTERN_ENTRYIDENTIFIER, $identifier) === 1;
|
||||
}
|
||||
|
||||
public function isValidTag(string $tag): bool
|
||||
{
|
||||
return preg_match(self::PATTERN_TAG, $tag) === 1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* 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!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\Cache\Frontend;
|
||||
|
||||
use TYPO3\CMS\Core\Cache\Backend\BackendInterface;
|
||||
|
||||
/**
|
||||
* Contract for a Cache (frontend)
|
||||
*/
|
||||
interface FrontendInterface
|
||||
{
|
||||
/**
|
||||
* Pattern an entry identifier must match.
|
||||
*/
|
||||
public const PATTERN_ENTRYIDENTIFIER = '/^[a-zA-Z0-9_%\\-&]{1,250}$/';
|
||||
|
||||
/**
|
||||
* Pattern a tag must match.
|
||||
*/
|
||||
public const PATTERN_TAG = '/^[a-zA-Z0-9_%\\-&]{1,250}$/';
|
||||
|
||||
/**
|
||||
* Returns this cache's identifier
|
||||
*
|
||||
* @return string The identifier for this cache
|
||||
*/
|
||||
public function getIdentifier(): string;
|
||||
|
||||
/**
|
||||
* Returns the backend used by this cache
|
||||
*
|
||||
* @return BackendInterface The backend used by this cache
|
||||
*/
|
||||
public function getBackend(): BackendInterface;
|
||||
|
||||
/**
|
||||
* Saves data in the cache.
|
||||
*
|
||||
* @param string $entryIdentifier Something which identifies the data - depends on concrete cache
|
||||
* @param mixed $data The data to cache - also depends on the concrete cache implementation
|
||||
* @param array $tags Tags to associate with this cache entry
|
||||
* @param int|null $lifetime Lifetime of this cache entry in seconds. If NULL is specified, the default lifetime is used. "0" means unlimited lifetime.
|
||||
*/
|
||||
public function set(string $entryIdentifier, mixed $data, array $tags = [], ?int $lifetime = null): void;
|
||||
|
||||
/**
|
||||
* Finds and returns data from the cache.
|
||||
*
|
||||
* @param string $entryIdentifier Something which identifies the cache entry - depends on concrete cache
|
||||
*/
|
||||
public function get(string $entryIdentifier): mixed;
|
||||
|
||||
/**
|
||||
* Checks if a cache entry with the specified identifier exists.
|
||||
*
|
||||
* @param string $entryIdentifier An identifier specifying the cache entry
|
||||
* @return bool TRUE if such an entry exists, FALSE if not
|
||||
*/
|
||||
public function has(string $entryIdentifier): bool;
|
||||
|
||||
/**
|
||||
* Removes the given cache entry from the cache.
|
||||
*
|
||||
* @param string $entryIdentifier An identifier specifying the cache entry
|
||||
* @return bool TRUE if such an entry exists, FALSE if not
|
||||
*/
|
||||
public function remove(string $entryIdentifier): bool;
|
||||
|
||||
/**
|
||||
* Removes all cache entries of this cache.
|
||||
*/
|
||||
public function flush(): void;
|
||||
|
||||
/**
|
||||
* Removes all cache entries of this cache which are tagged by the specified tag.
|
||||
*
|
||||
* @param string $tag The tag the entries must have
|
||||
*/
|
||||
public function flushByTag(string $tag): void;
|
||||
|
||||
/**
|
||||
* Removes all cache entries of this cache which are tagged by any of the specified tags.
|
||||
*
|
||||
* @param string[] $tags List of tags
|
||||
*/
|
||||
public function flushByTags(array $tags): void;
|
||||
|
||||
/**
|
||||
* Does garbage collection
|
||||
*/
|
||||
public function collectGarbage(): void;
|
||||
|
||||
/**
|
||||
* Checks the validity of an entry identifier. Returns TRUE if it's valid.
|
||||
*
|
||||
* @param string $identifier An identifier to be checked for validity
|
||||
*/
|
||||
public function isValidEntryIdentifier(string $identifier): bool;
|
||||
|
||||
/**
|
||||
* Checks the validity of a tag. Returns TRUE if it's valid.
|
||||
*
|
||||
* @param string $tag A tag to be checked for validity
|
||||
*/
|
||||
public function isValidTag(string $tag): bool;
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* 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!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\Cache\Frontend;
|
||||
|
||||
use TYPO3\CMS\Core\Cache\Backend\NullBackend;
|
||||
|
||||
/**
|
||||
* This class only acts as shortcut to construct a cache frontend with a null backend.
|
||||
* It extends PhpFrontend to be sure it can also be used for all types of caches (also the one requiring a PhpFrontend like "core").
|
||||
*
|
||||
* @todo: Instead a factory class should be introduced that replaces this class and \TYPO3\CMS\Core\Core\Bootstrap::createCache
|
||||
*/
|
||||
class NullFrontend extends PhpFrontend
|
||||
{
|
||||
public function __construct(string $identifier)
|
||||
{
|
||||
$backend = new NullBackend();
|
||||
parent::__construct($identifier, $backend);
|
||||
}
|
||||
|
||||
public function set(string $entryIdentifier, mixed $data, array $tags = [], ?int $lifetime = null): void
|
||||
{
|
||||
// Noop
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* 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!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\Cache\Frontend;
|
||||
|
||||
use TYPO3\CMS\Core\Cache\Backend\PhpCapableBackendInterface;
|
||||
use TYPO3\CMS\Core\Cache\Exception\InvalidDataException;
|
||||
|
||||
/**
|
||||
* A cache frontend tailored to PHP code.
|
||||
*/
|
||||
class PhpFrontend extends AbstractFrontend
|
||||
{
|
||||
public function __construct(string $identifier, PhpCapableBackendInterface $backend)
|
||||
{
|
||||
parent::__construct($identifier, $backend);
|
||||
}
|
||||
|
||||
public function set(string $entryIdentifier, mixed $data, array $tags = [], ?int $lifetime = null): void
|
||||
{
|
||||
if (!$this->isValidEntryIdentifier($entryIdentifier)) {
|
||||
throw new \InvalidArgumentException('"' . $entryIdentifier . '" is not a valid cache entry identifier.', 1264023823);
|
||||
}
|
||||
if (!is_string($data)) {
|
||||
throw new InvalidDataException('The given source code is not a valid string.', 1264023824);
|
||||
}
|
||||
foreach ($tags as $tag) {
|
||||
if (!$this->isValidTag($tag)) {
|
||||
throw new \InvalidArgumentException('"' . $tag . '" is not a valid tag for a cache entry.', 1264023825);
|
||||
}
|
||||
}
|
||||
$sourceCode = '<?php' . LF . $data . LF . '#';
|
||||
$this->backend->set($entryIdentifier, $sourceCode, $tags, $lifetime);
|
||||
}
|
||||
|
||||
public function get(string $entryIdentifier): mixed
|
||||
{
|
||||
if (!$this->isValidEntryIdentifier($entryIdentifier)) {
|
||||
throw new \InvalidArgumentException('"' . $entryIdentifier . '" is not a valid cache entry identifier.', 1233057753);
|
||||
}
|
||||
return $this->backend->get($entryIdentifier);
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads PHP code from the cache and require_onces it right away.
|
||||
*
|
||||
* @param string $entryIdentifier An identifier which describes the cache entry to load
|
||||
* @return mixed Potential return value from the include operation
|
||||
*/
|
||||
public function requireOnce(string $entryIdentifier): mixed
|
||||
{
|
||||
$backend = $this->getBackend();
|
||||
if (!($backend instanceof PhpCapableBackendInterface)) {
|
||||
throw new \RuntimeException('Can not require: Not a PhpCapableBackendInterface', 1763660480);
|
||||
}
|
||||
return $backend->requireOnce($entryIdentifier);
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads PHP code from the cache and require() it right away. Note require()
|
||||
* in comparison to requireOnce() is only "safe" if the cache entry only contain stuff
|
||||
* that can be required multiple times during one request. For instance a class definition
|
||||
* would fail here.
|
||||
*
|
||||
* @param string $entryIdentifier An identifier which describes the cache entry to load
|
||||
* @return mixed Potential return value from the include operation
|
||||
*/
|
||||
public function require(string $entryIdentifier): mixed
|
||||
{
|
||||
$backend = $this->getBackend();
|
||||
if (!($backend instanceof PhpCapableBackendInterface)) {
|
||||
throw new \RuntimeException('Can not require: Not a PhpCapableBackendInterface', 1763660481);
|
||||
}
|
||||
return $backend->require($entryIdentifier);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* 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!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\Cache\Frontend;
|
||||
|
||||
use TYPO3\CMS\Core\Cache\Backend\TransientBackendInterface;
|
||||
use TYPO3\CMS\Core\Crypto\HashService;
|
||||
use TYPO3\CMS\Core\Serializer\AuthenticatedMessageDeserializer;
|
||||
use TYPO3\CMS\Core\Serializer\DeserializationService;
|
||||
use TYPO3\CMS\Core\Serializer\Exception\DeserializerException;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
|
||||
/**
|
||||
* A cache frontend for any kinds of PHP variables
|
||||
*/
|
||||
class VariableFrontend extends AbstractFrontend
|
||||
{
|
||||
/**
|
||||
* Saves the value of a PHP variable in the cache. Note that the variable
|
||||
* will be serialized if necessary.
|
||||
*
|
||||
* @param int|null $lifetime Lifetime of this cache entry in seconds. If NULL is specified, the default lifetime is used. "0" means unlimited lifetime.
|
||||
*/
|
||||
public function set(string $entryIdentifier, mixed $data, array $tags = [], ?int $lifetime = null): void
|
||||
{
|
||||
if (!$this->isValidEntryIdentifier($entryIdentifier)) {
|
||||
throw new \InvalidArgumentException(
|
||||
'"' . $entryIdentifier . '" is not a valid cache entry identifier.',
|
||||
1233058264
|
||||
);
|
||||
}
|
||||
foreach ($tags as $tag) {
|
||||
if (!$this->isValidTag($tag)) {
|
||||
throw new \InvalidArgumentException('"' . $tag . '" is not a valid tag for a cache entry.', 1233058269);
|
||||
}
|
||||
}
|
||||
foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/cache/frontend/class.t3lib_cache_frontend_variablefrontend.php']['set'] ?? [] as $_funcRef) {
|
||||
$params = [
|
||||
'entryIdentifier' => &$entryIdentifier,
|
||||
'variable' => &$data,
|
||||
'tags' => &$tags,
|
||||
'lifetime' => &$lifetime,
|
||||
];
|
||||
GeneralUtility::callUserFunction($_funcRef, $params, $this);
|
||||
}
|
||||
if (!$this->backend instanceof TransientBackendInterface) {
|
||||
// No DI/GeneralUtility::makeInstance usage, since caching needs to operate prior to DI container setup.
|
||||
$deserializer = new AuthenticatedMessageDeserializer(new HashService(), new DeserializationService());
|
||||
$data = $deserializer->serialize($data, VariableFrontend::class);
|
||||
}
|
||||
$this->backend->set($entryIdentifier, $data, $tags, $lifetime);
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds and returns a variable value from the cache.
|
||||
*/
|
||||
public function get(string $entryIdentifier): mixed
|
||||
{
|
||||
if (!$this->isValidEntryIdentifier($entryIdentifier)) {
|
||||
throw new \InvalidArgumentException(
|
||||
'"' . $entryIdentifier . '" is not a valid cache entry identifier.',
|
||||
1233058294
|
||||
);
|
||||
}
|
||||
$rawResult = $this->backend->get($entryIdentifier);
|
||||
if ($rawResult === false) {
|
||||
return false;
|
||||
}
|
||||
if ($this->backend instanceof TransientBackendInterface) {
|
||||
return $rawResult;
|
||||
}
|
||||
try {
|
||||
// No DI/GeneralUtility::makeInstance usage, since caching needs to operate prior to DI container setup.
|
||||
$deserializer = new AuthenticatedMessageDeserializer(new HashService(), new DeserializationService());
|
||||
return $deserializer->deserialize($rawResult, VariableFrontend::class);
|
||||
} catch (DeserializerException) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user