a5969329c7f0b4a206152d9487390065fe269f4d
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Imaging / IconFactory.php
1 <?php
2 namespace TYPO3\CMS\Core\Imaging;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\SingletonInterface;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19
20 /**
21 * The main factory class, which acts as the entrypoint for generating an Icon object which
22 * is responsible for rendering an icon. Checks for the correct icon provider through the IconRegistry.
23 */
24 class IconFactory {
25
26 /**
27 * @var IconRegistry
28 */
29 protected $iconRegistry;
30
31 /**
32 * @param IconRegistry $iconRegistry
33 */
34 public function __construct(IconRegistry $iconRegistry = NULL) {
35 $this->iconRegistry = $iconRegistry ? $iconRegistry : GeneralUtility::makeInstance(IconRegistry::class);
36 }
37
38 /**
39 * @param string $identifier
40 * @param string $size
41 * @param string $overlayIdentifier
42 *
43 * @return Icon
44 */
45 public function getIcon($identifier, $size = Icon::SIZE_DEFAULT, $overlayIdentifier = NULL) {
46 if (!$this->iconRegistry->isRegistered($identifier)) {
47 $identifier = $this->iconRegistry->getDefaultIconIdentifier();
48 }
49
50 $icon = $this->createIcon($identifier, $size, $overlayIdentifier);
51 $iconConfiguration = $this->iconRegistry->getIconConfigurationByIdentifier($identifier);
52 /** @var IconProviderInterface $iconProvider */
53 $iconProvider = GeneralUtility::makeInstance($iconConfiguration['provider']);
54 $iconProvider->prepareIconMarkup($icon, $iconConfiguration['options']);
55 return $icon;
56 }
57
58 /**
59 * Creates an icon object
60 *
61 * @param string $identifier
62 * @param string $size "large" "small" or "default", see the constants of the Icon class
63 * @param string $overlayIdentifier
64 * @return Icon
65 */
66 protected function createIcon($identifier, $size, $overlayIdentifier = NULL) {
67 $icon = GeneralUtility::makeInstance(Icon::class);
68 $icon->setIdentifier($identifier);
69 $icon->setSize($size);
70 if ($overlayIdentifier !== NULL) {
71 $icon->setOverlayIcon($this->getIcon($overlayIdentifier, Icon::SIZE_OVERLAY));
72 }
73 return $icon;
74 }
75 }