‪TYPO3CMS  ‪main
SoftReferenceParserFactory.php
Go to the documentation of this file.
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 
19 
20 use Psr\Log\LoggerInterface;
23 
28 {
29  protected array ‪$softReferenceParsers = [];
31  protected LoggerInterface ‪$logger;
32 
34  {
35  $this->runtimeCache = ‪$runtimeCache;
36  $this->logger = ‪$logger;
37  }
38 
44  public function ‪addParser(‪SoftReferenceParserInterface $softReferenceParser, string $parserKey): void
45  {
46  if (!isset($this->softReferenceParsers[$parserKey])) {
47  $this->softReferenceParsers[$parserKey] = $softReferenceParser;
48  }
49  }
50 
57  protected function ‪explodeSoftRefParserList(string $parserList): ?array
58  {
59  // Return immediately if list is blank:
60  if ($parserList === '') {
61  return null;
62  }
63  $cacheId = 'backend-softRefList-' . md5($parserList);
64  $parserListCache = $this->runtimeCache->get($cacheId);
65  if ($parserListCache !== false) {
66  return $parserListCache;
67  }
68  // Otherwise parse the list:
69  $keyList = ‪GeneralUtility::trimExplode(',', $parserList, true);
70  ‪$output = [];
71  foreach ($keyList as $val) {
72  $reg = [];
73  if (preg_match('/^([[:alnum:]_-]+)\\[(.*)\\]$/', $val, $reg)) {
74  ‪$output[$reg[1]] = ‪GeneralUtility::trimExplode(';', $reg[2], true);
75  } else {
76  ‪$output[$val] = '';
77  }
78  }
79  $this->runtimeCache->set($cacheId, ‪$output);
80  return ‪$output;
81  }
82 
87  public function ‪getParsersBySoftRefParserList(string $softRefParserList, array $forcedParameters = null): iterable
88  {
89  foreach ($this->‪explodeSoftRefParserList($softRefParserList) ?? [] as $parserKey => $parameters) {
90  if (!is_array($parameters)) {
91  $parameters = $forcedParameters ?? [];
92  }
93 
94  if (!$this->‪hasSoftReferenceParser($parserKey)) {
95  $this->logger->warning('No soft reference parser exists for the key "{parserKey}".', ['parserKey' => $parserKey]);
96  continue;
97  }
98 
99  ‪$parser = $this->‪getSoftReferenceParser($parserKey);
100  ‪$parser->setParserKey($parserKey, $parameters);
101 
102  yield ‪$parser;
103  }
104  }
105 
106  public function ‪hasSoftReferenceParser(string $softReferenceParserKey): bool
107  {
108  return isset($this->softReferenceParsers[$softReferenceParserKey]);
109  }
110 
123  public function ‪getSoftReferenceParser(string $softReferenceParserKey): ‪SoftReferenceParserInterface
124  {
125  if ($softReferenceParserKey === '') {
126  throw new \InvalidArgumentException(
127  'The soft reference parser key cannot be empty.',
128  1627899274
129  );
130  }
131 
132  if (!$this->‪hasSoftReferenceParser($softReferenceParserKey)) {
133  throw new \OutOfRangeException(
134  sprintf('No soft reference parser found for "%s".', $softReferenceParserKey),
135  1627899342
136  );
137  }
138 
139  return $this->softReferenceParsers[$softReferenceParserKey];
140  }
141 
145  public function ‪getSoftReferenceParsers(): array
146  {
148  }
149 }
‪TYPO3\CMS\Core\DataHandling\SoftReference\SoftReferenceParserFactory\getSoftReferenceParser
‪getSoftReferenceParser(string $softReferenceParserKey)
Definition: SoftReferenceParserFactory.php:123
‪TYPO3\CMS\Core\DataHandling\SoftReference\SoftReferenceParserFactory\$runtimeCache
‪FrontendInterface $runtimeCache
Definition: SoftReferenceParserFactory.php:30
‪TYPO3\CMS\Core\DataHandling\SoftReference\SoftReferenceParserFactory
Definition: SoftReferenceParserFactory.php:28
‪TYPO3\CMS\Core\DataHandling\SoftReference\SoftReferenceParserFactory\hasSoftReferenceParser
‪hasSoftReferenceParser(string $softReferenceParserKey)
Definition: SoftReferenceParserFactory.php:106
‪TYPO3\CMS\Core\DataHandling\SoftReference\SoftReferenceParserFactory\getParsersBySoftRefParserList
‪iterable< SoftReferenceParserInterface > getParsersBySoftRefParserList(string $softRefParserList, array $forcedParameters=null)
Definition: SoftReferenceParserFactory.php:87
‪$parser
‪$parser
Definition: annotationChecker.php:103
‪TYPO3\CMS\Core\DataHandling\SoftReference\SoftReferenceParserInterface
Definition: SoftReferenceParserInterface.php:31
‪TYPO3\CMS\Core\DataHandling\SoftReference
Definition: AbstractSoftReferenceParser.php:18
‪TYPO3\CMS\Core\DataHandling\SoftReference\SoftReferenceParserFactory\$logger
‪LoggerInterface $logger
Definition: SoftReferenceParserFactory.php:31
‪TYPO3\CMS\Core\DataHandling\SoftReference\SoftReferenceParserFactory\addParser
‪addParser(SoftReferenceParserInterface $softReferenceParser, string $parserKey)
Definition: SoftReferenceParserFactory.php:44
‪TYPO3\CMS\Core\DataHandling\SoftReference\SoftReferenceParserFactory\$softReferenceParsers
‪array $softReferenceParsers
Definition: SoftReferenceParserFactory.php:29
‪$output
‪$output
Definition: annotationChecker.php:114
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:22
‪TYPO3\CMS\Core\DataHandling\SoftReference\SoftReferenceParserFactory\explodeSoftRefParserList
‪array null explodeSoftRefParserList(string $parserList)
Definition: SoftReferenceParserFactory.php:57
‪TYPO3\CMS\Core\DataHandling\SoftReference\SoftReferenceParserFactory\getSoftReferenceParsers
‪getSoftReferenceParsers()
Definition: SoftReferenceParserFactory.php:145
‪TYPO3\CMS\Core\DataHandling\SoftReference\SoftReferenceParserFactory\__construct
‪__construct(FrontendInterface $runtimeCache, LoggerInterface $logger)
Definition: SoftReferenceParserFactory.php:33
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static list< string > trimExplode(string $delim, string $string, bool $removeEmptyValues=false, int $limit=0)
Definition: GeneralUtility.php:822