Skip to content

Commit 9835f42

Browse files
committed
[TASK] Extract UserTsConfigGenerator into own service
1 parent 5dc912f commit 9835f42

File tree

3 files changed

+63
-34
lines changed

3 files changed

+63
-34
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the TYPO3 CMS project.
7+
*
8+
* It is free software; you can redistribute it and/or modify it under
9+
* the terms of the GNU General Public License, either version 2
10+
* of the License, or any later version.
11+
*
12+
* For the full copyright and license information, please read the
13+
* LICENSE.txt file that was distributed with this source code.
14+
*
15+
* The TYPO3 project - inspiring people to share!
16+
*/
17+
18+
namespace TYPO3\CMS\ContentBlocks\EventListener;
19+
20+
use Symfony\Component\DependencyInjection\Attribute\Autowire;
21+
use TYPO3\CMS\ContentBlocks\Generator\UserTsConfigGenerator;
22+
use TYPO3\CMS\Core\Attribute\AsEventListener;
23+
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
24+
use TYPO3\CMS\Core\Cache\Frontend\PhpFrontend;
25+
use TYPO3\CMS\Core\TypoScript\IncludeTree\Event\BeforeLoadedUserTsConfigEvent;
26+
27+
readonly class AddUserTsConfig
28+
{
29+
public function __construct(
30+
/** @var PhpFrontend */
31+
#[Autowire(service: 'cache.core')]
32+
protected FrontendInterface $cache,
33+
protected UserTsConfigGenerator $userTsConfigGenerator,
34+
) {
35+
}
36+
37+
#[AsEventListener(identifier: 'content-blocks-user-ts-config')]
38+
public function __invoke(BeforeLoadedUserTsConfigEvent $event): void
39+
{
40+
$userTsConfig = $this->create();
41+
$event->addTsConfig($userTsConfig);
42+
}
43+
44+
protected function create(): string
45+
{
46+
$typoScriptFromCache = $this->cache->require('ContentBlocks_UserTsConfig');
47+
if ($typoScriptFromCache !== false) {
48+
return $typoScriptFromCache;
49+
}
50+
$userTsConfig = $this->userTsConfigGenerator->generate();
51+
$this->cache->set('ContentBlocks_UserTsConfig', 'return ' . var_export($userTsConfig, true) . ';');
52+
return $userTsConfig;
53+
}
54+
}

Classes/Generator/UserTsConfigGenerator.php

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,41 +17,19 @@
1717

1818
namespace TYPO3\CMS\ContentBlocks\Generator;
1919

20-
use Symfony\Component\DependencyInjection\Attribute\Autowire;
2120
use TYPO3\CMS\ContentBlocks\Definition\ContentType\PageTypeDefinition;
2221
use TYPO3\CMS\ContentBlocks\Definition\TableDefinitionCollection;
23-
use TYPO3\CMS\Core\Attribute\AsEventListener;
24-
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
25-
use TYPO3\CMS\Core\Cache\Frontend\PhpFrontend;
26-
use TYPO3\CMS\Core\TypoScript\IncludeTree\Event\BeforeLoadedUserTsConfigEvent;
2722

23+
/**
24+
* @internal Not part of TYPO3's public API.
25+
*/
2826
readonly class UserTsConfigGenerator
2927
{
3028
public function __construct(
31-
/** @var PhpFrontend */
32-
#[Autowire(service: 'cache.core')]
33-
protected FrontendInterface $cache,
3429
protected TableDefinitionCollection $tableDefinitionCollection,
3530
) {}
3631

37-
#[AsEventListener(identifier: 'content-blocks-user-ts-config')]
38-
public function __invoke(BeforeLoadedUserTsConfigEvent $event): void
39-
{
40-
$userTsConfig = $this->create();
41-
$concatenatedTypoScript = implode(LF, $userTsConfig);
42-
$event->addTsConfig($concatenatedTypoScript);
43-
}
44-
45-
protected function create(): array
46-
{
47-
$typoScriptFromCache = $this->cache->require('ContentBlocks_UserTsConfig');
48-
if ($typoScriptFromCache !== false) {
49-
return $typoScriptFromCache;
50-
}
51-
return $this->createUncached();
52-
}
53-
54-
protected function createUncached(): array
32+
public function generate(): string
5533
{
5634
$userTsConfig = [];
5735
foreach ($this->tableDefinitionCollection as $tableDefinition) {
@@ -62,7 +40,7 @@ protected function createUncached(): array
6240
}
6341
}
6442
}
65-
$this->cache->set('ContentBlocks_UserTsConfig', 'return ' . var_export($userTsConfig, true) . ';');
66-
return $userTsConfig;
43+
$concatenatedTypoScript = implode(LF, $userTsConfig);
44+
return $concatenatedTypoScript;
6745
}
6846
}

Tests/Functional/Generator/UserTsConfigGeneratorTest.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,10 @@ class UserTsConfigGeneratorTest extends FunctionalTestCase
3232
#[Test]
3333
public function userTsConfigIsGenerated(): void
3434
{
35+
/** @var UserTsConfigGenerator $userTsConfigGenerator */
3536
$userTsConfigGenerator = $this->get(UserTsConfigGenerator::class);
36-
$beforeLoadedUserTsConfigEvent = new BeforeLoadedUserTsConfigEvent();
37-
38-
$userTsConfigGenerator->__invoke($beforeLoadedUserTsConfigEvent);
39-
40-
$expected = ['options.pageTree.doktypesToShowInNewPageDragArea := addToList(942)'];
41-
$result = $beforeLoadedUserTsConfigEvent->getTsConfig();
37+
$result = $userTsConfigGenerator->generate();
38+
$expected = 'options.pageTree.doktypesToShowInNewPageDragArea := addToList(942)';
4239
self::assertSame($expected, $result);
4340
}
4441
}

0 commit comments

Comments
 (0)