‪TYPO3CMS  ‪main
ContentDataProcessor.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use Psr\Container\ContainerInterface;
22 
27 {
28  public function ‪__construct(
29  private readonly ContainerInterface $container,
30  private readonly ‪DataProcessorRegistry $dataProcessorRegistry,
31  ) {}
32 
41  public function ‪process(‪ContentObjectRenderer $cObject, array $configuration, array $variables)
42  {
43  if (
44  !empty($configuration['dataProcessing.'])
45  && is_array($configuration['dataProcessing.'])
46  ) {
47  $processors = $configuration['dataProcessing.'];
48  $processorKeys = ArrayUtility::filterAndSortByNumericKeys($processors);
49 
50  foreach ($processorKeys as $key) {
51  $dataProcessor = $this->dataProcessorRegistry->getDataProcessor($processors[$key])
52  ?? $this->‪getDataProcessor($processors[$key]);
53  $processorConfiguration = $processors[$key . '.'] ?? [];
54  $variables = $dataProcessor->process(
55  $cObject,
56  $configuration,
57  $processorConfiguration,
58  $variables
59  );
60  }
61  }
62 
63  return $variables;
64  }
65 
66  private function ‪getDataProcessor(string $serviceName): ‪DataProcessorInterface
67  {
68  if (!$this->container->has($serviceName)) {
69  // assume serviceName is the class name if it is not available in the container
70  return $this->‪instantiateDataProcessor($serviceName);
71  }
72 
73  $dataProcessor = $this->container->get($serviceName);
74  if (!$dataProcessor instanceof ‪DataProcessorInterface) {
75  throw new \UnexpectedValueException(
76  'Processor with service name "' . $serviceName . '" ' .
77  'must implement interface "' . DataProcessorInterface::class . '"',
78  1635927108
79  );
80  }
81  return $dataProcessor;
82  }
83 
84  private function ‪instantiateDataProcessor(string $className): ‪DataProcessorInterface
85  {
86  if (!class_exists($className)) {
87  throw new \UnexpectedValueException('Processor class or service name "' . $className . '" does not exist!', 1427455378);
88  }
89 
90  if (!in_array(DataProcessorInterface::class, class_implements($className) ?: [], true)) {
91  throw new \UnexpectedValueException(
92  'Processor with class name "' . $className . '" ' .
93  'must implement interface "' . DataProcessorInterface::class . '"',
94  1427455377
95  );
96  }
97  return GeneralUtility::makeInstance($className);
98  }
99 }
‪TYPO3\CMS\Frontend\ContentObject\ContentDataProcessor\instantiateDataProcessor
‪instantiateDataProcessor(string $className)
Definition: ContentDataProcessor.php:84
‪TYPO3\CMS\Frontend\ContentObject\ContentDataProcessor
Definition: ContentDataProcessor.php:27
‪TYPO3\CMS\Frontend\ContentObject
Definition: AbstractContentObject.php:18
‪TYPO3\CMS\Frontend\ContentObject\ContentDataProcessor\getDataProcessor
‪getDataProcessor(string $serviceName)
Definition: ContentDataProcessor.php:66
‪TYPO3\CMS\Frontend\ContentObject\ContentDataProcessor\__construct
‪__construct(private readonly ContainerInterface $container, private readonly DataProcessorRegistry $dataProcessorRegistry,)
Definition: ContentDataProcessor.php:28
‪TYPO3\CMS\Frontend\ContentObject\DataProcessorInterface
Definition: DataProcessorInterface.php:23
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:26
‪TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
Definition: ContentObjectRenderer.php:102
‪TYPO3\CMS\Frontend\ContentObject\ContentDataProcessor\process
‪array process(ContentObjectRenderer $cObject, array $configuration, array $variables)
Definition: ContentDataProcessor.php:41
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Frontend\DataProcessing\DataProcessorRegistry
Definition: DataProcessorRegistry.php:28