‪TYPO3CMS  11.5
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;
21 
26 {
27  private ContainerInterface ‪$container;
28 
29  public function ‪__construct(ContainerInterface ‪$container)
30  {
31  $this->container = ‪$container;
32  }
33 
43  public function ‪process(ContentObjectRenderer $cObject, array $configuration, array $variables)
44  {
45  if (
46  !empty($configuration['dataProcessing.'])
47  && is_array($configuration['dataProcessing.'])
48  ) {
49  $processors = $configuration['dataProcessing.'];
50  $processorKeys = ‪ArrayUtility::filterAndSortByNumericKeys($processors);
51 
52  foreach ($processorKeys as $key) {
53  $dataProcessor = $this->‪getDataProcessor($processors[$key]);
54  $processorConfiguration = $processors[$key . '.'] ?? [];
55  $variables = $dataProcessor->process(
56  $cObject,
57  $configuration,
58  $processorConfiguration,
59  $variables
60  );
61  }
62  }
63 
64  return $variables;
65  }
66 
67  private function ‪getDataProcessor(string $serviceName): ‪DataProcessorInterface
68  {
69  if (!$this->container->has($serviceName)) {
70  // assume serviceName is the class name if it is not available in the container
71  return $this->‪instantiateDataProcessor($serviceName);
72  }
73 
74  $dataProcessor = $this->container->get($serviceName);
75  if (!$dataProcessor instanceof ‪DataProcessorInterface) {
76  throw new \UnexpectedValueException(
77  'Processor with service name "' . $serviceName . '" ' .
78  'must implement interface "' . DataProcessorInterface::class . '"',
79  1635927108
80  );
81  }
82  return $dataProcessor;
83  }
84 
85  private function ‪instantiateDataProcessor(string $className): ‪DataProcessorInterface
86  {
87  if (!class_exists($className)) {
88  throw new \UnexpectedValueException('Processor class or service name "' . $className . '" does not exist!', 1427455378);
89  }
90 
91  if (!in_array(DataProcessorInterface::class, class_implements($className) ?: [], true)) {
92  throw new \UnexpectedValueException(
93  'Processor with class name "' . $className . '" ' .
94  'must implement interface "' . DataProcessorInterface::class . '"',
95  1427455377
96  );
97  }
98  return GeneralUtility::makeInstance($className);
99  }
100 }
‪TYPO3\CMS\Frontend\ContentObject\ContentDataProcessor\instantiateDataProcessor
‪instantiateDataProcessor(string $className)
Definition: ContentDataProcessor.php:85
‪TYPO3\CMS\Frontend\ContentObject\ContentDataProcessor
Definition: ContentDataProcessor.php:26
‪TYPO3\CMS\Frontend\ContentObject
Definition: AbstractContentObject.php:16
‪TYPO3\CMS\Frontend\ContentObject\ContentDataProcessor\__construct
‪__construct(ContainerInterface $container)
Definition: ContentDataProcessor.php:29
‪TYPO3\CMS\Frontend\ContentObject\ContentDataProcessor\getDataProcessor
‪getDataProcessor(string $serviceName)
Definition: ContentDataProcessor.php:67
‪TYPO3\CMS\Frontend\ContentObject\DataProcessorInterface
Definition: DataProcessorInterface.php:23
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:24
‪TYPO3\CMS\Frontend\ContentObject\ContentDataProcessor\$container
‪ContainerInterface $container
Definition: ContentDataProcessor.php:27
‪TYPO3\CMS\Frontend\ContentObject\ContentDataProcessor\process
‪array process(ContentObjectRenderer $cObject, array $configuration, array $variables)
Definition: ContentDataProcessor.php:43
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Utility\ArrayUtility\filterAndSortByNumericKeys
‪static array filterAndSortByNumericKeys($setupArr, $acceptAnyKeys=false)
Definition: ArrayUtility.php:852