5428612aa7c7e90d1d457d16316eeafa2eb76c2e
[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\Type\Icon\IconState;
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 "large", "small" or "default", see the constants of the Icon class
41 * @param string $overlayIdentifier
42 * @param IconState $state
43 *
44 * @return Icon
45 */
46 public function getIcon($identifier, $size = Icon::SIZE_DEFAULT, $overlayIdentifier = NULL, IconState $state = NULL) {
47 if ($this->iconRegistry->isDeprecated($identifier)) {
48 $deprecationSettings = $this->iconRegistry->getDeprecationSettings($identifier);
49 GeneralUtility::deprecationLog(sprintf($deprecationSettings['message'], $identifier));
50 if (!empty($deprecationSettings['replacement'])) {
51 $identifier = $deprecationSettings['replacement'];
52 }
53 }
54 if (!$this->iconRegistry->isRegistered($identifier)) {
55 $identifier = $this->iconRegistry->getDefaultIconIdentifier();
56 }
57
58 $iconConfiguration = $this->iconRegistry->getIconConfigurationByIdentifier($identifier);
59 $iconConfiguration['state'] = $state;
60 $icon = $this->createIcon($identifier, $size, $overlayIdentifier, $iconConfiguration);
61 /** @var IconProviderInterface $iconProvider */
62 $iconProvider = GeneralUtility::makeInstance($iconConfiguration['provider']);
63 $iconProvider->prepareIconMarkup($icon, $iconConfiguration['options']);
64 return $icon;
65 }
66
67 /**
68 * Creates an icon object
69 *
70 * @param string $identifier
71 * @param string $size "large", "small" or "default", see the constants of the Icon class
72 * @param string $overlayIdentifier
73 * @param array $iconConfiguration the icon configuration array
74 * @return Icon
75 */
76 protected function createIcon($identifier, $size, $overlayIdentifier = NULL, array $iconConfiguration) {
77 $icon = GeneralUtility::makeInstance(Icon::class);
78 $icon->setIdentifier($identifier);
79 $icon->setSize($size);
80 $icon->setState($iconConfiguration['state'] ?: new IconState());
81 if ($overlayIdentifier !== NULL) {
82 $icon->setOverlayIcon($this->getIcon($overlayIdentifier, Icon::SIZE_OVERLAY));
83 }
84 if (!empty($iconConfiguration['options']['spinning'])) {
85 $icon->setSpinning(TRUE);
86 }
87 return $icon;
88 }
89 }