‪TYPO3CMS  11.5
AbstractSvgIconProvider.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
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 
19 
24 
31 {
32  public const ‪MARKUP_IDENTIFIER_INLINE = 'inline';
33 
34  abstract protected function ‪generateMarkup(‪Icon $icon, array $options): string;
35  abstract protected function ‪generateInlineMarkup(array $options): string;
36 
41  public function ‪prepareIconMarkup(‪Icon $icon, array $options = []): void
42  {
43  $icon->‪setMarkup($this->‪generateMarkup($icon, $options));
44  $icon->‪setAlternativeMarkup(self::MARKUP_IDENTIFIER_INLINE, $this->‪generateInlineMarkup($options));
45  }
46 
53  protected function ‪getPublicPath(string $source): string
54  {
55  if (‪PathUtility::isExtensionPath($source)) {
57  }
58  // TODO: deprecate non extension resources in icon API
59  return ‪PathUtility::getAbsoluteWebPath(‪PathUtility::isAbsolutePath($source) ? $source : GeneralUtility::getFileAbsFileName($source));
60  }
61 
62  protected function ‪getInlineSvg(string $source): string
63  {
64  if (!file_exists($source)) {
65  return '';
66  }
67 
68  $svgContent = file_get_contents($source);
69  if ($svgContent === false) {
70  return '';
71  }
72  $svgContent = (string)preg_replace('/<script[\s\S]*?>[\s\S]*?<\/script>/i', '', $svgContent);
73  // Disables the functionality to allow external entities to be loaded when parsing the XML, must be kept
74  $previousValueOfEntityLoader = null;
75  if (PHP_MAJOR_VERSION < 8) {
76  $previousValueOfEntityLoader = libxml_disable_entity_loader(true);
77  }
78  $svgElement = simplexml_load_string($svgContent);
79  if (PHP_MAJOR_VERSION < 8) {
80  libxml_disable_entity_loader($previousValueOfEntityLoader);
81  }
82  if ($svgElement === false) {
83  return '';
84  }
85 
86  // remove xml version tag
87  $domXml = dom_import_simplexml($svgElement);
88  return $domXml->ownerDocument->saveXML($domXml->ownerDocument->documentElement);
89  }
90 }
‪TYPO3\CMS\Core\Imaging\IconProvider\AbstractSvgIconProvider\generateMarkup
‪generateMarkup(Icon $icon, array $options)
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:25
‪TYPO3\CMS\Core\Imaging\IconProvider\AbstractSvgIconProvider\MARKUP_IDENTIFIER_INLINE
‪const MARKUP_IDENTIFIER_INLINE
Definition: AbstractSvgIconProvider.php:32
‪TYPO3\CMS\Core\Imaging\Icon
Definition: Icon.php:26
‪TYPO3\CMS\Core\Utility\PathUtility\isExtensionPath
‪static bool isExtensionPath(string $path)
Definition: PathUtility.php:121
‪TYPO3\CMS\Core\Imaging\IconProvider\AbstractSvgIconProvider\getInlineSvg
‪getInlineSvg(string $source)
Definition: AbstractSvgIconProvider.php:62
‪TYPO3\CMS\Core\Imaging\IconProvider\AbstractSvgIconProvider
Definition: AbstractSvgIconProvider.php:31
‪TYPO3\CMS\Core\Imaging\IconProvider
Definition: AbstractSvgIconProvider.php:18
‪TYPO3\CMS\Core\Utility\PathUtility\getPublicResourceWebPath
‪static string getPublicResourceWebPath(string $resourcePath, bool $prefixWithSitePath=true)
Definition: PathUtility.php:98
‪TYPO3\CMS\Core\Imaging\IconProvider\AbstractSvgIconProvider\getPublicPath
‪string getPublicPath(string $source)
Definition: AbstractSvgIconProvider.php:53
‪TYPO3\CMS\Core\Imaging\IconProviderInterface
Definition: IconProviderInterface.php:22
‪TYPO3\CMS\Core\Imaging\Icon\setMarkup
‪setMarkup($markup)
Definition: Icon.php:108
‪TYPO3\CMS\Core\Utility\PathUtility\isAbsolutePath
‪static bool isAbsolutePath($path)
Definition: PathUtility.php:296
‪TYPO3\CMS\Core\Imaging\IconProvider\AbstractSvgIconProvider\generateInlineMarkup
‪generateInlineMarkup(array $options)
‪TYPO3\CMS\Core\Imaging\IconProvider\AbstractSvgIconProvider\prepareIconMarkup
‪prepareIconMarkup(Icon $icon, array $options=[])
Definition: AbstractSvgIconProvider.php:41
‪TYPO3\CMS\Core\Imaging\Icon\setAlternativeMarkup
‪setAlternativeMarkup($markupIdentifier, $markup)
Definition: Icon.php:127
‪TYPO3\CMS\Core\Utility\PathUtility\getAbsoluteWebPath
‪static string getAbsoluteWebPath($targetPath, bool $prefixWithSitePath=true)
Definition: PathUtility.php:51
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50