Skip to content
Snippets Groups Projects
Commit 3bd57aec authored by Christian Kuhn's avatar Christian Kuhn
Browse files

[TASK] Stateless TsConfigTreeBuilder

We can reduce class state by explicitely handing
over runtime state to consuming methods in favor
of parking them on $this.

Resolves: #101804
Releases: main, 12.4
Change-Id: Ic33d619a189625fa9407a3ee36cddf76d0f14d32
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/80691


Tested-by: default avatarChristian Kuhn <lolli@schwarzbu.ch>
Tested-by: default avatarcore-ci <typo3@b13.com>
Reviewed-by: default avatarChristian Kuhn <lolli@schwarzbu.ch>
parent bc6ccceb
Branches
Tags
No related merge requests found
...@@ -38,9 +38,6 @@ use TYPO3\CMS\Core\Utility\PathUtility; ...@@ -38,9 +38,6 @@ use TYPO3\CMS\Core\Utility\PathUtility;
*/ */
final class TsConfigTreeBuilder final class TsConfigTreeBuilder
{ {
private TokenizerInterface $tokenizer;
private ?PhpFrontend $cache = null;
public function __construct( public function __construct(
private readonly TreeFromLineStreamBuilder $treeFromTokenStreamBuilder, private readonly TreeFromLineStreamBuilder $treeFromTokenStreamBuilder,
private readonly PackageManager $packageManager, private readonly PackageManager $packageManager,
...@@ -53,24 +50,22 @@ final class TsConfigTreeBuilder ...@@ -53,24 +50,22 @@ final class TsConfigTreeBuilder
TokenizerInterface $tokenizer, TokenizerInterface $tokenizer,
?PhpFrontend $cache = null ?PhpFrontend $cache = null
): RootInclude { ): RootInclude {
$this->tokenizer = $tokenizer;
$this->cache = $cache;
$includeTree = new RootInclude(); $includeTree = new RootInclude();
if (!empty($GLOBALS['TYPO3_CONF_VARS']['BE']['defaultUserTSconfig'] ?? '')) { if (!empty($GLOBALS['TYPO3_CONF_VARS']['BE']['defaultUserTSconfig'] ?? '')) {
$includeTree->addChild($this->getTreeFromString('userTsConfig-globals', $GLOBALS['TYPO3_CONF_VARS']['BE']['defaultUserTSconfig'])); $includeTree->addChild($this->getTreeFromString('userTsConfig-globals', $GLOBALS['TYPO3_CONF_VARS']['BE']['defaultUserTSconfig'], $tokenizer, $cache));
} }
if ($backendUser->isAdmin()) { if ($backendUser->isAdmin()) {
// @todo: Could we maybe solve this differently somehow? Maybe in ext:adminpanel in FE directly? // @todo: Could we maybe solve this differently somehow? Maybe in ext:adminpanel in FE directly?
$includeTree->addChild($this->getTreeFromString('userTsConfig-admpanel', 'admPanel.enable.all = 1')); $includeTree->addChild($this->getTreeFromString('userTsConfig-admpanel', 'admPanel.enable.all = 1', $tokenizer, $cache));
} }
foreach ($backendUser->userGroupsUID as $groupId) { foreach ($backendUser->userGroupsUID as $groupId) {
// Loop through all groups and add their 'TSconfig' fields // Loop through all groups and add their 'TSconfig' fields
if (!empty($backendUser->userGroups[$groupId]['TSconfig'] ?? '')) { if (!empty($backendUser->userGroups[$groupId]['TSconfig'] ?? '')) {
$includeTree->addChild($this->getTreeFromString('userTsConfig-group-' . $groupId, $backendUser->userGroups[$groupId]['TSconfig'])); $includeTree->addChild($this->getTreeFromString('userTsConfig-group-' . $groupId, $backendUser->userGroups[$groupId]['TSconfig'], $tokenizer, $cache));
} }
} }
if (!empty($backendUser->user['TSconfig'] ?? '')) { if (!empty($backendUser->user['TSconfig'] ?? '')) {
$includeTree->addChild($this->getTreeFromString('userTsConfig-user', $backendUser->user['TSconfig'])); $includeTree->addChild($this->getTreeFromString('userTsConfig-user', $backendUser->user['TSconfig'], $tokenizer, $cache));
} }
return $includeTree; return $includeTree;
} }
...@@ -80,9 +75,6 @@ final class TsConfigTreeBuilder ...@@ -80,9 +75,6 @@ final class TsConfigTreeBuilder
TokenizerInterface $tokenizer, TokenizerInterface $tokenizer,
?PhpFrontend $cache = null ?PhpFrontend $cache = null
): RootInclude { ): RootInclude {
$this->tokenizer = $tokenizer;
$this->cache = $cache;
$collectedPagesTsConfigArray = []; $collectedPagesTsConfigArray = [];
$gotPackagesPagesTsConfigFromCache = false; $gotPackagesPagesTsConfigFromCache = false;
if ($cache) { if ($cache) {
...@@ -155,7 +147,7 @@ final class TsConfigTreeBuilder ...@@ -155,7 +147,7 @@ final class TsConfigTreeBuilder
$includeTree = new RootInclude(); $includeTree = new RootInclude();
foreach ($collectedPagesTsConfigArray as $key => $typoScriptString) { foreach ($collectedPagesTsConfigArray as $key => $typoScriptString) {
$includeTree->addChild($this->getTreeFromString((string)$key, $typoScriptString)); $includeTree->addChild($this->getTreeFromString((string)$key, $typoScriptString, $tokenizer, $cache));
} }
return $includeTree; return $includeTree;
} }
...@@ -163,20 +155,22 @@ final class TsConfigTreeBuilder ...@@ -163,20 +155,22 @@ final class TsConfigTreeBuilder
private function getTreeFromString( private function getTreeFromString(
string $name, string $name,
string $typoScriptString, string $typoScriptString,
TokenizerInterface $tokenizer,
?PhpFrontend $cache = null
): TsConfigInclude { ): TsConfigInclude {
$lowercaseName = mb_strtolower($name); $lowercaseName = mb_strtolower($name);
$identifier = $lowercaseName . '-' . hash('xxh3', $typoScriptString); $identifier = $lowercaseName . '-' . hash('xxh3', $typoScriptString);
if ($this->cache) { if ($cache) {
$includeNode = $this->cache->require($identifier); $includeNode = $cache->require($identifier);
if ($includeNode instanceof TsConfigInclude) { if ($includeNode instanceof TsConfigInclude) {
return $includeNode; return $includeNode;
} }
} }
$includeNode = new TsConfigInclude(); $includeNode = new TsConfigInclude();
$includeNode->setName($name); $includeNode->setName($name);
$includeNode->setLineStream($this->tokenizer->tokenize($typoScriptString)); $includeNode->setLineStream($tokenizer->tokenize($typoScriptString));
$this->treeFromTokenStreamBuilder->buildTree($includeNode, 'tsconfig', $this->tokenizer); $this->treeFromTokenStreamBuilder->buildTree($includeNode, 'tsconfig', $tokenizer);
$this->cache?->set($identifier, 'return unserialize(\'' . addcslashes(serialize($includeNode), '\'\\') . '\');'); $cache?->set($identifier, 'return unserialize(\'' . addcslashes(serialize($includeNode), '\'\\') . '\');');
return $includeNode; return $includeNode;
} }
} }
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment