This repository has been archived by the owner on Mar 1, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
316 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
<?php | ||
/* | ||
* 2017 Romain CANON <romain.hydrocanon@gmail.com> | ||
* | ||
* This file is part of the TYPO3 Configuration Object project. | ||
* It is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License, either | ||
* version 3 of the License, or any later version. | ||
* | ||
* For the full copyright and license information, see: | ||
* http://www.gnu.org/licenses/gpl-3.0.html | ||
*/ | ||
|
||
namespace Romm\ConfigurationObject\Core\Service; | ||
|
||
use TYPO3\CMS\Core\Cache\Backend\FileBackend; | ||
use TYPO3\CMS\Core\Cache\CacheManager; | ||
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface; | ||
use TYPO3\CMS\Core\Cache\Frontend\VariableFrontend; | ||
use TYPO3\CMS\Core\SingletonInterface; | ||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
|
||
class CacheService implements SingletonInterface | ||
{ | ||
const CACHE_IDENTIFIER = 'cache_configuration_object'; | ||
const CACHE_TAG_DYNAMIC_CACHE = 'dynamic-cache'; | ||
|
||
/** | ||
* Options for the internal cache. | ||
* | ||
* @var array | ||
*/ | ||
protected $cacheOptions = [ | ||
'backend' => FileBackend::class, | ||
'frontend' => VariableFrontend::class, | ||
'groups' => ['all', 'system'] | ||
]; | ||
|
||
/** | ||
* Function called from `ext_localconf` file which will register the | ||
* internal cache earlier. | ||
* | ||
* @internal | ||
*/ | ||
public function registerInternalCache() | ||
{ | ||
$this->getCacheManager() | ||
->setCacheConfigurations([self::CACHE_IDENTIFIER => $this->cacheOptions]); | ||
} | ||
|
||
/** | ||
* This function will take care of initializing all caches that were defined | ||
* previously by the `CacheService` which allows dynamic caches to be used | ||
* for every configuration object type. | ||
* | ||
* @see \Romm\ConfigurationObject\Service\Items\Cache\CacheService::initialize() | ||
* @internal | ||
*/ | ||
public function registerDynamicCaches() | ||
{ | ||
$dynamicCaches = $this->getCache()->getByTag(self::CACHE_TAG_DYNAMIC_CACHE); | ||
|
||
foreach ($dynamicCaches as $cacheData) { | ||
$identifier = $cacheData['identifier']; | ||
$options = $cacheData['options']; | ||
|
||
$this->registerCacheInternal($identifier, $options); | ||
} | ||
} | ||
|
||
/** | ||
* Registers a new dynamic cache: the cache will be added to the cache | ||
* manager after this function is executed. Its configuration will also be | ||
* remembered so the cache will be registered properly during the cache | ||
* framework initialization in further requests. | ||
* | ||
* @param string $identifier | ||
* @param array $options | ||
*/ | ||
public function registerDynamicCache($identifier, array $options) | ||
{ | ||
if (false === $this->getCache()->has($identifier)) { | ||
$this->getCache()->set( | ||
$identifier, | ||
[ | ||
'identifier' => $identifier, | ||
'options' => $options | ||
], | ||
[self::CACHE_TAG_DYNAMIC_CACHE] | ||
); | ||
} | ||
|
||
$this->registerCacheInternal($identifier, $options); | ||
} | ||
|
||
/** | ||
* @param string $identifier | ||
* @param array $options | ||
*/ | ||
protected function registerCacheInternal($identifier, array $options) | ||
{ | ||
$cacheManager = $this->getCacheManager(); | ||
|
||
if (false === $cacheManager->hasCache($identifier)) { | ||
$cacheManager->setCacheConfigurations([$identifier => $options]); | ||
} | ||
} | ||
|
||
/** | ||
* @return FrontendInterface | ||
*/ | ||
protected function getCache() | ||
{ | ||
return $this->getCacheManager()->getCache(self::CACHE_IDENTIFIER); | ||
} | ||
|
||
/** | ||
* @return CacheManager | ||
*/ | ||
protected function getCacheManager() | ||
{ | ||
/** @var CacheManager $cacheManager */ | ||
$cacheManager = GeneralUtility::makeInstance(CacheManager::class); | ||
|
||
return $cacheManager; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
namespace Romm\ConfigurationObject\Tests\Unit\Core\Service\Cache; | ||
|
||
use Prophecy\Argument; | ||
use Prophecy\Prophecy\ObjectProphecy; | ||
use Romm\ConfigurationObject\Core\Service\CacheService; | ||
use Romm\ConfigurationObject\Tests\Unit\AbstractUnitTest; | ||
use TYPO3\CMS\Core\Cache\CacheManager; | ||
use TYPO3\CMS\Core\Cache\Frontend\VariableFrontend; | ||
|
||
class CacheServiceTest extends AbstractUnitTest | ||
{ | ||
/** | ||
* Checks that the internal cache used by the API is properly registered | ||
* during TYPO3 initialization. | ||
* | ||
* @test | ||
*/ | ||
public function internalCacheIsRegisteredProperly() | ||
{ | ||
$cacheManager = new CacheManager; | ||
|
||
/** @var CacheService|\PHPUnit_Framework_MockObject_MockObject $cacheServiceMock */ | ||
$cacheServiceMock = $this->getMockBuilder(CacheService::class) | ||
->setMethods(['getCacheManager']) | ||
->getMock(); | ||
|
||
$cacheServiceMock->method('getCacheManager') | ||
->willReturn($cacheManager); | ||
|
||
$this->assertFalse($cacheManager->hasCache(CacheService::CACHE_IDENTIFIER)); | ||
$cacheServiceMock->registerInternalCache(); | ||
$this->assertTrue($cacheManager->hasCache(CacheService::CACHE_IDENTIFIER)); | ||
} | ||
|
||
/** | ||
* Checks that a cache can be registered dynamically, and only once per | ||
* request. | ||
* | ||
* @test | ||
*/ | ||
public function registerDynamicCacheWorksProperly() | ||
{ | ||
$cacheIdentifier = 'foo'; | ||
$cacheOptions = ['bar' => 'baz']; | ||
|
||
/** @var CacheService|\PHPUnit_Framework_MockObject_MockObject $cacheServiceMock */ | ||
$cacheServiceMock = $this->getMockBuilder(CacheService::class) | ||
->setMethods(['getCache', 'getCacheManager']) | ||
->getMock(); | ||
|
||
/** @var VariableFrontend|ObjectProphecy $cacheProphecy */ | ||
$cacheProphecy = $this->prophesize(VariableFrontend::class); | ||
|
||
$cacheProphecy->has($cacheIdentifier) | ||
->shouldBeCalledTimes(2) | ||
->will(function () use ($cacheProphecy, $cacheIdentifier, $cacheOptions) { | ||
$cacheProphecy->set($cacheIdentifier, Argument::type('array'), [CacheService::CACHE_TAG_DYNAMIC_CACHE]) | ||
->shouldBeCalledTimes(1); | ||
|
||
$cacheProphecy->has($cacheIdentifier) | ||
->willReturn(true); | ||
|
||
return false; | ||
}); | ||
|
||
$cacheProphecy->getByTag(CacheService::CACHE_TAG_DYNAMIC_CACHE) | ||
->shouldBeCalledTimes(1) | ||
->willReturn([ | ||
'foo' => [ | ||
'identifier' => 'foo', | ||
'options' => [] | ||
] | ||
]); | ||
|
||
$cacheServiceMock->method('getCache') | ||
->willReturn($cacheProphecy->reveal()); | ||
|
||
/** @var CacheManager|ObjectProphecy $cacheManagerProphecy */ | ||
$cacheManagerProphecy = $this->prophesize(CacheManager::class); | ||
|
||
$cacheManagerProphecy->hasCache($cacheIdentifier) | ||
->shouldBeCalledTimes(3) | ||
->will(function () use ($cacheManagerProphecy, $cacheIdentifier) { | ||
$cacheManagerProphecy->hasCache($cacheIdentifier) | ||
->willReturn(true); | ||
|
||
$cacheManagerProphecy->setCacheConfigurations(Argument::type('array')) | ||
->shouldBeCalledTimes(1); | ||
|
||
return false; | ||
}); | ||
|
||
$cacheServiceMock->method('getCacheManager') | ||
->willReturn($cacheManagerProphecy->reveal()); | ||
|
||
$cacheServiceMock->registerDynamicCache($cacheIdentifier, $cacheOptions); | ||
$cacheServiceMock->registerDynamicCache($cacheIdentifier, $cacheOptions); | ||
$cacheServiceMock->registerDynamicCaches(); | ||
} | ||
} |
Oops, something went wrong.