‪TYPO3CMS  10.4
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 
46  public static function ‪getInstance()
47  {
48  return GeneralUtility::makeInstance(self::class);
49  }
50 
57  public function ‪registerRendererClass($className)
58  {
59  if (!class_exists($className)) {
60  throw new \InvalidArgumentException('The class "' . $className . '" you are trying to register is not available', 1411840171);
61  }
62  if (!in_array(FileRendererInterface::class, class_implements($className), true)) {
63  throw new \InvalidArgumentException('The renderer needs to implement the FileRendererInterface', 1411840172);
64  }
65  $this->classNames[] = $className;
66  }
67 
73  public function ‪getRendererInstances()
74  {
75  if ($this->instances === null) {
76  $this->instances = [];
77 
78  // As the result is in reverse order we need to reverse
79  // the array before processing to keep the items with same
80  // priority in the same order as they were added to the registry.
81  ‪$classNames = array_reverse($this->classNames);
82  foreach (‪$classNames as $className) {
84  $object = $this->‪createRendererInstance($className);
85  $this->instances[] = $object;
86  }
87 
88  if (count($this->instances) > 1) {
89  usort($this->instances, [$this, 'compareRendererPriority']);
90  }
91  }
92  return ‪$this->instances;
93  }
94 
101  protected function ‪createRendererInstance($className)
102  {
103  return GeneralUtility::makeInstance($className);
104  }
105 
116  protected function ‪compareRendererPriority(‪FileRendererInterface $rendererA, ‪FileRendererInterface $rendererB)
117  {
118  return $rendererB->‪getPriority() - $rendererA->‪getPriority();
119  }
120 
127  public function ‪getRenderer(‪FileInterface $file)
128  {
129  $matchingFileRenderer = null;
130 
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:55
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:22
‪TYPO3\CMS\Core\Resource\Rendering\RendererRegistry\createRendererInstance
‪FileRendererInterface createRendererInstance($className)
Definition: RendererRegistry.php:99
‪TYPO3\CMS\Core\Resource\Rendering\RendererRegistry\getRenderer
‪FileRendererInterface null getRenderer(FileInterface $file)
Definition: RendererRegistry.php:125
‪TYPO3\CMS\Core\Resource\Rendering\RendererRegistry\compareRendererPriority
‪int compareRendererPriority(FileRendererInterface $rendererA, FileRendererInterface $rendererB)
Definition: RendererRegistry.php:114
‪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:44
‪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:71
‪TYPO3\CMS\Core\Resource\Rendering\FileRendererInterface\getPriority
‪int getPriority()
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:23
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Resource\Rendering\RendererRegistry
Definition: RendererRegistry.php:26