@@ -14,216 +14,216 @@ |
||
14 | 14 | */ |
15 | 15 | class SvgStoreService implements SingletonInterface |
16 | 16 | { |
17 | - public function __construct() |
|
18 | - { |
|
19 | - //$this->styl = []; # https://stackoverflow.com/questions/39583880/external-svg-fails-to-apply-internal-css |
|
20 | - //$this->defs = []; # https://bugs.chromium.org/p/chromium/issues/detail?id=751733#c14 |
|
21 | - $this->svgs = []; |
|
22 | - |
|
23 | - $this->outputDir = '/typo3temp/assets/svg/'; |
|
24 | - $this->sitePath = \TYPO3\CMS\Core\Core\Environment::getPublicPath(); // [^/]$ |
|
25 | - |
|
26 | - $this->connPool = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\ConnectionPool::class); |
|
27 | - $this->svgCache = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Cache\CacheManager::class)->getCache('svgstore'); |
|
28 | - } |
|
29 | - |
|
30 | - public function process(string $html): string |
|
31 | - { |
|
32 | - $this->spritePath = $this->svgCache->get('spritePath'); |
|
33 | - $this->svgFileArr = $this->svgCache->get('svgFileArr'); |
|
34 | - |
|
35 | - if (empty($this->spritePath) && !$this->populateCache()) { |
|
36 | - throw new \Exception('could not write file: '.$this->sitePath.$this->spritePath); |
|
37 | - } |
|
38 | - |
|
39 | - if (!file_exists($this->sitePath.$this->spritePath)) { |
|
40 | - throw new \Exception('file does not exists: '.$this->sitePath.$this->spritePath); |
|
41 | - } |
|
42 | - |
|
43 | - if (!preg_match('/(?<head>.+?<\/head>)(?<body>.+)/s', $html, $html) && 5 == \count($html)) { |
|
44 | - throw new \Exception('fix HTML!'); |
|
45 | - } |
|
46 | - |
|
47 | - // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#attributes |
|
48 | - $html['body'] = preg_replace_callback('/<img(?<pre>[^>]*)src="(?<src>\/[^"]+\.svg)"(?<post>[^>]*?)[\s\/]*>(?!\s*<\/picture>)/s', function (array $match): string { // ^[/] |
|
49 | - |
|
50 | - if (!isset($this->svgFileArr[$match['src']])) { // check usage |
|
51 | - return $match[0]; |
|
52 | - } |
|
53 | - $attr = preg_replace('/\s(?:alt|ismap|loading|title|sizes|srcset|usemap)="[^"]*"/', '', $match['pre'].$match['post']); // cleanup |
|
54 | - |
|
55 | - return sprintf('<svg %s %s><use href="%s#%s"/></svg>', $this->svgFileArr[$match['src']]['attr'], $attr, $this->spritePath, $this->convertFilePath($match['src'])); |
|
56 | - }, $html['body']); |
|
57 | - |
|
58 | - // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/object#attributes |
|
59 | - $html['body'] = preg_replace_callback('/<object(?<pre>[^>]*)data="(?<data>\/[^"]+\.svg)"(?<post>[^>]*?)[\s\/]*>(?:<\/object>)/s', function (array $match): string { // ^[/] |
|
60 | - |
|
61 | - if (!isset($this->svgFileArr[$match['data']])) { // check usage |
|
62 | - return $match[0]; |
|
63 | - } |
|
64 | - $attr = preg_replace('/\s(?:form|name|type|usemap)="[^"]*"/', '', $match['pre'].$match['post']); // cleanup |
|
65 | - |
|
66 | - return sprintf('<svg %s %s><use href="%s#%s"/></svg>', $this->svgFileArr[$match['src']]['attr'], $attr, $this->spritePath, $this->convertFilePath($match['data'])); |
|
67 | - }, $html['body']); |
|
68 | - |
|
69 | - return $html['head'].$html['body']; |
|
70 | - } |
|
71 | - |
|
72 | - private function convertFilePath(string $path): string |
|
73 | - { |
|
74 | - return preg_replace('/.svg$|[^\w\-]/', '', str_replace('/', '-', ltrim($path, '/'))); // ^[^/] |
|
75 | - } |
|
76 | - |
|
77 | - private function addFileToSpriteArr(string $hash, string $path): ?array |
|
78 | - { |
|
79 | - if (1 === preg_match('/;base64/', $svg = file_get_contents($this->sitePath.$path))) { // noop! |
|
80 | - return null; |
|
81 | - } |
|
82 | - |
|
83 | - if (1 === preg_match('/<(?:style|defs|use)|url\(/', $svg)) { |
|
84 | - return null; // check links @ __construct |
|
85 | - } |
|
86 | - |
|
87 | - //$svg = preg_replace('/((?:id|class)=")/', '$1'.$hash.'__', $svg); // extend IDs |
|
88 | - //$svg = preg_replace('/(href="|url\()#/', '$1#'.$hash.'__', $svg); // recover IDs |
|
89 | - |
|
90 | - //$svg = preg_replace_callback('/<style[^>]*>(?<styl>.+?)<\/style>|<defs[^>]*>(?<defs>.+?)<\/defs>/s', function(array $match) use($hash): string { |
|
91 | - // |
|
92 | - // if(isset($match['styl'])) |
|
93 | - // { |
|
94 | - // $this->styl[] = preg_replace('/\s*(\.|#){1}(.+?)\s*\{/', '$1'.$hash.'__$2{', $match['styl']); // patch CSS # https://mathiasbynens.be/notes/css-escapes |
|
95 | - // } |
|
96 | - // if(isset($match['defs'])) |
|
97 | - // { |
|
98 | - // $this->defs[] = trim($match['defs']); |
|
99 | - // } |
|
100 | - // return ''; |
|
101 | - //}, $svg); |
|
102 | - |
|
103 | - |
|
104 | - // https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/xlink:href |
|
105 | - $svg = preg_replace('/.*<svg|<\/svg>.*|xlink:|\s(?:(?:version|xmlns)|(?:[a-z\-]+\:[a-z\-]+))="[^"]*"/s', '', $svg); // cleanup |
|
106 | - |
|
107 | - // https://developer.mozilla.org/en-US/docs/Web/SVG/Element/svg#attributes |
|
108 | - $svg = preg_replace_callback('/([^>]+)\s*(?=>)/s', function (array $match) use(&$attr): string { |
|
109 | - |
|
110 | - if(false === preg_match_all('/\s(?<attr>[\w\-]+)=["\']\s*(?<value>[^"\']+)\s*["\']/', $match[1], $matches)) { |
|
111 | - return $match[0]; |
|
112 | - } |
|
113 | - foreach($matches['attr'] as $index => $attribute) |
|
114 | - { |
|
115 | - switch($attribute) |
|
116 | - { |
|
117 | - case 'id': |
|
118 | - case 'width': |
|
119 | - case 'height': |
|
120 | - unset($matches[0][$index]); |
|
121 | - break; |
|
122 | - |
|
123 | - case 'viewBox': |
|
124 | - $attr[] = sprintf('%s="%s"', $attribute, $matches['value'][$index]); // save! |
|
125 | - default: |
|
126 | - $matches[0][$index] = sprintf('%s="%s"', $attribute, $matches['value'][$index]); // cleanup |
|
127 | - } |
|
128 | - } |
|
129 | - return implode(' ', $matches[0]); |
|
130 | - }, $svg, 1); |
|
131 | - |
|
132 | - if ($attr) { // TODO; beautify |
|
133 | - $this->svgs[] = sprintf('id="%s" %s', $this->convertFilePath($path), $svg); // append ID |
|
134 | - } |
|
135 | - |
|
136 | - return !$attr ?: ['attr' => implode(' ', $attr), 'hash' => $hash]; |
|
137 | - } |
|
138 | - |
|
139 | - private function populateCache(): bool |
|
140 | - { |
|
141 | - $storageArr = $this->getStorageArrayFromDB(); |
|
142 | - $svgFileArr = $this->getSvgFilesArrayFromDB(array_keys($storageArr)); |
|
143 | - |
|
144 | - $this->svgFileArr = []; |
|
145 | - foreach ($svgFileArr as $index => $row) { |
|
146 | - if (!$this->svgFileArr[($row['path'] = '/'.$storageArr[$row['storage']].$row['identifier'])] = $this->addFileToSpriteArr($row['sha1'], $row['path'])) { // ^[/] |
|
147 | - unset($this->svgFileArr[$row['path']]); |
|
148 | - } |
|
149 | - } |
|
150 | - |
|
151 | - $svg = preg_replace_callback( |
|
152 | - '/<use(?<pre>.*?)(?:xlink:)?href="(?<href>\/.+?\.svg)#[^"]+"(?<post>.*?)[\s\/]*>(?:<\/use>)?/s', |
|
153 | - function (array $match): string { |
|
154 | - return sprintf('<use%s href="#%s"/>', $match['pre'].$match['post'], $this->convertFilePath($match['href'])); |
|
155 | - }, |
|
156 | - '<svg xmlns="http://www.w3.org/2000/svg">' |
|
157 | - //."\n<style>\n".implode("\n", $this->styl)."\n</style>" |
|
158 | - //."\n<defs>\n".implode("\n", $this->defs)."\n</defs>" |
|
159 | - ."\n<symbol ".implode("</symbol>\n<symbol ", $this->svgs)."</symbol>\n" |
|
160 | - .'</svg>' |
|
161 | - ); |
|
162 | - |
|
163 | - //unset($this->styl); // save MEM |
|
164 | - //unset($this->defs); // save MEM |
|
165 | - unset($this->svgs); // save MEM |
|
166 | - |
|
167 | - if (\is_int($var = $GLOBALS['TSFE']->config['config']['sourceopt.']['formatHtml']) && 1 == $var) { |
|
168 | - $svg = preg_replace('/[\n\r\t\v\0]|\s{2,}/', '', $svg); |
|
169 | - } |
|
170 | - |
|
171 | - $svg = preg_replace('/<([a-z]+)\s*(\/|>\s*<\/\1)>\s*/i', '', $svg); // remove emtpy |
|
172 | - $svg = preg_replace('/<((circle|ellipse|line|path|polygon|polyline|rect|stop|use)\s[^>]+?)\s*>\s*<\/\2>/', '<$1/>', $svg); // shorten/minify |
|
173 | - |
|
174 | - |
|
175 | - if (!is_dir($this->sitePath.$this->outputDir)) { |
|
176 | - GeneralUtility::mkdir_deep($this->sitePath.$this->outputDir); |
|
177 | - } |
|
178 | - |
|
179 | - $this->spritePath = $this->outputDir.hash('sha1', serialize($this->svgFileArr)).'.svg'; |
|
180 | - if (false === file_put_contents($this->sitePath.$this->spritePath, $svg)) { |
|
181 | - return false; |
|
182 | - } |
|
183 | - unset($svg); // save MEM |
|
184 | - |
|
185 | - $this->svgCache->set('svgFileArr', $this->svgFileArr); |
|
186 | - $this->svgCache->set('spritePath', $this->spritePath); |
|
187 | - |
|
188 | - return true; |
|
189 | - } |
|
190 | - |
|
191 | - private function getStorageArrayFromDB(): array |
|
192 | - { |
|
193 | - $storageResources = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\StorageRepository::class)->findAll(); |
|
194 | - foreach ($storageResources as $storage) { |
|
195 | - if ('relative' == $storage->getConfiguration()['pathType']) { |
|
196 | - $storageResources[$storage->getUid()] = rtrim($storage->getConfiguration()['basePath'], '/'); // [^/]$ |
|
197 | - } |
|
198 | - } |
|
199 | - unset($storageResources[0]); // keep! |
|
200 | - |
|
201 | - return $storageResources; |
|
202 | - } |
|
203 | - |
|
204 | - private function getSvgFilesArrayFromDB(array $storageIds): array |
|
205 | - { |
|
206 | - return ($queryBuilder = $this->connPool->getQueryBuilderForTable('sys_file')) |
|
207 | - ->select('sys_file.storage', 'sys_file.identifier', 'sys_file.sha1') |
|
208 | - ->from('sys_file') |
|
209 | - ->innerJoin( |
|
210 | - 'sys_file', |
|
211 | - 'sys_file_reference', |
|
212 | - 'sys_file_reference', |
|
213 | - $queryBuilder->expr()->eq( |
|
214 | - 'sys_file_reference.uid_local', |
|
215 | - $queryBuilder->quoteIdentifier('sys_file.uid') |
|
216 | - ) |
|
217 | - ) |
|
218 | - ->where( |
|
219 | - $queryBuilder->expr()->in('sys_file.storage', $queryBuilder->createNamedParameter($storageIds, \TYPO3\CMS\Core\Database\Connection::PARAM_INT_ARRAY)), |
|
220 | - $queryBuilder->expr()->eq('sys_file.mime_type', $queryBuilder->createNamedParameter('image/svg+xml')), |
|
221 | - $queryBuilder->expr()->lt('sys_file.size', $queryBuilder->createNamedParameter($GLOBALS['TSFE']->config['config']['svgstore.']['fileSize'])), |
|
222 | - ) |
|
223 | - ->groupBy('sys_file.uid') |
|
224 | - ->orderBy('sys_file.uid') |
|
225 | - ->execute() |
|
226 | - ->fetchAll() // TODO; use stdClass |
|
227 | - ; |
|
228 | - } |
|
17 | + public function __construct() |
|
18 | + { |
|
19 | + //$this->styl = []; # https://stackoverflow.com/questions/39583880/external-svg-fails-to-apply-internal-css |
|
20 | + //$this->defs = []; # https://bugs.chromium.org/p/chromium/issues/detail?id=751733#c14 |
|
21 | + $this->svgs = []; |
|
22 | + |
|
23 | + $this->outputDir = '/typo3temp/assets/svg/'; |
|
24 | + $this->sitePath = \TYPO3\CMS\Core\Core\Environment::getPublicPath(); // [^/]$ |
|
25 | + |
|
26 | + $this->connPool = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\ConnectionPool::class); |
|
27 | + $this->svgCache = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Cache\CacheManager::class)->getCache('svgstore'); |
|
28 | + } |
|
29 | + |
|
30 | + public function process(string $html): string |
|
31 | + { |
|
32 | + $this->spritePath = $this->svgCache->get('spritePath'); |
|
33 | + $this->svgFileArr = $this->svgCache->get('svgFileArr'); |
|
34 | + |
|
35 | + if (empty($this->spritePath) && !$this->populateCache()) { |
|
36 | + throw new \Exception('could not write file: '.$this->sitePath.$this->spritePath); |
|
37 | + } |
|
38 | + |
|
39 | + if (!file_exists($this->sitePath.$this->spritePath)) { |
|
40 | + throw new \Exception('file does not exists: '.$this->sitePath.$this->spritePath); |
|
41 | + } |
|
42 | + |
|
43 | + if (!preg_match('/(?<head>.+?<\/head>)(?<body>.+)/s', $html, $html) && 5 == \count($html)) { |
|
44 | + throw new \Exception('fix HTML!'); |
|
45 | + } |
|
46 | + |
|
47 | + // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#attributes |
|
48 | + $html['body'] = preg_replace_callback('/<img(?<pre>[^>]*)src="(?<src>\/[^"]+\.svg)"(?<post>[^>]*?)[\s\/]*>(?!\s*<\/picture>)/s', function (array $match): string { // ^[/] |
|
49 | + |
|
50 | + if (!isset($this->svgFileArr[$match['src']])) { // check usage |
|
51 | + return $match[0]; |
|
52 | + } |
|
53 | + $attr = preg_replace('/\s(?:alt|ismap|loading|title|sizes|srcset|usemap)="[^"]*"/', '', $match['pre'].$match['post']); // cleanup |
|
54 | + |
|
55 | + return sprintf('<svg %s %s><use href="%s#%s"/></svg>', $this->svgFileArr[$match['src']]['attr'], $attr, $this->spritePath, $this->convertFilePath($match['src'])); |
|
56 | + }, $html['body']); |
|
57 | + |
|
58 | + // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/object#attributes |
|
59 | + $html['body'] = preg_replace_callback('/<object(?<pre>[^>]*)data="(?<data>\/[^"]+\.svg)"(?<post>[^>]*?)[\s\/]*>(?:<\/object>)/s', function (array $match): string { // ^[/] |
|
60 | + |
|
61 | + if (!isset($this->svgFileArr[$match['data']])) { // check usage |
|
62 | + return $match[0]; |
|
63 | + } |
|
64 | + $attr = preg_replace('/\s(?:form|name|type|usemap)="[^"]*"/', '', $match['pre'].$match['post']); // cleanup |
|
65 | + |
|
66 | + return sprintf('<svg %s %s><use href="%s#%s"/></svg>', $this->svgFileArr[$match['src']]['attr'], $attr, $this->spritePath, $this->convertFilePath($match['data'])); |
|
67 | + }, $html['body']); |
|
68 | + |
|
69 | + return $html['head'].$html['body']; |
|
70 | + } |
|
71 | + |
|
72 | + private function convertFilePath(string $path): string |
|
73 | + { |
|
74 | + return preg_replace('/.svg$|[^\w\-]/', '', str_replace('/', '-', ltrim($path, '/'))); // ^[^/] |
|
75 | + } |
|
76 | + |
|
77 | + private function addFileToSpriteArr(string $hash, string $path): ?array |
|
78 | + { |
|
79 | + if (1 === preg_match('/;base64/', $svg = file_get_contents($this->sitePath.$path))) { // noop! |
|
80 | + return null; |
|
81 | + } |
|
82 | + |
|
83 | + if (1 === preg_match('/<(?:style|defs|use)|url\(/', $svg)) { |
|
84 | + return null; // check links @ __construct |
|
85 | + } |
|
86 | + |
|
87 | + //$svg = preg_replace('/((?:id|class)=")/', '$1'.$hash.'__', $svg); // extend IDs |
|
88 | + //$svg = preg_replace('/(href="|url\()#/', '$1#'.$hash.'__', $svg); // recover IDs |
|
89 | + |
|
90 | + //$svg = preg_replace_callback('/<style[^>]*>(?<styl>.+?)<\/style>|<defs[^>]*>(?<defs>.+?)<\/defs>/s', function(array $match) use($hash): string { |
|
91 | + // |
|
92 | + // if(isset($match['styl'])) |
|
93 | + // { |
|
94 | + // $this->styl[] = preg_replace('/\s*(\.|#){1}(.+?)\s*\{/', '$1'.$hash.'__$2{', $match['styl']); // patch CSS # https://mathiasbynens.be/notes/css-escapes |
|
95 | + // } |
|
96 | + // if(isset($match['defs'])) |
|
97 | + // { |
|
98 | + // $this->defs[] = trim($match['defs']); |
|
99 | + // } |
|
100 | + // return ''; |
|
101 | + //}, $svg); |
|
102 | + |
|
103 | + |
|
104 | + // https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/xlink:href |
|
105 | + $svg = preg_replace('/.*<svg|<\/svg>.*|xlink:|\s(?:(?:version|xmlns)|(?:[a-z\-]+\:[a-z\-]+))="[^"]*"/s', '', $svg); // cleanup |
|
106 | + |
|
107 | + // https://developer.mozilla.org/en-US/docs/Web/SVG/Element/svg#attributes |
|
108 | + $svg = preg_replace_callback('/([^>]+)\s*(?=>)/s', function (array $match) use(&$attr): string { |
|
109 | + |
|
110 | + if(false === preg_match_all('/\s(?<attr>[\w\-]+)=["\']\s*(?<value>[^"\']+)\s*["\']/', $match[1], $matches)) { |
|
111 | + return $match[0]; |
|
112 | + } |
|
113 | + foreach($matches['attr'] as $index => $attribute) |
|
114 | + { |
|
115 | + switch($attribute) |
|
116 | + { |
|
117 | + case 'id': |
|
118 | + case 'width': |
|
119 | + case 'height': |
|
120 | + unset($matches[0][$index]); |
|
121 | + break; |
|
122 | + |
|
123 | + case 'viewBox': |
|
124 | + $attr[] = sprintf('%s="%s"', $attribute, $matches['value'][$index]); // save! |
|
125 | + default: |
|
126 | + $matches[0][$index] = sprintf('%s="%s"', $attribute, $matches['value'][$index]); // cleanup |
|
127 | + } |
|
128 | + } |
|
129 | + return implode(' ', $matches[0]); |
|
130 | + }, $svg, 1); |
|
131 | + |
|
132 | + if ($attr) { // TODO; beautify |
|
133 | + $this->svgs[] = sprintf('id="%s" %s', $this->convertFilePath($path), $svg); // append ID |
|
134 | + } |
|
135 | + |
|
136 | + return !$attr ?: ['attr' => implode(' ', $attr), 'hash' => $hash]; |
|
137 | + } |
|
138 | + |
|
139 | + private function populateCache(): bool |
|
140 | + { |
|
141 | + $storageArr = $this->getStorageArrayFromDB(); |
|
142 | + $svgFileArr = $this->getSvgFilesArrayFromDB(array_keys($storageArr)); |
|
143 | + |
|
144 | + $this->svgFileArr = []; |
|
145 | + foreach ($svgFileArr as $index => $row) { |
|
146 | + if (!$this->svgFileArr[($row['path'] = '/'.$storageArr[$row['storage']].$row['identifier'])] = $this->addFileToSpriteArr($row['sha1'], $row['path'])) { // ^[/] |
|
147 | + unset($this->svgFileArr[$row['path']]); |
|
148 | + } |
|
149 | + } |
|
150 | + |
|
151 | + $svg = preg_replace_callback( |
|
152 | + '/<use(?<pre>.*?)(?:xlink:)?href="(?<href>\/.+?\.svg)#[^"]+"(?<post>.*?)[\s\/]*>(?:<\/use>)?/s', |
|
153 | + function (array $match): string { |
|
154 | + return sprintf('<use%s href="#%s"/>', $match['pre'].$match['post'], $this->convertFilePath($match['href'])); |
|
155 | + }, |
|
156 | + '<svg xmlns="http://www.w3.org/2000/svg">' |
|
157 | + //."\n<style>\n".implode("\n", $this->styl)."\n</style>" |
|
158 | + //."\n<defs>\n".implode("\n", $this->defs)."\n</defs>" |
|
159 | + ."\n<symbol ".implode("</symbol>\n<symbol ", $this->svgs)."</symbol>\n" |
|
160 | + .'</svg>' |
|
161 | + ); |
|
162 | + |
|
163 | + //unset($this->styl); // save MEM |
|
164 | + //unset($this->defs); // save MEM |
|
165 | + unset($this->svgs); // save MEM |
|
166 | + |
|
167 | + if (\is_int($var = $GLOBALS['TSFE']->config['config']['sourceopt.']['formatHtml']) && 1 == $var) { |
|
168 | + $svg = preg_replace('/[\n\r\t\v\0]|\s{2,}/', '', $svg); |
|
169 | + } |
|
170 | + |
|
171 | + $svg = preg_replace('/<([a-z]+)\s*(\/|>\s*<\/\1)>\s*/i', '', $svg); // remove emtpy |
|
172 | + $svg = preg_replace('/<((circle|ellipse|line|path|polygon|polyline|rect|stop|use)\s[^>]+?)\s*>\s*<\/\2>/', '<$1/>', $svg); // shorten/minify |
|
173 | + |
|
174 | + |
|
175 | + if (!is_dir($this->sitePath.$this->outputDir)) { |
|
176 | + GeneralUtility::mkdir_deep($this->sitePath.$this->outputDir); |
|
177 | + } |
|
178 | + |
|
179 | + $this->spritePath = $this->outputDir.hash('sha1', serialize($this->svgFileArr)).'.svg'; |
|
180 | + if (false === file_put_contents($this->sitePath.$this->spritePath, $svg)) { |
|
181 | + return false; |
|
182 | + } |
|
183 | + unset($svg); // save MEM |
|
184 | + |
|
185 | + $this->svgCache->set('svgFileArr', $this->svgFileArr); |
|
186 | + $this->svgCache->set('spritePath', $this->spritePath); |
|
187 | + |
|
188 | + return true; |
|
189 | + } |
|
190 | + |
|
191 | + private function getStorageArrayFromDB(): array |
|
192 | + { |
|
193 | + $storageResources = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\StorageRepository::class)->findAll(); |
|
194 | + foreach ($storageResources as $storage) { |
|
195 | + if ('relative' == $storage->getConfiguration()['pathType']) { |
|
196 | + $storageResources[$storage->getUid()] = rtrim($storage->getConfiguration()['basePath'], '/'); // [^/]$ |
|
197 | + } |
|
198 | + } |
|
199 | + unset($storageResources[0]); // keep! |
|
200 | + |
|
201 | + return $storageResources; |
|
202 | + } |
|
203 | + |
|
204 | + private function getSvgFilesArrayFromDB(array $storageIds): array |
|
205 | + { |
|
206 | + return ($queryBuilder = $this->connPool->getQueryBuilderForTable('sys_file')) |
|
207 | + ->select('sys_file.storage', 'sys_file.identifier', 'sys_file.sha1') |
|
208 | + ->from('sys_file') |
|
209 | + ->innerJoin( |
|
210 | + 'sys_file', |
|
211 | + 'sys_file_reference', |
|
212 | + 'sys_file_reference', |
|
213 | + $queryBuilder->expr()->eq( |
|
214 | + 'sys_file_reference.uid_local', |
|
215 | + $queryBuilder->quoteIdentifier('sys_file.uid') |
|
216 | + ) |
|
217 | + ) |
|
218 | + ->where( |
|
219 | + $queryBuilder->expr()->in('sys_file.storage', $queryBuilder->createNamedParameter($storageIds, \TYPO3\CMS\Core\Database\Connection::PARAM_INT_ARRAY)), |
|
220 | + $queryBuilder->expr()->eq('sys_file.mime_type', $queryBuilder->createNamedParameter('image/svg+xml')), |
|
221 | + $queryBuilder->expr()->lt('sys_file.size', $queryBuilder->createNamedParameter($GLOBALS['TSFE']->config['config']['svgstore.']['fileSize'])), |
|
222 | + ) |
|
223 | + ->groupBy('sys_file.uid') |
|
224 | + ->orderBy('sys_file.uid') |
|
225 | + ->execute() |
|
226 | + ->fetchAll() // TODO; use stdClass |
|
227 | + ; |
|
228 | + } |
|
229 | 229 | } |
@@ -45,7 +45,7 @@ discard block |
||
45 | 45 | } |
46 | 46 | |
47 | 47 | // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#attributes |
48 | - $html['body'] = preg_replace_callback('/<img(?<pre>[^>]*)src="(?<src>\/[^"]+\.svg)"(?<post>[^>]*?)[\s\/]*>(?!\s*<\/picture>)/s', function (array $match): string { // ^[/] |
|
48 | + $html['body'] = preg_replace_callback('/<img(?<pre>[^>]*)src="(?<src>\/[^"]+\.svg)"(?<post>[^>]*?)[\s\/]*>(?!\s*<\/picture>)/s', function(array $match): string { // ^[/] |
|
49 | 49 | |
50 | 50 | if (!isset($this->svgFileArr[$match['src']])) { // check usage |
51 | 51 | return $match[0]; |
@@ -56,7 +56,7 @@ discard block |
||
56 | 56 | }, $html['body']); |
57 | 57 | |
58 | 58 | // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/object#attributes |
59 | - $html['body'] = preg_replace_callback('/<object(?<pre>[^>]*)data="(?<data>\/[^"]+\.svg)"(?<post>[^>]*?)[\s\/]*>(?:<\/object>)/s', function (array $match): string { // ^[/] |
|
59 | + $html['body'] = preg_replace_callback('/<object(?<pre>[^>]*)data="(?<data>\/[^"]+\.svg)"(?<post>[^>]*?)[\s\/]*>(?:<\/object>)/s', function(array $match): string { // ^[/] |
|
60 | 60 | |
61 | 61 | if (!isset($this->svgFileArr[$match['data']])) { // check usage |
62 | 62 | return $match[0]; |
@@ -105,14 +105,14 @@ discard block |
||
105 | 105 | $svg = preg_replace('/.*<svg|<\/svg>.*|xlink:|\s(?:(?:version|xmlns)|(?:[a-z\-]+\:[a-z\-]+))="[^"]*"/s', '', $svg); // cleanup |
106 | 106 | |
107 | 107 | // https://developer.mozilla.org/en-US/docs/Web/SVG/Element/svg#attributes |
108 | - $svg = preg_replace_callback('/([^>]+)\s*(?=>)/s', function (array $match) use(&$attr): string { |
|
108 | + $svg = preg_replace_callback('/([^>]+)\s*(?=>)/s', function(array $match) use(&$attr): string { |
|
109 | 109 | |
110 | - if(false === preg_match_all('/\s(?<attr>[\w\-]+)=["\']\s*(?<value>[^"\']+)\s*["\']/', $match[1], $matches)) { |
|
110 | + if (false === preg_match_all('/\s(?<attr>[\w\-]+)=["\']\s*(?<value>[^"\']+)\s*["\']/', $match[1], $matches)) { |
|
111 | 111 | return $match[0]; |
112 | 112 | } |
113 | - foreach($matches['attr'] as $index => $attribute) |
|
113 | + foreach ($matches['attr'] as $index => $attribute) |
|
114 | 114 | { |
115 | - switch($attribute) |
|
115 | + switch ($attribute) |
|
116 | 116 | { |
117 | 117 | case 'id': |
118 | 118 | case 'width': |
@@ -150,7 +150,7 @@ discard block |
||
150 | 150 | |
151 | 151 | $svg = preg_replace_callback( |
152 | 152 | '/<use(?<pre>.*?)(?:xlink:)?href="(?<href>\/.+?\.svg)#[^"]+"(?<post>.*?)[\s\/]*>(?:<\/use>)?/s', |
153 | - function (array $match): string { |
|
153 | + function(array $match): string { |
|
154 | 154 | return sprintf('<use%s href="#%s"/>', $match['pre'].$match['post'], $this->convertFilePath($match['href'])); |
155 | 155 | }, |
156 | 156 | '<svg xmlns="http://www.w3.org/2000/svg">' |
@@ -18,27 +18,27 @@ |
||
18 | 18 | */ |
19 | 19 | class SvgStoreMiddleware implements MiddlewareInterface |
20 | 20 | { |
21 | - /** |
|
22 | - * Search/Extract/Merge SVGs @ HTML output. |
|
23 | - */ |
|
24 | - public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
25 | - { |
|
26 | - $response = $handler->handle($request); |
|
21 | + /** |
|
22 | + * Search/Extract/Merge SVGs @ HTML output. |
|
23 | + */ |
|
24 | + public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
25 | + { |
|
26 | + $response = $handler->handle($request); |
|
27 | 27 | |
28 | - if (!($response instanceof NullResponse) |
|
29 | - && $GLOBALS['TSFE'] instanceof TypoScriptFrontendController |
|
30 | - && false !== (bool) $GLOBALS['TSFE']->config['config']['svgstore.']['enabled'] |
|
31 | - && 'text/html' == substr($response->getHeaderLine('Content-Type'),0,9) |
|
32 | - ) { |
|
33 | - $processedHtml = GeneralUtility::makeInstance(\HTML\Sourceopt\Service\SvgStoreService::class) |
|
34 | - ->process($response->getBody()->__toString()) |
|
35 | - ; |
|
28 | + if (!($response instanceof NullResponse) |
|
29 | + && $GLOBALS['TSFE'] instanceof TypoScriptFrontendController |
|
30 | + && false !== (bool) $GLOBALS['TSFE']->config['config']['svgstore.']['enabled'] |
|
31 | + && 'text/html' == substr($response->getHeaderLine('Content-Type'),0,9) |
|
32 | + ) { |
|
33 | + $processedHtml = GeneralUtility::makeInstance(\HTML\Sourceopt\Service\SvgStoreService::class) |
|
34 | + ->process($response->getBody()->__toString()) |
|
35 | + ; |
|
36 | 36 | |
37 | - $responseBody = new Stream('php://temp', 'rw'); |
|
38 | - $responseBody->write($processedHtml); |
|
39 | - $response = $response->withBody($responseBody); |
|
40 | - } |
|
37 | + $responseBody = new Stream('php://temp', 'rw'); |
|
38 | + $responseBody->write($processedHtml); |
|
39 | + $response = $response->withBody($responseBody); |
|
40 | + } |
|
41 | 41 | |
42 | - return $response; |
|
43 | - } |
|
42 | + return $response; |
|
43 | + } |
|
44 | 44 | } |
@@ -28,7 +28,7 @@ |
||
28 | 28 | if (!($response instanceof NullResponse) |
29 | 29 | && $GLOBALS['TSFE'] instanceof TypoScriptFrontendController |
30 | 30 | && false !== (bool) $GLOBALS['TSFE']->config['config']['svgstore.']['enabled'] |
31 | - && 'text/html' == substr($response->getHeaderLine('Content-Type'),0,9) |
|
31 | + && 'text/html' == substr($response->getHeaderLine('Content-Type'), 0, 9) |
|
32 | 32 | ) { |
33 | 33 | $processedHtml = GeneralUtility::makeInstance(\HTML\Sourceopt\Service\SvgStoreService::class) |
34 | 34 | ->process($response->getBody()->__toString()) |
@@ -19,39 +19,39 @@ |
||
19 | 19 | */ |
20 | 20 | class CleanHtmlMiddleware implements MiddlewareInterface |
21 | 21 | { |
22 | - /** |
|
23 | - * @var CleanHtmlService |
|
24 | - */ |
|
25 | - protected $cleanHtmlService; |
|
26 | - |
|
27 | - public function __construct() |
|
28 | - { |
|
29 | - $this->cleanHtmlService = GeneralUtility::makeInstance(CleanHtmlService::class); |
|
30 | - } |
|
31 | - |
|
32 | - /** |
|
33 | - * Clean the HTML output. |
|
34 | - */ |
|
35 | - public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
36 | - { |
|
37 | - $response = $handler->handle($request); |
|
38 | - |
|
39 | - if (!($response instanceof NullResponse) |
|
40 | - && $GLOBALS['TSFE'] instanceof TypoScriptFrontendController |
|
41 | - && false !== (bool) $GLOBALS['TSFE']->config['config']['sourceopt.']['enabled'] |
|
42 | - && 'text/html' == substr($response->getHeaderLine('Content-Type'),0,9) |
|
43 | - ) { |
|
44 | - $processedHtml = $this->cleanHtmlService->clean( |
|
45 | - $response->getBody()->__toString(), |
|
46 | - $GLOBALS['TSFE']->config['config']['sourceopt.'] |
|
47 | - ); |
|
48 | - |
|
49 | - // Replace old body with $processedHtml |
|
50 | - $responseBody = new Stream('php://temp', 'rw'); |
|
51 | - $responseBody->write($processedHtml); |
|
52 | - $response = $response->withBody($responseBody); |
|
53 | - } |
|
54 | - |
|
55 | - return $response; |
|
56 | - } |
|
22 | + /** |
|
23 | + * @var CleanHtmlService |
|
24 | + */ |
|
25 | + protected $cleanHtmlService; |
|
26 | + |
|
27 | + public function __construct() |
|
28 | + { |
|
29 | + $this->cleanHtmlService = GeneralUtility::makeInstance(CleanHtmlService::class); |
|
30 | + } |
|
31 | + |
|
32 | + /** |
|
33 | + * Clean the HTML output. |
|
34 | + */ |
|
35 | + public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
36 | + { |
|
37 | + $response = $handler->handle($request); |
|
38 | + |
|
39 | + if (!($response instanceof NullResponse) |
|
40 | + && $GLOBALS['TSFE'] instanceof TypoScriptFrontendController |
|
41 | + && false !== (bool) $GLOBALS['TSFE']->config['config']['sourceopt.']['enabled'] |
|
42 | + && 'text/html' == substr($response->getHeaderLine('Content-Type'),0,9) |
|
43 | + ) { |
|
44 | + $processedHtml = $this->cleanHtmlService->clean( |
|
45 | + $response->getBody()->__toString(), |
|
46 | + $GLOBALS['TSFE']->config['config']['sourceopt.'] |
|
47 | + ); |
|
48 | + |
|
49 | + // Replace old body with $processedHtml |
|
50 | + $responseBody = new Stream('php://temp', 'rw'); |
|
51 | + $responseBody->write($processedHtml); |
|
52 | + $response = $response->withBody($responseBody); |
|
53 | + } |
|
54 | + |
|
55 | + return $response; |
|
56 | + } |
|
57 | 57 | } |
@@ -39,7 +39,7 @@ |
||
39 | 39 | if (!($response instanceof NullResponse) |
40 | 40 | && $GLOBALS['TSFE'] instanceof TypoScriptFrontendController |
41 | 41 | && false !== (bool) $GLOBALS['TSFE']->config['config']['sourceopt.']['enabled'] |
42 | - && 'text/html' == substr($response->getHeaderLine('Content-Type'),0,9) |
|
42 | + && 'text/html' == substr($response->getHeaderLine('Content-Type'), 0, 9) |
|
43 | 43 | ) { |
44 | 44 | $processedHtml = $this->cleanHtmlService->clean( |
45 | 45 | $response->getBody()->__toString(), |