1<?php
2declare(strict_types = 1);
3namespace TYPO3\CMS\Core\Controller;
4
5/*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18use Psr\Http\Message\ResponseInterface;
19use Psr\Http\Message\ServerRequestInterface;
20use TYPO3\CMS\Core\Http\HtmlResponse;
21use TYPO3\CMS\Core\Imaging\IconFactory;
22use TYPO3\CMS\Core\Imaging\IconRegistry;
23use TYPO3\CMS\Core\Type\Icon\IconState;
24use TYPO3\CMS\Core\Utility\GeneralUtility;
25
26/**
27 * Controller for icon handling
28 *
29 * @internal
30 */
31class IconController
32{
33    /**
34     * @var IconRegistry
35     */
36    protected $iconRegistry;
37
38    /**
39     * @var IconFactory
40     */
41    protected $iconFactory;
42
43    /**
44     * Set up dependencies
45     */
46    public function __construct()
47    {
48        $this->iconRegistry = GeneralUtility::makeInstance(IconRegistry::class);
49        $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
50    }
51
52    /**
53     * @return ResponseInterface
54     * @internal
55     */
56    public function getCacheIdentifier(): ResponseInterface
57    {
58        return new HtmlResponse($this->iconRegistry->getCacheIdentifier());
59    }
60
61    /**
62     * @param ServerRequestInterface $request
63     * @return ResponseInterface
64     * @internal
65     */
66    public function getIcon(ServerRequestInterface $request): ResponseInterface
67    {
68        $parsedBody = $request->getParsedBody();
69        $queryParams = $request->getQueryParams();
70        $requestedIcon = json_decode($parsedBody['icon'] ?? $queryParams['icon'], true);
71
72        list($identifier, $size, $overlayIdentifier, $iconState, $alternativeMarkupIdentifier) = $requestedIcon;
73
74        if (empty($overlayIdentifier)) {
75            $overlayIdentifier = null;
76        }
77
78        $iconState = IconState::cast($iconState);
79        $icon = $this->iconFactory->getIcon($identifier, $size, $overlayIdentifier, $iconState);
80
81        return new HtmlResponse($icon->render($alternativeMarkupIdentifier));
82    }
83}
84