‪TYPO3CMS  11.5
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  // TODO remove in TYPO3 v12.0
39  foreach (‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['GLOBAL']['softRefParser'] ?? [] as $parserKey => $className) {
40  trigger_error(
41  sprintf(
42  'The soft reference parser %s for the key "%s" is registered in the global array $GLOBALS[TYPO3_CONF_VARS][SC_OPTIONS][GLOBAL][softRefParser]. This way of registration will stop working in TYPO3 v12.0. Register the class in Configuration/Services.yaml instead.',
43  $className,
44  $parserKey
45  ),
46  E_USER_DEPRECATED
47  );
48  $this->softReferenceParsers[$parserKey] = GeneralUtility::makeInstance($className);
49  }
50  }
51 
59  public function ‪addParser(‪SoftReferenceParserInterface $softReferenceParser, string $parserKey): void
60  {
61  if (!isset($this->softReferenceParsers[$parserKey])) {
62  $this->softReferenceParsers[$parserKey] = $softReferenceParser;
63  }
64  }
65 
72  protected function ‪explodeSoftRefParserList(string $parserList): ?array
73  {
74  // Return immediately if list is blank:
75  if ($parserList === '') {
76  return null;
77  }
78  $cacheId = 'backend-softRefList-' . md5($parserList);
79  $parserListCache = $this->runtimeCache->get($cacheId);
80  if ($parserListCache !== false) {
81  return $parserListCache;
82  }
83  // Otherwise parse the list:
84  $keyList = ‪GeneralUtility::trimExplode(',', $parserList, true);
85  ‪$output = [];
86  foreach ($keyList as $val) {
87  $reg = [];
88  if (preg_match('/^([[:alnum:]_-]+)\\[(.*)\\]$/', $val, $reg)) {
89  ‪$output[$reg[1]] = ‪GeneralUtility::trimExplode(';', $reg[2], true);
90  } else {
91  ‪$output[$val] = '';
92  }
93  }
94  $this->runtimeCache->set($cacheId, ‪$output);
95  return ‪$output;
96  }
97 
103  public function ‪getParsersBySoftRefParserList(string $softRefParserList, ?array $forcedParameters = null): iterable
104  {
105  foreach ($this->‪explodeSoftRefParserList($softRefParserList) ?? [] as $parserKey => $parameters) {
106  if (!is_array($parameters)) {
107  $parameters = $forcedParameters ?? [];
108  }
109 
110  if (!$this->‪hasSoftReferenceParser($parserKey)) {
111  $this->logger->warning('No soft reference parser exists for the key "{parserKey}".', ['parserKey' => $parserKey]);
112  continue;
113  }
114 
115  ‪$parser = $this->‪getSoftReferenceParser($parserKey);
116  ‪$parser->setParserKey($parserKey, $parameters);
117 
118  yield ‪$parser;
119  }
120  }
121 
126  public function ‪hasSoftReferenceParser(string $softReferenceParserKey): bool
127  {
128  return isset($this->softReferenceParsers[$softReferenceParserKey]);
129  }
130 
144  public function ‪getSoftReferenceParser(string $softReferenceParserKey): ‪SoftReferenceParserInterface
145  {
146  if ($softReferenceParserKey === '') {
147  throw new \InvalidArgumentException(
148  'The soft reference parser key cannot be empty.',
149  1627899274
150  );
151  }
152 
153  if (!$this->‪hasSoftReferenceParser($softReferenceParserKey)) {
154  throw new \OutOfRangeException(
155  sprintf('No soft reference parser found for "%s".', $softReferenceParserKey),
156  1627899342
157  );
158  }
159 
160  $softReferenceParser = $this->softReferenceParsers[$softReferenceParserKey];
161 
162  // @todo in v12 soft reference parsers, not implementing SoftReferenceParserInterface should throw an exception
163  if ($softReferenceParser instanceof ‪SoftReferenceParserInterface) {
164  return $softReferenceParser;
165  }
166 
167  // @todo everything below is deprecated and will be removed in v12
168 
169  trigger_error(
170  sprintf('The class %s does not implement %s. The compatibility layer will be dropped in TYPO3 v12.0.', get_class($softReferenceParser), SoftReferenceParserInterface::class),
171  E_USER_DEPRECATED
172  );
173 
174  if (!method_exists($softReferenceParser, 'findRef')) {
175  throw new \RuntimeException(
176  sprintf('The class %s must implement the findRef method.', get_class($softReferenceParser)),
177  1627899708
178  );
179  }
180 
181  // Build a temporary class acting as a wrapper to call findRef() with the new API.
183  return new class ($softReferenceParser, $softReferenceParserKey) implements ‪SoftReferenceParserInterface {
184  private object ‪$parser;
185  private string $parserKey;
186  private array $parameters = [];
187  public function ‪__construct(object $softReferenceParser, $parserKey)
188  {
189  $this->parser = $softReferenceParser;
190  $this->parserKey = $parserKey;
191  }
192  public function setParserKey(string $parserKey, array $parameters): void
193  {
194  $this->parserKey = $parserKey;
195  $this->parameters = $parameters;
196  }
197  public function getParserKey(): string
198  {
199  return $this->parserKey;
200  }
201  public function parse(
202  string $table,
203  string $field,
204  int $uid,
205  string $content,
206  string $structurePath = ''
208  $result = $this->parser->findRef($table, $field, $uid, $content, $this->parserKey, $this->parameters, $structurePath);
209  if (is_array($result)) {
210  return ‪SoftReferenceParserResult::create($result['content'] ?? '', $result['elements'] ?? []);
211  }
213  }
214  };
215  }
216 
220  public function ‪getSoftReferenceParsers(): array
221  {
223  }
224 }
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static list< string > trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
Definition: GeneralUtility.php:999
‪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\SoftReferenceParserResult\create
‪static create(string $content, array $elements)
Definition: SoftReferenceParserResult.php:48
‪$parser
‪$parser
Definition: annotationChecker.php:111
‪TYPO3\CMS\Core\DataHandling\SoftReference\SoftReferenceParserInterface
Definition: SoftReferenceParserInterface.php:31
‪TYPO3\CMS\Core\DataHandling\SoftReference\SoftReferenceParserFactory\getParsersBySoftRefParserList
‪iterable< SoftReferenceParserInterface > getParsersBySoftRefParserList(string $softRefParserList, ?array $forcedParameters=null)
Definition: SoftReferenceParserFactory.php:103
‪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:59
‪TYPO3\CMS\Core\DataHandling\SoftReference\SoftReferenceParserFactory\hasSoftReferenceParser
‪bool hasSoftReferenceParser(string $softReferenceParserKey)
Definition: SoftReferenceParserFactory.php:126
‪TYPO3\CMS\Core\DataHandling\SoftReference\SoftReferenceParserFactory\$softReferenceParsers
‪array $softReferenceParsers
Definition: SoftReferenceParserFactory.php:29
‪TYPO3\CMS\Core\DataHandling\SoftReference\SoftReferenceParserResult\createWithoutMatches
‪static createWithoutMatches()
Definition: SoftReferenceParserResult.php:62
‪TYPO3\CMS\Core\DataHandling\SoftReference\SoftReferenceParserFactory\getSoftReferenceParser
‪SoftReferenceParserInterface getSoftReferenceParser(string $softReferenceParserKey)
Definition: SoftReferenceParserFactory.php:144
‪$output
‪$output
Definition: annotationChecker.php:122
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:22
‪TYPO3\CMS\Core\DataHandling\SoftReference\SoftReferenceParserResult
Definition: SoftReferenceParserResult.php:43
‪TYPO3\CMS\Core\DataHandling\SoftReference\SoftReferenceParserFactory\explodeSoftRefParserList
‪array null explodeSoftRefParserList(string $parserList)
Definition: SoftReferenceParserFactory.php:72
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\DataHandling\SoftReference\SoftReferenceParserFactory\getSoftReferenceParsers
‪getSoftReferenceParsers()
Definition: SoftReferenceParserFactory.php:220
‪TYPO3\CMS\Core\DataHandling\SoftReference\SoftReferenceParserFactory\__construct
‪__construct(FrontendInterface $runtimeCache, LoggerInterface $logger)
Definition: SoftReferenceParserFactory.php:33
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50