‪TYPO3CMS  10.4
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 
21 
28 {
29  public const ‪MARKUP_IDENTIFIER_INLINE = 'inline';
30 
31  abstract protected function ‪generateMarkup(‪Icon $icon, array $options): string;
32  abstract protected function ‪generateInlineMarkup(array $options): string;
33 
38  public function ‪prepareIconMarkup(‪Icon $icon, array $options = []): void
39  {
40  $icon->‪setMarkup($this->‪generateMarkup($icon, $options));
41  $icon->‪setAlternativeMarkup(self::MARKUP_IDENTIFIER_INLINE, $this->‪generateInlineMarkup($options));
42  }
43 
44  protected function ‪getInlineSvg(string $source): string
45  {
46  if (!file_exists($source)) {
47  return '';
48  }
49 
50  $svgContent = file_get_contents($source);
51  if ($svgContent === false) {
52  return '';
53  }
54  $svgContent = (string)preg_replace('/<script[\s\S]*?>[\s\S]*?<\/script>/i', '', $svgContent);
55  // Disables the functionality to allow external entities to be loaded when parsing the XML, must be kept
56  $previousValueOfEntityLoader = null;
57  if (PHP_MAJOR_VERSION < 8) {
58  $previousValueOfEntityLoader = libxml_disable_entity_loader(true);
59  }
60  $svgElement = simplexml_load_string($svgContent);
61  if (PHP_MAJOR_VERSION < 8) {
62  libxml_disable_entity_loader($previousValueOfEntityLoader);
63  }
64  if ($svgElement === false) {
65  return '';
66  }
67 
68  // remove xml version tag
69  $domXml = dom_import_simplexml($svgElement);
70  return $domXml->ownerDocument->saveXML($domXml->ownerDocument->documentElement);
71  }
72 }
‪TYPO3\CMS\Core\Imaging\IconProvider\AbstractSvgIconProvider\generateMarkup
‪generateMarkup(Icon $icon, array $options)
‪TYPO3\CMS\Core\Imaging\IconProvider\AbstractSvgIconProvider\MARKUP_IDENTIFIER_INLINE
‪const MARKUP_IDENTIFIER_INLINE
Definition: AbstractSvgIconProvider.php:29
‪TYPO3\CMS\Core\Imaging\Icon
Definition: Icon.php:26
‪TYPO3\CMS\Core\Imaging\IconProvider\AbstractSvgIconProvider\getInlineSvg
‪getInlineSvg(string $source)
Definition: AbstractSvgIconProvider.php:44
‪TYPO3\CMS\Core\Imaging\IconProvider\AbstractSvgIconProvider
Definition: AbstractSvgIconProvider.php:28
‪TYPO3\CMS\Core\Imaging\IconProvider
Definition: AbstractSvgIconProvider.php:18
‪TYPO3\CMS\Core\Imaging\Icon\setMarkup
‪setMarkup($markup)
Definition: Icon.php:108
‪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:38
‪TYPO3\CMS\Core\Imaging\Icon\setAlternativeMarkup
‪setAlternativeMarkup($markupIdentifier, $markup)
Definition: Icon.php:127