Skip to content

Commit

Permalink
Merge pull request #3 from netlogix/TYPO3_11
Browse files Browse the repository at this point in the history
TYPO3 11.5
  • Loading branch information
phroust authored Jul 18, 2022
2 parents b657b82 + 96218ba commit e1a5eba
Show file tree
Hide file tree
Showing 21 changed files with 302 additions and 230 deletions.
12 changes: 10 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,16 @@ jobs:
strategy:
fail-fast: false
matrix:
php: [ 7.4]
typo3: [ 10.4 ]
php: [ 7.4, 8.0, 8.1 ]
typo3: [ 10.4, 11.5 ]

exclude:
# do not run TYPO3 10.4 on PHP 8
- php: 8.0
typo3: 10.4
# do not run TYPO3 10.4 on PHP 8.1
- php: 8.1
typo3: 10.4

steps:
- name: Checkout
Expand Down
30 changes: 30 additions & 0 deletions Classes/EventListener/EntityAddedToPersistence.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Netlogix\Nxcachetags\EventListener;

use Netlogix\Nxcachetags\Service\CacheTagService;
use TYPO3\CMS\Extbase\Event\Persistence\EntityAddedToPersistenceEvent;

class EntityAddedToPersistence
{
/**
* @var CacheTagService
*/
protected CacheTagService $cacheTagService;

public function injectCacheTagService(CacheTagService $cacheTagService)
{
$this->cacheTagService = $cacheTagService;
}

public function __invoke(EntityAddedToPersistenceEvent $event): void
{
$tags = $this->cacheTagService->createCacheTags($event->getObject());

foreach ($tags as $tag) {
$this->cacheTagService->flushCachesByTag($tag);
}
}
}
30 changes: 30 additions & 0 deletions Classes/EventListener/EntityRemovedFromPersistence.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Netlogix\Nxcachetags\EventListener;

use Netlogix\Nxcachetags\Service\CacheTagService;
use TYPO3\CMS\Extbase\Event\Persistence\EntityRemovedFromPersistenceEvent;

class EntityRemovedFromPersistence
{
/**
* @var CacheTagService
*/
protected CacheTagService $cacheTagService;

public function injectCacheTagService(CacheTagService $cacheTagService)
{
$this->cacheTagService = $cacheTagService;
}

public function __invoke(EntityRemovedFromPersistenceEvent $event): void
{
$tags = $this->cacheTagService->createCacheTags($event->getObject());

foreach ($tags as $tag) {
$this->cacheTagService->flushCachesByTag($tag);
}
}
}
30 changes: 30 additions & 0 deletions Classes/EventListener/EntityUpdatedInPersistence.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Netlogix\Nxcachetags\EventListener;

use Netlogix\Nxcachetags\Service\CacheTagService;
use TYPO3\CMS\Extbase\Event\Persistence\EntityUpdatedInPersistenceEvent;

class EntityUpdatedInPersistence
{
/**
* @var CacheTagService
*/
protected CacheTagService $cacheTagService;

public function injectCacheTagService(CacheTagService $cacheTagService)
{
$this->cacheTagService = $cacheTagService;
}

public function __invoke(EntityUpdatedInPersistenceEvent $event): void
{
$tags = $this->cacheTagService->createCacheTags($event->getObject());

foreach ($tags as $tag) {
$this->cacheTagService->flushCachesByTag($tag);
}
}
}
35 changes: 0 additions & 35 deletions Classes/Persistence/Generic/Storage/BackendSlot.php

This file was deleted.

13 changes: 1 addition & 12 deletions Classes/Service/CacheTagService.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use TYPO3\CMS\Core\SingletonInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

/**
Expand All @@ -23,11 +22,6 @@ class CacheTagService extends AbstractService implements SingletonInterface
public const ENVIRONMENT_TAGS = 't';
public const ENVIRONMENT_LIFETIME = 'l';

/**
* @var ObjectManagerInterface
*/
protected ObjectManagerInterface $objectManager;

/**
* @var ConfigurationManagerInterface
*/
Expand Down Expand Up @@ -64,11 +58,6 @@ class CacheTagService extends AbstractService implements SingletonInterface
],
];

public function injectObjectManager(ObjectManagerInterface $objectManager)
{
$this->objectManager = $objectManager;
}

public function injectConfigurationManager(ConfigurationManagerInterface $configurationManager)
{
$this->configurationManager = $configurationManager;
Expand All @@ -91,7 +80,7 @@ protected function initializeObjectIdentificationHelpers()
ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT
);
foreach ($settings['config.']['tx_nxcachetags.']['settings.']['objectIdentificationHelpers.'] as $key => $objectIdentificationHelperName) {
$this->objectIdentificationHelpers[$key] = $this->objectManager->get($objectIdentificationHelperName);
$this->objectIdentificationHelpers[$key] = GeneralUtility::makeInstance($objectIdentificationHelperName);
}
ksort($this->objectIdentificationHelpers);
}
Expand Down
12 changes: 6 additions & 6 deletions Classes/Service/MinimalLifetimeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function findMinimalLifetime(int $lifetime, array $identifiers = [], arra

protected function findMinimalLifetimeForRecord(int $expires, string $tableName, int $uid): int
{
if (!$GLOBALS['TCA'][$tableName]) {
if (!isset($GLOBALS['TCA'][$tableName])) {
return $expires;
}
$now = $GLOBALS['ACCESS_TIME'];
Expand All @@ -73,7 +73,7 @@ protected function findMinimalLifetimeForRecord(int $expires, string $tableName,
$queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($uid, PDO::PARAM_INT))
)
->execute()
->fetch();
->fetchAssociative();
if (!$record) {
return $expires;
}
Expand All @@ -98,12 +98,12 @@ protected function getStoragePids(array $lifetimeSource = []): array
);

foreach ($lifetimeSource as $tableName) {
if (in_array($tableName, $frameworkConfiguration['persistence']['noStoragePidForCacheLifetime'])) {
if (in_array($tableName, $frameworkConfiguration['persistence']['noStoragePidForCacheLifetime'] ?? [])) {
return [];
}
}
$storagePids = array_unique(
GeneralUtility::intExplode(',', $frameworkConfiguration['persistence']['storagePid'])
GeneralUtility::intExplode(',', $frameworkConfiguration['persistence']['storagePid'] ?? '')
);

$storagePids = array_flip($storagePids);
Expand All @@ -117,7 +117,7 @@ protected function getStoragePids(array $lifetimeSource = []): array

protected function findMinimalLifetimeForTable(int $expires, string $tableName, array $storagePids = []): int
{
if (!$GLOBALS['TCA'][$tableName]) {
if (!isset($GLOBALS['TCA'][$tableName])) {
return $expires;
}

Expand Down Expand Up @@ -149,7 +149,7 @@ protected function findMinimalLifetimeForTable(int $expires, string $tableName,
$queryBuilder->expr()->in('pid', array_map('intval', $storagePids))
);
}
$row = $query->execute()->fetch();
$row = $query->execute()->fetchAssociative();
if ($row && !is_null($row['minValue'])) {
$expires = (int)min($expires, $row['minValue']);
}
Expand Down
31 changes: 31 additions & 0 deletions Configuration/Services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
services:
_defaults:
autowire: true
autoconfigure: true
public: false

Netlogix\Nxcachetags\:
resource: '../Classes/*'

Netlogix\Nxcachetags\Service\RenderingContextIdentificationService:
# the parent class has in inject for `ViewHelperVariableContainer` which cannot be injected
# disable autowiring for this class and let TYPO3 do the work
autowire: false

Netlogix\Nxcachetags\EventListener\EntityAddedToPersistence:
tags:
- name: event.listener
identifier: 'nxcachetags-afterInsertObject'
event: TYPO3\CMS\Extbase\Event\Persistence\EntityAddedToPersistenceEvent

Netlogix\Nxcachetags\EventListener\EntityUpdatedInPersistence:
tags:
- name: event.listener
identifier: 'nxcachetags-afterUpdateObject'
event: TYPO3\CMS\Extbase\Event\Persistence\EntityUpdatedInPersistenceEvent

Netlogix\Nxcachetags\EventListener\EntityRemovedFromPersistence:
tags:
- name: event.listener
identifier: 'nxcachetags-afterRemoveObject'
event: TYPO3\CMS\Extbase\Event\Persistence\EntityRemovedFromPersistenceEvent
8 changes: 7 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# TYPO3 Extension "nxcachetags"
# TYPO3 Extension nxcachetags

[![stability-beta](https://img.shields.io/badge/stability-beta-33bbff.svg)](https://github.com/netlogix/nxcachetags)
[![TYPO3 V10](https://img.shields.io/badge/TYPO3-10-orange.svg)](https://get.typo3.org/version/10)
[![TYPO3 V11](https://img.shields.io/badge/TYPO3-11-orange.svg)](https://get.typo3.org/version/11)
[![Minimum PHP Version](https://img.shields.io/badge/php-%3E%3D%207.4-8892BF.svg)](https://php.net/)
[![GitHub CI status](https://github.com/netlogix/nxcachetags/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/netlogix/nxcachetags/actions)

Simplifies cache handling in TYPO3. Provides mechanisms for nested cached fragments,
e.g. a news list. In a news list the single news can be cached and the whole list can
Expand Down
45 changes: 45 additions & 0 deletions Tests/Functional/EventListener/EntityAddedToPersistenceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Netlogix\Nxcachetags\Tests\Functional\EventListener;

use Netlogix\Nxcachetags\EventListener\EntityAddedToPersistence;
use Netlogix\Nxcachetags\Service\CacheTagService;
use Nimut\TestingFramework\TestCase\FunctionalTestCase;
use TYPO3\CMS\Core\Cache\CacheManager;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManager;
use TYPO3\CMS\Extbase\Domain\Model\Category;
use TYPO3\CMS\Extbase\Domain\Repository\CategoryRepository;
use TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager;

class EntityAddedToPersistenceTest extends FunctionalTestCase
{
protected $testExtensionsToLoad = ['typo3conf/ext/nxcachetags'];

/**
* @test
* @return void
*/
public function itCallsEventAfterAddingObjectToPersistence() {
$mockCacheTagService = $this->getMockBuilder(CacheTagService::class)
->onlyMethods(['flushCachesByTag'])
->getMock();
$mockCacheTagService->injectCacheManager(GeneralUtility::makeInstance(CacheManager::class));
$mockCacheTagService->injectConfigurationManager(GeneralUtility::makeInstance(ConfigurationManager::class));
$mockCacheTagService->initializeObject();
$mockCacheTagService->expects(self::atLeastOnce())->method('flushCachesByTag');


GeneralUtility::setSingletonInstance(CacheTagService::class, $mockCacheTagService);


$object = new Category();
$object->setTitle(uniqid('title_'));

$repo = GeneralUtility::makeInstance(CategoryRepository::class);
$repo->add($object);
GeneralUtility::makeInstance(PersistenceManager::class)->persistAll();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Netlogix\Nxcachetags\Tests\Functional\EventListener;

use Netlogix\Nxcachetags\Service\CacheTagService;
use Nimut\TestingFramework\TestCase\FunctionalTestCase;
use TYPO3\CMS\Core\Cache\CacheManager;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManager;
use TYPO3\CMS\Extbase\Domain\Model\Category;
use TYPO3\CMS\Extbase\Domain\Repository\CategoryRepository;
use TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager;

class EntityRemovedFromPersistenceTest extends FunctionalTestCase
{
protected $testExtensionsToLoad = ['typo3conf/ext/nxcachetags'];

/**
* @test
* @return void
*/
public function itCallsEventAfterAddingObjectToPersistence()
{
$mockCacheTagService = $this->getMockBuilder(CacheTagService::class)
->onlyMethods(['flushCachesByTag'])
->getMock();
$mockCacheTagService->injectCacheManager(GeneralUtility::makeInstance(CacheManager::class));
$mockCacheTagService->injectConfigurationManager(GeneralUtility::makeInstance(ConfigurationManager::class));
$mockCacheTagService->initializeObject();
$mockCacheTagService->expects(self::atLeastOnce())->method('flushCachesByTag');
GeneralUtility::setSingletonInstance(CacheTagService::class, $mockCacheTagService);

$repo = GeneralUtility::makeInstance(CategoryRepository::class);

$cat = new Category();
$cat->setTitle(uniqid('title_'));
$repo->add($cat);
GeneralUtility::makeInstance(PersistenceManager::class)->persistAll();


$cat->setTitle(uniqid('title_'));
$repo->remove($cat);
GeneralUtility::makeInstance(PersistenceManager::class)->persistAll();
}
}
Loading

0 comments on commit e1a5eba

Please sign in to comment.