‪TYPO3CMS  ‪main
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 function ‪registerRendererClass($className)
48  {
49  if (!class_exists($className)) {
50  throw new \InvalidArgumentException('The class "' . $className . '" you are trying to register is not available', 1411840171);
51  }
52  if (!in_array(FileRendererInterface::class, class_implements($className) ?: [], true)) {
53  throw new \InvalidArgumentException('The renderer needs to implement the FileRendererInterface', 1411840172);
54  }
55  $this->classNames[] = $className;
56  }
57 
63  public function ‪getRendererInstances()
64  {
65  if ($this->instances === null) {
66  $this->instances = [];
67 
68  // As the result is in reverse order we need to reverse
69  // the array before processing to keep the items with same
70  // priority in the same order as they were added to the registry.
71  ‪$classNames = array_reverse($this->classNames);
72  foreach (‪$classNames as $className) {
73  $object = $this->‪createRendererInstance($className);
74  $this->instances[] = $object;
75  }
76 
77  if (count($this->instances) > 1) {
78  usort($this->instances, [$this, 'compareRendererPriority']);
79  }
80  }
81  return ‪$this->instances;
82  }
83 
90  protected function ‪createRendererInstance($className)
91  {
92  return GeneralUtility::makeInstance($className);
93  }
94 
103  protected function ‪compareRendererPriority(‪FileRendererInterface $rendererA, ‪FileRendererInterface $rendererB)
104  {
105  return $rendererB->‪getPriority() - $rendererA->‪getPriority();
106  }
107 
113  public function ‪getRenderer(‪FileInterface $file)
114  {
115  $matchingFileRenderer = null;
116 
117  foreach ($this->‪getRendererInstances() as $fileRenderer) {
118  if ($fileRenderer->canRender($file)) {
119  $matchingFileRenderer = $fileRenderer;
120  break;
121  }
122  }
123  return $matchingFileRenderer;
124  }
125 }
‪TYPO3\CMS\Core\Resource\Rendering\RendererRegistry\registerRendererClass
‪registerRendererClass($className)
Definition: RendererRegistry.php:45
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:26
‪TYPO3\CMS\Core\Resource\Rendering\RendererRegistry\createRendererInstance
‪FileRendererInterface createRendererInstance($className)
Definition: RendererRegistry.php:88
‪TYPO3\CMS\Core\Resource\Rendering\RendererRegistry\getRenderer
‪FileRendererInterface null getRenderer(FileInterface $file)
Definition: RendererRegistry.php:111
‪TYPO3\CMS\Core\Resource\Rendering\RendererRegistry\compareRendererPriority
‪int compareRendererPriority(FileRendererInterface $rendererA, FileRendererInterface $rendererB)
Definition: RendererRegistry.php:101
‪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\$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:61
‪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:52
‪TYPO3\CMS\Core\Resource\Rendering\RendererRegistry
Definition: RendererRegistry.php:26