‪TYPO3CMS  11.5
RendererRegistry.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 
21 
26 {
32  protected ‪$classNames = [];
33 
39  protected ‪$instances;
40 
47  public static function ‪getInstance()
48  {
49  trigger_error(__CLASS__ . '::getInstance() will be removed in TYPO3 v12.0. Use Dependency Injection or GeneralUtility::makeInstance() if DI is not possible.', E_USER_DEPRECATED);
50  return GeneralUtility::makeInstance(self::class);
51  }
52 
59  public function ‪registerRendererClass($className)
60  {
61  if (!class_exists($className)) {
62  throw new \InvalidArgumentException('The class "' . $className . '" you are trying to register is not available', 1411840171);
63  }
64  if (!in_array(FileRendererInterface::class, class_implements($className) ?: [], true)) {
65  throw new \InvalidArgumentException('The renderer needs to implement the FileRendererInterface', 1411840172);
66  }
67  $this->classNames[] = $className;
68  }
69 
75  public function ‪getRendererInstances()
76  {
77  if ($this->instances === null) {
78  $this->instances = [];
79 
80  // As the result is in reverse order we need to reverse
81  // the array before processing to keep the items with same
82  // priority in the same order as they were added to the registry.
83  ‪$classNames = array_reverse($this->classNames);
84  foreach (‪$classNames as $className) {
85  $object = $this->‪createRendererInstance($className);
86  $this->instances[] = $object;
87  }
88 
89  if (count($this->instances) > 1) {
90  usort($this->instances, [$this, 'compareRendererPriority']);
91  }
92  }
93  return ‪$this->instances;
94  }
95 
102  protected function ‪createRendererInstance($className)
103  {
104  return GeneralUtility::makeInstance($className);
105  }
106 
117  protected function ‪compareRendererPriority(‪FileRendererInterface $rendererA, ‪FileRendererInterface $rendererB)
118  {
119  return $rendererB->‪getPriority() - $rendererA->‪getPriority();
120  }
121 
128  public function ‪getRenderer(‪FileInterface $file)
129  {
130  $matchingFileRenderer = null;
131 
132  foreach ($this->‪getRendererInstances() as $fileRenderer) {
133  if ($fileRenderer->canRender($file)) {
134  $matchingFileRenderer = $fileRenderer;
135  break;
136  }
137  }
138  return $matchingFileRenderer;
139  }
140 }
‪TYPO3\CMS\Core\Resource\Rendering\RendererRegistry\registerRendererClass
‪registerRendererClass($className)
Definition: RendererRegistry.php:57
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:22
‪TYPO3\CMS\Core\Resource\Rendering\RendererRegistry\createRendererInstance
‪FileRendererInterface createRendererInstance($className)
Definition: RendererRegistry.php:100
‪TYPO3\CMS\Core\Resource\Rendering\RendererRegistry\getRenderer
‪FileRendererInterface null getRenderer(FileInterface $file)
Definition: RendererRegistry.php:126
‪TYPO3\CMS\Core\Resource\Rendering\RendererRegistry\compareRendererPriority
‪int compareRendererPriority(FileRendererInterface $rendererA, FileRendererInterface $rendererB)
Definition: RendererRegistry.php:115
‪TYPO3\CMS\Core\Resource\Rendering\RendererRegistry\$classNames
‪array $classNames
Definition: RendererRegistry.php:31
‪TYPO3\CMS\Core\Resource\Rendering
Definition: AudioTagRenderer.php:16
‪TYPO3\CMS\Core\Resource\Rendering\RendererRegistry\getInstance
‪static RendererRegistry getInstance()
Definition: RendererRegistry.php:45
‪TYPO3\CMS\Core\Resource\Rendering\RendererRegistry\$instances
‪FileRendererInterface[] $instances
Definition: RendererRegistry.php:37
‪TYPO3\CMS\Core\Resource\Rendering\FileRendererInterface
Definition: FileRendererInterface.php:25
‪TYPO3\CMS\Core\Resource\Rendering\RendererRegistry\getRendererInstances
‪FileRendererInterface[] getRendererInstances()
Definition: RendererRegistry.php:73
‪TYPO3\CMS\Core\Resource\Rendering\FileRendererInterface\getPriority
‪int getPriority()
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:22
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Resource\Rendering\RendererRegistry
Definition: RendererRegistry.php:26