[FEATURE] Add spinning feature for icons
[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\Utility\GeneralUtility;
18
19 /**
20 * The main factory class, which acts as the entrypoint for generating an Icon object which
21 * is responsible for rendering an icon. Checks for the correct icon provider through the IconRegistry.
22 */
23 class IconFactory {
24
25 /**
26 * @var IconRegistry
27 */
28 protected $iconRegistry;
29
30 /**
31 * @param IconRegistry $iconRegistry
32 */
33 public function __construct(IconRegistry $iconRegistry = NULL) {
34 $this->iconRegistry = $iconRegistry ? $iconRegistry : GeneralUtility::makeInstance(IconRegistry::class);
35 }
36
37 /**
38 * @param string $identifier
39 * @param string $size
40 * @param string $overlayIdentifier
41 *
42 * @return Icon
43 */
44 public function getIcon($identifier, $size = Icon::SIZE_DEFAULT, $overlayIdentifier = NULL) {
45 if (!$this->iconRegistry->isRegistered($identifier)) {
46 $identifier = $this->iconRegistry->getDefaultIconIdentifier();
47 }
48
49 $iconConfiguration = $this->iconRegistry->getIconConfigurationByIdentifier($identifier);
50 $icon = $this->createIcon($identifier, $size, $overlayIdentifier, $iconConfiguration);
51 /** @var IconProviderInterface $iconProvider */
52 $iconProvider = GeneralUtility::makeInstance($iconConfiguration['provider']);
53 $iconProvider->prepareIconMarkup($icon, $iconConfiguration['options']);
54 return $icon;
55 }
56
57 /**
58 * Creates an icon object
59 *
60 * @param string $identifier
61 * @param string $size "large" "small" or "default", see the constants of the Icon class
62 * @param string $overlayIdentifier
63 * @param array $iconConfiguration the icon configuration array
64 * @return Icon
65 */
66 protected function createIcon($identifier, $size, $overlayIdentifier = NULL, array $iconConfiguration) {
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 if (!empty($iconConfiguration['options']['spinning'])) {
74 $icon->setSpinning(TRUE);
75 }
76 return $icon;
77 }
78 }