@@ -18,235 +18,235 @@ |
||
18 | 18 | */ |
19 | 19 | class SvgStoreService implements \TYPO3\CMS\Core\SingletonInterface |
20 | 20 | { |
21 | - /** |
|
22 | - * SVG-Sprite relativ storage directory. |
|
23 | - */ |
|
24 | - protected string $outputDir = '/typo3temp/assets/svg/'; |
|
25 | - |
|
26 | - /** |
|
27 | - * TYPO3 absolute path to public web. |
|
28 | - */ |
|
29 | - protected string $sitePath = ''; |
|
30 | - |
|
31 | - /** |
|
32 | - * Final TYPO3 Frontend-Cache object. |
|
33 | - */ |
|
34 | - protected FrontendInterface $svgCache; |
|
35 | - |
|
36 | - /** |
|
37 | - * Cached SVG-Sprite relativ file path. |
|
38 | - */ |
|
39 | - protected string $spritePath = ''; |
|
40 | - |
|
41 | - /** |
|
42 | - * Cached used SVG files (incl. defs). |
|
43 | - */ |
|
44 | - protected array $svgFileArr = []; |
|
45 | - |
|
46 | - /** |
|
47 | - * Final SVG-Sprite Vectors. |
|
48 | - */ |
|
49 | - protected array $svgs = []; |
|
50 | - |
|
51 | - /** |
|
52 | - * Final SVG-Sprite Styles. |
|
53 | - */ |
|
54 | - protected array $styl = []; // ToFix ; https://stackoverflow.com/questions/39583880/external-svg-fails-to-apply-internal-css |
|
55 | - |
|
56 | - /** |
|
57 | - * Final SVG-Sprite Objects. |
|
58 | - */ |
|
59 | - protected array $defs = []; // ToFix ; https://bugs.chromium.org/p/chromium/issues/detail?id=751733#c14 |
|
60 | - |
|
61 | - public function __construct() |
|
62 | - { |
|
63 | - $this->sitePath = Environment::getPublicPath(); // [^/]$ |
|
64 | - $this->svgCache = GeneralUtility::makeInstance(CacheManager::class)->getCache('svgstore'); |
|
65 | - |
|
66 | - $this->spritePath = $this->svgCache->get('spritePath') ?: ''; |
|
67 | - $this->svgFileArr = $this->svgCache->get('svgFileArr') ?: []; |
|
68 | - |
|
69 | - if (empty($this->spritePath) && !$this->populateCache()) { |
|
70 | - throw new \Exception('could not write file: ' . $this->sitePath . $this->spritePath); |
|
71 | - } |
|
72 | - |
|
73 | - if (!file_exists($this->sitePath . $this->spritePath)) { |
|
74 | - throw new \Exception('file does not exists: ' . $this->sitePath . $this->spritePath); |
|
75 | - } |
|
76 | - } |
|
77 | - |
|
78 | - public function process(string $html): string |
|
79 | - { |
|
80 | - if (empty($this->svgFileArr)) { |
|
81 | - return $html; |
|
82 | - } |
|
83 | - |
|
84 | - if ($GLOBALS['TSFE']->config['config']['disableAllHeaderCode'] ?? false) { |
|
85 | - $dom = ['head' => '', 'body' => $html]; |
|
86 | - } elseif (!preg_match('/(?<head>.+?<\/head>)(?<body>.+)/s', $html, $dom)) { |
|
87 | - return $html; |
|
88 | - } |
|
89 | - |
|
90 | - // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#attributes |
|
91 | - $dom['body'] = preg_replace_callback('/<img(?<pre>[^>]*)src="(?:https?:)?(?:\/\/[^\/]+?)?(?<src>\/[^"]+\.svg)"(?<post>[^>]*?)[\s\/]*>(?!\s*<\/picture>)/s', function (array $match): string { // ^[/] |
|
92 | - if (!isset($this->svgFileArr[$match['src']])) { // check usage |
|
93 | - return $match[0]; |
|
94 | - } |
|
95 | - $attr = preg_replace('/\s(?:alt|ismap|loading|title|sizes|srcset|usemap|crossorigin|decoding|fetchpriority|referrerpolicy)="[^"]*"/', '', $match['pre'] . $match['post']); // cleanup |
|
96 | - |
|
97 | - return \sprintf('<svg %s %s><use href="%s#%s"/></svg>', $this->svgFileArr[$match['src']]['attr'], trim($attr), $this->spritePath, $this->convertFilePath($match['src'])); |
|
98 | - }, $dom['body']); |
|
99 | - |
|
100 | - // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/object#attributes |
|
101 | - $dom['body'] = preg_replace_callback('/<object(?<pre>[^>]*)data="(?<data>\/[^"]+\.svg)"(?<post>[^>]*?)[\s\/]*>(?:<\/object>)/s', function (array $match): string { // ^[/] |
|
102 | - if (!isset($this->svgFileArr[$match['data']])) { // check usage |
|
103 | - return $match[0]; |
|
104 | - } |
|
105 | - $attr = preg_replace('/\s(?:form|name|type|usemap)="[^"]*"/', '', $match['pre'] . $match['post']); // cleanup |
|
106 | - |
|
107 | - return \sprintf('<svg %s %s><use href="%s#%s"/></svg>', $this->svgFileArr[$match['data']]['attr'], trim($attr), $this->spritePath, $this->convertFilePath($match['data'])); |
|
108 | - }, $dom['body']); |
|
109 | - |
|
110 | - return $dom['head'] . $dom['body']; |
|
111 | - } |
|
112 | - |
|
113 | - private function convertFilePath(string $path): string |
|
114 | - { |
|
115 | - return preg_replace('/.svg$|[^\w\-]/', '', str_replace('/', '-', ltrim($path, '/'))); // ^[^/] |
|
116 | - } |
|
117 | - |
|
118 | - private function addFileToSpriteArr(string $hash, string $path, array $attr = []): ?array |
|
119 | - { |
|
120 | - if (!file_exists($this->sitePath . $path)) { |
|
121 | - return null; |
|
122 | - } |
|
123 | - |
|
124 | - $svg = file_get_contents($this->sitePath . $path); |
|
125 | - |
|
126 | - if (preg_match('/(?:;base64|i:a?i?pgf)/', $svg)) { // noop! |
|
127 | - return null; |
|
128 | - } |
|
129 | - |
|
130 | - if (preg_match('/<(?:style|defs)|url\(/', $svg)) { |
|
131 | - return null; // check links @ __construct |
|
132 | - } |
|
133 | - |
|
134 | - // https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/xlink:href |
|
135 | - $svg = preg_replace('/^.*?<svg|\s*(<\/svg>)(?!.*\1).*$|xlink:|\s(?:(?:version|xmlns)|(?:[a-z\-]+\:[a-z\-]+))="[^"]*"/s', '', $svg); // cleanup |
|
136 | - |
|
137 | - // $svg = preg_replace('/(?<=(?:id|class)=")/', $hash.'__', $svg); // extend IDs |
|
138 | - // $svg = preg_replace('/(?<=href="|url\()#/', $hash.'__', $svg); // recover IDs |
|
139 | - |
|
140 | - // $svg = preg_replace_callback('/<style[^>]*>(?<styl>.+?)<\/style>|<defs[^>]*>(?<defs>.+?)<\/defs>/s', function(array $match) use($hash): string { |
|
141 | - // |
|
142 | - // if(isset($match['styl'])) |
|
143 | - // { |
|
144 | - // $this->styl[] = preg_replace('/\s*(\.|#){1}(.+?)\s*\{/', '$1'.$hash.'__$2{', $match['styl']); // patch CSS # https://mathiasbynens.be/notes/css-escapes |
|
145 | - // } |
|
146 | - // if(isset($match['defs'])) |
|
147 | - // { |
|
148 | - // $this->defs[] = trim($match['defs']); |
|
149 | - // } |
|
150 | - // return ''; |
|
151 | - // }, $svg); |
|
152 | - |
|
153 | - // https://developer.mozilla.org/en-US/docs/Web/SVG/Element/svg#attributes |
|
154 | - $svg = preg_replace_callback('/([^>]*)\s*(?=>)/s', function (array $match) use (&$attr): string { |
|
155 | - if (false === preg_match_all('/(?!\s)(?<attr>[a-z\-]+)="\s*(?<value>[^"]+)\s*"/i', $match[1], $matches)) { |
|
156 | - return $match[0]; |
|
157 | - } |
|
158 | - foreach ($matches['attr'] as $index => $attribute) { |
|
159 | - switch ($attribute) { |
|
160 | - case 'id': |
|
161 | - case 'width': |
|
162 | - case 'height': |
|
163 | - unset($matches[0][$index]); |
|
164 | - break; |
|
165 | - |
|
166 | - case 'viewBox': |
|
167 | - if (false !== preg_match('/(?<minX>[-+]?[\d\.]+)\s(?<minY>[-+]?[\d\.]+)\s\+?(?<width>[\d\.]+)\s\+?(?<height>[\d\.]+)/', $matches['value'][$index], $match)) { |
|
168 | - $attr[] = \sprintf('%s="%s %s %s %s"', $attribute, $match['minX'], $match['minY'], $match['width'], $match['height']); // save! |
|
169 | - } |
|
170 | - } |
|
171 | - } |
|
172 | - |
|
173 | - return implode(' ', $matches[0]); |
|
174 | - }, $svg, 1); |
|
175 | - |
|
176 | - if (empty($attr)) { |
|
177 | - return null; |
|
178 | - } |
|
179 | - |
|
180 | - $this->svgs[] = \sprintf('id="%s" %s', $this->convertFilePath($path), $svg); // prepend ID |
|
181 | - |
|
182 | - return ['attr' => implode(' ', $attr), 'hash' => $hash]; |
|
183 | - } |
|
184 | - |
|
185 | - private function populateCache(): bool |
|
186 | - { |
|
187 | - $storageArr = GeneralUtility::makeInstance(StorageRepository::class)->findByStorageType('Local'); |
|
188 | - foreach ($storageArr as $storage) { |
|
189 | - $storageConfig = $storage->getConfiguration(); |
|
190 | - if (!\is_array($storageConfig) || !isset($storageConfig['pathType'], $storageConfig['basePath'])) { |
|
191 | - continue; |
|
192 | - } |
|
193 | - if ('relative' == $storageConfig['pathType']) { |
|
194 | - $storageArr[$storage->getUid()] = rtrim($storageConfig['basePath'], '/'); // [^/]$ |
|
195 | - } |
|
196 | - } |
|
197 | - unset($storageArr[0]); // keep! |
|
198 | - |
|
199 | - $fileArr = GeneralUtility::makeInstance(SvgFileRepository::class)->findAllByStorageUids(array_keys($storageArr)); |
|
200 | - foreach ($fileArr as $file) { |
|
201 | - $file['path'] = '/' . $storageArr[$file['storage']] . $file['identifier']; // ^[/] |
|
202 | - $file['defs'] = $this->addFileToSpriteArr($file['sha1'], $file['path']); |
|
203 | - |
|
204 | - if (null !== $file['defs']) { |
|
205 | - $this->svgFileArr[$file['path']] = $file['defs']; |
|
206 | - } |
|
207 | - } |
|
208 | - |
|
209 | - if (empty($this->svgFileArr)) { |
|
210 | - return true; |
|
211 | - } |
|
212 | - |
|
213 | - $svg = preg_replace_callback( |
|
214 | - '/<use(?<pre>.*?)(?:xlink:)?href="(?<href>\/.+?\.svg)(?:#[^"]*?)?"(?<post>.*?)[\s\/]*>(?:<\/use>)?/s', |
|
215 | - function (array $match): string { |
|
216 | - if (!isset($this->svgFileArr[$match['href']])) { // check usage |
|
217 | - return $match[0]; |
|
218 | - } |
|
219 | - |
|
220 | - return \sprintf('<use%s href="#%s"/>', $match['pre'] . $match['post'], $this->convertFilePath($match['href'])); |
|
221 | - }, |
|
222 | - '<svg xmlns="http://www.w3.org/2000/svg">' |
|
223 | - // ."\n<style>\n".implode("\n", $this->styl)."\n</style>" |
|
224 | - // ."\n<defs>\n".implode("\n", $this->defs)."\n</defs>" |
|
225 | - . "\n<symbol " . implode("</symbol>\n<symbol ", $this->svgs) . "</symbol>\n" |
|
226 | - . '</svg>' |
|
227 | - ); |
|
228 | - |
|
229 | - if ($GLOBALS['TSFE']->config['config']['sourceopt.']['formatHtml'] ?? false) { |
|
230 | - $svg = preg_replace('/(?<=>)\s+(?=<)/', '', $svg); // remove emptiness |
|
231 | - $svg = preg_replace('/[\t\v]/', ' ', $svg); // prepare shrinkage |
|
232 | - $svg = preg_replace('/\s{2,}/', ' ', $svg); // shrink whitespace |
|
233 | - } |
|
234 | - |
|
235 | - $svg = preg_replace('/<([a-z\-]+)\s*(\/|>\s*<\/\1)>\s*|\s+(?=\/>)/i', '', $svg); // remove emtpy TAGs & shorten endings |
|
236 | - $svg = preg_replace('/<((circle|ellipse|line|path|polygon|polyline|rect|stop|use)\s[^>]+?)\s*>\s*<\/\2>/', '<$1/>', $svg); // shorten/minify TAG syntax |
|
237 | - |
|
238 | - if (!is_dir($this->sitePath . $this->outputDir)) { |
|
239 | - GeneralUtility::mkdir_deep($this->sitePath . $this->outputDir); |
|
240 | - } |
|
241 | - |
|
242 | - $this->spritePath = $this->outputDir . hash('sha1', serialize($this->svgFileArr)) . '.svg'; |
|
243 | - if (false === file_put_contents($this->sitePath . $this->spritePath, $svg)) { |
|
244 | - return false; |
|
245 | - } |
|
246 | - |
|
247 | - $this->svgCache->set('spritePath', $this->spritePath); |
|
248 | - $this->svgCache->set('svgFileArr', $this->svgFileArr); |
|
249 | - |
|
250 | - return true; |
|
251 | - } |
|
21 | + /** |
|
22 | + * SVG-Sprite relativ storage directory. |
|
23 | + */ |
|
24 | + protected string $outputDir = '/typo3temp/assets/svg/'; |
|
25 | + |
|
26 | + /** |
|
27 | + * TYPO3 absolute path to public web. |
|
28 | + */ |
|
29 | + protected string $sitePath = ''; |
|
30 | + |
|
31 | + /** |
|
32 | + * Final TYPO3 Frontend-Cache object. |
|
33 | + */ |
|
34 | + protected FrontendInterface $svgCache; |
|
35 | + |
|
36 | + /** |
|
37 | + * Cached SVG-Sprite relativ file path. |
|
38 | + */ |
|
39 | + protected string $spritePath = ''; |
|
40 | + |
|
41 | + /** |
|
42 | + * Cached used SVG files (incl. defs). |
|
43 | + */ |
|
44 | + protected array $svgFileArr = []; |
|
45 | + |
|
46 | + /** |
|
47 | + * Final SVG-Sprite Vectors. |
|
48 | + */ |
|
49 | + protected array $svgs = []; |
|
50 | + |
|
51 | + /** |
|
52 | + * Final SVG-Sprite Styles. |
|
53 | + */ |
|
54 | + protected array $styl = []; // ToFix ; https://stackoverflow.com/questions/39583880/external-svg-fails-to-apply-internal-css |
|
55 | + |
|
56 | + /** |
|
57 | + * Final SVG-Sprite Objects. |
|
58 | + */ |
|
59 | + protected array $defs = []; // ToFix ; https://bugs.chromium.org/p/chromium/issues/detail?id=751733#c14 |
|
60 | + |
|
61 | + public function __construct() |
|
62 | + { |
|
63 | + $this->sitePath = Environment::getPublicPath(); // [^/]$ |
|
64 | + $this->svgCache = GeneralUtility::makeInstance(CacheManager::class)->getCache('svgstore'); |
|
65 | + |
|
66 | + $this->spritePath = $this->svgCache->get('spritePath') ?: ''; |
|
67 | + $this->svgFileArr = $this->svgCache->get('svgFileArr') ?: []; |
|
68 | + |
|
69 | + if (empty($this->spritePath) && !$this->populateCache()) { |
|
70 | + throw new \Exception('could not write file: ' . $this->sitePath . $this->spritePath); |
|
71 | + } |
|
72 | + |
|
73 | + if (!file_exists($this->sitePath . $this->spritePath)) { |
|
74 | + throw new \Exception('file does not exists: ' . $this->sitePath . $this->spritePath); |
|
75 | + } |
|
76 | + } |
|
77 | + |
|
78 | + public function process(string $html): string |
|
79 | + { |
|
80 | + if (empty($this->svgFileArr)) { |
|
81 | + return $html; |
|
82 | + } |
|
83 | + |
|
84 | + if ($GLOBALS['TSFE']->config['config']['disableAllHeaderCode'] ?? false) { |
|
85 | + $dom = ['head' => '', 'body' => $html]; |
|
86 | + } elseif (!preg_match('/(?<head>.+?<\/head>)(?<body>.+)/s', $html, $dom)) { |
|
87 | + return $html; |
|
88 | + } |
|
89 | + |
|
90 | + // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#attributes |
|
91 | + $dom['body'] = preg_replace_callback('/<img(?<pre>[^>]*)src="(?:https?:)?(?:\/\/[^\/]+?)?(?<src>\/[^"]+\.svg)"(?<post>[^>]*?)[\s\/]*>(?!\s*<\/picture>)/s', function (array $match): string { // ^[/] |
|
92 | + if (!isset($this->svgFileArr[$match['src']])) { // check usage |
|
93 | + return $match[0]; |
|
94 | + } |
|
95 | + $attr = preg_replace('/\s(?:alt|ismap|loading|title|sizes|srcset|usemap|crossorigin|decoding|fetchpriority|referrerpolicy)="[^"]*"/', '', $match['pre'] . $match['post']); // cleanup |
|
96 | + |
|
97 | + return \sprintf('<svg %s %s><use href="%s#%s"/></svg>', $this->svgFileArr[$match['src']]['attr'], trim($attr), $this->spritePath, $this->convertFilePath($match['src'])); |
|
98 | + }, $dom['body']); |
|
99 | + |
|
100 | + // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/object#attributes |
|
101 | + $dom['body'] = preg_replace_callback('/<object(?<pre>[^>]*)data="(?<data>\/[^"]+\.svg)"(?<post>[^>]*?)[\s\/]*>(?:<\/object>)/s', function (array $match): string { // ^[/] |
|
102 | + if (!isset($this->svgFileArr[$match['data']])) { // check usage |
|
103 | + return $match[0]; |
|
104 | + } |
|
105 | + $attr = preg_replace('/\s(?:form|name|type|usemap)="[^"]*"/', '', $match['pre'] . $match['post']); // cleanup |
|
106 | + |
|
107 | + return \sprintf('<svg %s %s><use href="%s#%s"/></svg>', $this->svgFileArr[$match['data']]['attr'], trim($attr), $this->spritePath, $this->convertFilePath($match['data'])); |
|
108 | + }, $dom['body']); |
|
109 | + |
|
110 | + return $dom['head'] . $dom['body']; |
|
111 | + } |
|
112 | + |
|
113 | + private function convertFilePath(string $path): string |
|
114 | + { |
|
115 | + return preg_replace('/.svg$|[^\w\-]/', '', str_replace('/', '-', ltrim($path, '/'))); // ^[^/] |
|
116 | + } |
|
117 | + |
|
118 | + private function addFileToSpriteArr(string $hash, string $path, array $attr = []): ?array |
|
119 | + { |
|
120 | + if (!file_exists($this->sitePath . $path)) { |
|
121 | + return null; |
|
122 | + } |
|
123 | + |
|
124 | + $svg = file_get_contents($this->sitePath . $path); |
|
125 | + |
|
126 | + if (preg_match('/(?:;base64|i:a?i?pgf)/', $svg)) { // noop! |
|
127 | + return null; |
|
128 | + } |
|
129 | + |
|
130 | + if (preg_match('/<(?:style|defs)|url\(/', $svg)) { |
|
131 | + return null; // check links @ __construct |
|
132 | + } |
|
133 | + |
|
134 | + // https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/xlink:href |
|
135 | + $svg = preg_replace('/^.*?<svg|\s*(<\/svg>)(?!.*\1).*$|xlink:|\s(?:(?:version|xmlns)|(?:[a-z\-]+\:[a-z\-]+))="[^"]*"/s', '', $svg); // cleanup |
|
136 | + |
|
137 | + // $svg = preg_replace('/(?<=(?:id|class)=")/', $hash.'__', $svg); // extend IDs |
|
138 | + // $svg = preg_replace('/(?<=href="|url\()#/', $hash.'__', $svg); // recover IDs |
|
139 | + |
|
140 | + // $svg = preg_replace_callback('/<style[^>]*>(?<styl>.+?)<\/style>|<defs[^>]*>(?<defs>.+?)<\/defs>/s', function(array $match) use($hash): string { |
|
141 | + // |
|
142 | + // if(isset($match['styl'])) |
|
143 | + // { |
|
144 | + // $this->styl[] = preg_replace('/\s*(\.|#){1}(.+?)\s*\{/', '$1'.$hash.'__$2{', $match['styl']); // patch CSS # https://mathiasbynens.be/notes/css-escapes |
|
145 | + // } |
|
146 | + // if(isset($match['defs'])) |
|
147 | + // { |
|
148 | + // $this->defs[] = trim($match['defs']); |
|
149 | + // } |
|
150 | + // return ''; |
|
151 | + // }, $svg); |
|
152 | + |
|
153 | + // https://developer.mozilla.org/en-US/docs/Web/SVG/Element/svg#attributes |
|
154 | + $svg = preg_replace_callback('/([^>]*)\s*(?=>)/s', function (array $match) use (&$attr): string { |
|
155 | + if (false === preg_match_all('/(?!\s)(?<attr>[a-z\-]+)="\s*(?<value>[^"]+)\s*"/i', $match[1], $matches)) { |
|
156 | + return $match[0]; |
|
157 | + } |
|
158 | + foreach ($matches['attr'] as $index => $attribute) { |
|
159 | + switch ($attribute) { |
|
160 | + case 'id': |
|
161 | + case 'width': |
|
162 | + case 'height': |
|
163 | + unset($matches[0][$index]); |
|
164 | + break; |
|
165 | + |
|
166 | + case 'viewBox': |
|
167 | + if (false !== preg_match('/(?<minX>[-+]?[\d\.]+)\s(?<minY>[-+]?[\d\.]+)\s\+?(?<width>[\d\.]+)\s\+?(?<height>[\d\.]+)/', $matches['value'][$index], $match)) { |
|
168 | + $attr[] = \sprintf('%s="%s %s %s %s"', $attribute, $match['minX'], $match['minY'], $match['width'], $match['height']); // save! |
|
169 | + } |
|
170 | + } |
|
171 | + } |
|
172 | + |
|
173 | + return implode(' ', $matches[0]); |
|
174 | + }, $svg, 1); |
|
175 | + |
|
176 | + if (empty($attr)) { |
|
177 | + return null; |
|
178 | + } |
|
179 | + |
|
180 | + $this->svgs[] = \sprintf('id="%s" %s', $this->convertFilePath($path), $svg); // prepend ID |
|
181 | + |
|
182 | + return ['attr' => implode(' ', $attr), 'hash' => $hash]; |
|
183 | + } |
|
184 | + |
|
185 | + private function populateCache(): bool |
|
186 | + { |
|
187 | + $storageArr = GeneralUtility::makeInstance(StorageRepository::class)->findByStorageType('Local'); |
|
188 | + foreach ($storageArr as $storage) { |
|
189 | + $storageConfig = $storage->getConfiguration(); |
|
190 | + if (!\is_array($storageConfig) || !isset($storageConfig['pathType'], $storageConfig['basePath'])) { |
|
191 | + continue; |
|
192 | + } |
|
193 | + if ('relative' == $storageConfig['pathType']) { |
|
194 | + $storageArr[$storage->getUid()] = rtrim($storageConfig['basePath'], '/'); // [^/]$ |
|
195 | + } |
|
196 | + } |
|
197 | + unset($storageArr[0]); // keep! |
|
198 | + |
|
199 | + $fileArr = GeneralUtility::makeInstance(SvgFileRepository::class)->findAllByStorageUids(array_keys($storageArr)); |
|
200 | + foreach ($fileArr as $file) { |
|
201 | + $file['path'] = '/' . $storageArr[$file['storage']] . $file['identifier']; // ^[/] |
|
202 | + $file['defs'] = $this->addFileToSpriteArr($file['sha1'], $file['path']); |
|
203 | + |
|
204 | + if (null !== $file['defs']) { |
|
205 | + $this->svgFileArr[$file['path']] = $file['defs']; |
|
206 | + } |
|
207 | + } |
|
208 | + |
|
209 | + if (empty($this->svgFileArr)) { |
|
210 | + return true; |
|
211 | + } |
|
212 | + |
|
213 | + $svg = preg_replace_callback( |
|
214 | + '/<use(?<pre>.*?)(?:xlink:)?href="(?<href>\/.+?\.svg)(?:#[^"]*?)?"(?<post>.*?)[\s\/]*>(?:<\/use>)?/s', |
|
215 | + function (array $match): string { |
|
216 | + if (!isset($this->svgFileArr[$match['href']])) { // check usage |
|
217 | + return $match[0]; |
|
218 | + } |
|
219 | + |
|
220 | + return \sprintf('<use%s href="#%s"/>', $match['pre'] . $match['post'], $this->convertFilePath($match['href'])); |
|
221 | + }, |
|
222 | + '<svg xmlns="http://www.w3.org/2000/svg">' |
|
223 | + // ."\n<style>\n".implode("\n", $this->styl)."\n</style>" |
|
224 | + // ."\n<defs>\n".implode("\n", $this->defs)."\n</defs>" |
|
225 | + . "\n<symbol " . implode("</symbol>\n<symbol ", $this->svgs) . "</symbol>\n" |
|
226 | + . '</svg>' |
|
227 | + ); |
|
228 | + |
|
229 | + if ($GLOBALS['TSFE']->config['config']['sourceopt.']['formatHtml'] ?? false) { |
|
230 | + $svg = preg_replace('/(?<=>)\s+(?=<)/', '', $svg); // remove emptiness |
|
231 | + $svg = preg_replace('/[\t\v]/', ' ', $svg); // prepare shrinkage |
|
232 | + $svg = preg_replace('/\s{2,}/', ' ', $svg); // shrink whitespace |
|
233 | + } |
|
234 | + |
|
235 | + $svg = preg_replace('/<([a-z\-]+)\s*(\/|>\s*<\/\1)>\s*|\s+(?=\/>)/i', '', $svg); // remove emtpy TAGs & shorten endings |
|
236 | + $svg = preg_replace('/<((circle|ellipse|line|path|polygon|polyline|rect|stop|use)\s[^>]+?)\s*>\s*<\/\2>/', '<$1/>', $svg); // shorten/minify TAG syntax |
|
237 | + |
|
238 | + if (!is_dir($this->sitePath . $this->outputDir)) { |
|
239 | + GeneralUtility::mkdir_deep($this->sitePath . $this->outputDir); |
|
240 | + } |
|
241 | + |
|
242 | + $this->spritePath = $this->outputDir . hash('sha1', serialize($this->svgFileArr)) . '.svg'; |
|
243 | + if (false === file_put_contents($this->sitePath . $this->spritePath, $svg)) { |
|
244 | + return false; |
|
245 | + } |
|
246 | + |
|
247 | + $this->svgCache->set('spritePath', $this->spritePath); |
|
248 | + $this->svgCache->set('svgFileArr', $this->svgFileArr); |
|
249 | + |
|
250 | + return true; |
|
251 | + } |
|
252 | 252 | } |
@@ -14,44 +14,44 @@ |
||
14 | 14 | */ |
15 | 15 | class SvgFileRepository |
16 | 16 | { |
17 | - /** |
|
18 | - * Retrieves all used SVGs within given storage-array. |
|
19 | - */ |
|
20 | - public function findAllByStorageUids(array $storageUids): \Traversable |
|
21 | - { |
|
22 | - $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_file'); |
|
17 | + /** |
|
18 | + * Retrieves all used SVGs within given storage-array. |
|
19 | + */ |
|
20 | + public function findAllByStorageUids(array $storageUids): \Traversable |
|
21 | + { |
|
22 | + $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_file'); |
|
23 | 23 | |
24 | - return $queryBuilder |
|
25 | - ->select('sys_file.storage', 'sys_file.identifier', 'sys_file.sha1') |
|
26 | - ->from('sys_file') |
|
27 | - ->innerJoin( |
|
28 | - 'sys_file', |
|
29 | - 'sys_file_reference', |
|
30 | - 'sys_file_reference', |
|
31 | - $queryBuilder->expr()->eq( |
|
32 | - 'sys_file_reference.uid_local', |
|
33 | - $queryBuilder->quoteIdentifier('sys_file.uid') |
|
34 | - ) |
|
35 | - ) |
|
36 | - ->where( |
|
37 | - $queryBuilder->expr()->in( |
|
38 | - 'sys_file.storage', |
|
39 | - $queryBuilder->createNamedParameter($storageUids, \TYPO3\CMS\Core\Database\Connection::PARAM_INT_ARRAY) |
|
40 | - ), |
|
41 | - $queryBuilder->expr()->lt( |
|
42 | - 'sys_file.size', |
|
43 | - $queryBuilder->createNamedParameter((int) $GLOBALS['TSFE']->config['config']['svgstore.']['fileSize'] ?? null, \TYPO3\CMS\Core\Database\Connection::PARAM_INT) |
|
44 | - ), |
|
45 | - $queryBuilder->expr()->eq( |
|
46 | - 'sys_file.mime_type', |
|
47 | - $queryBuilder->createNamedParameter('image/svg+xml', \TYPO3\CMS\Core\Database\Connection::PARAM_STR) |
|
48 | - ) |
|
49 | - ) |
|
50 | - ->groupBy('sys_file.uid', 'sys_file.storage', 'sys_file.identifier', 'sys_file.sha1') |
|
51 | - ->orderBy('sys_file.storage') |
|
52 | - ->addOrderBy('sys_file.identifier') |
|
53 | - ->executeQuery() |
|
54 | - ->iterateAssociative() |
|
55 | - ; |
|
56 | - } |
|
24 | + return $queryBuilder |
|
25 | + ->select('sys_file.storage', 'sys_file.identifier', 'sys_file.sha1') |
|
26 | + ->from('sys_file') |
|
27 | + ->innerJoin( |
|
28 | + 'sys_file', |
|
29 | + 'sys_file_reference', |
|
30 | + 'sys_file_reference', |
|
31 | + $queryBuilder->expr()->eq( |
|
32 | + 'sys_file_reference.uid_local', |
|
33 | + $queryBuilder->quoteIdentifier('sys_file.uid') |
|
34 | + ) |
|
35 | + ) |
|
36 | + ->where( |
|
37 | + $queryBuilder->expr()->in( |
|
38 | + 'sys_file.storage', |
|
39 | + $queryBuilder->createNamedParameter($storageUids, \TYPO3\CMS\Core\Database\Connection::PARAM_INT_ARRAY) |
|
40 | + ), |
|
41 | + $queryBuilder->expr()->lt( |
|
42 | + 'sys_file.size', |
|
43 | + $queryBuilder->createNamedParameter((int) $GLOBALS['TSFE']->config['config']['svgstore.']['fileSize'] ?? null, \TYPO3\CMS\Core\Database\Connection::PARAM_INT) |
|
44 | + ), |
|
45 | + $queryBuilder->expr()->eq( |
|
46 | + 'sys_file.mime_type', |
|
47 | + $queryBuilder->createNamedParameter('image/svg+xml', \TYPO3\CMS\Core\Database\Connection::PARAM_STR) |
|
48 | + ) |
|
49 | + ) |
|
50 | + ->groupBy('sys_file.uid', 'sys_file.storage', 'sys_file.identifier', 'sys_file.sha1') |
|
51 | + ->orderBy('sys_file.storage') |
|
52 | + ->addOrderBy('sys_file.identifier') |
|
53 | + ->executeQuery() |
|
54 | + ->iterateAssociative() |
|
55 | + ; |
|
56 | + } |
|
57 | 57 | } |