@@ -14,47 +14,47 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class RegExRepService implements \TYPO3\CMS\Core\SingletonInterface |
| 16 | 16 | { |
| 17 | - public function process(string $html): string |
|
| 18 | - { |
|
| 19 | - $config = $GLOBALS['TSFE']->config['config']['replacer.']; |
|
| 20 | - |
|
| 21 | - foreach (['search.', 'replace.'] as $section) { |
|
| 22 | - if (!isset($config[$section]) || !\is_array($config[$section])) { |
|
| 23 | - throw new \Exception('missing entry @ config.replacer.'.$section); |
|
| 24 | - } |
|
| 25 | - |
|
| 26 | - if (preg_match_all('/"([\w\-]+)\.";/', serialize(array_keys($config[$section])), $matches)) { |
|
| 27 | - $cObj = $cObj ?? $GLOBALS['TSFE']->cObj ?? GeneralUtility::makeInstance(ContentObjectRenderer::class); |
|
| 28 | - |
|
| 29 | - foreach ($matches[1] as $key) { |
|
| 30 | - $config[$section][$key] = $cObj |
|
| 31 | - ->stdWrap( |
|
| 32 | - $config[$section][$key], |
|
| 33 | - $config[$section][$key.'.'] |
|
| 34 | - ) |
|
| 35 | - ; |
|
| 36 | - unset($config[$section][$key.'.']); // keep! |
|
| 37 | - } |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - ksort($config[$section], \SORT_NATURAL); // safety |
|
| 41 | - } |
|
| 42 | - unset($cObj, $matches); // save MEM |
|
| 43 | - |
|
| 44 | - if (\TYPO3\CMS\Core\Core\Environment::getContext()->isDevelopment()) { |
|
| 45 | - foreach ($config['search.'] as $key => $val) { |
|
| 46 | - if (false === @preg_match($val, '')) { |
|
| 47 | - throw new \Exception(preg_last_error_msg().' : please check your regex syntax @ '."{$key} = {$val}"); |
|
| 48 | - } |
|
| 49 | - } |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - $arrIntersectKeysCnt = 2 * \count(array_intersect_key($config['search.'], $config['replace.'])); |
|
| 53 | - |
|
| 54 | - if ((bool) (\count($config['search.']) + \count($config['replace.']) - $arrIntersectKeysCnt)) { |
|
| 55 | - throw new \Exception('config.replacer requests have diverged'); |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - return preg_replace($config['search.'], $config['replace.'], $html); |
|
| 59 | - } |
|
| 17 | + public function process(string $html): string |
|
| 18 | + { |
|
| 19 | + $config = $GLOBALS['TSFE']->config['config']['replacer.']; |
|
| 20 | + |
|
| 21 | + foreach (['search.', 'replace.'] as $section) { |
|
| 22 | + if (!isset($config[$section]) || !\is_array($config[$section])) { |
|
| 23 | + throw new \Exception('missing entry @ config.replacer.'.$section); |
|
| 24 | + } |
|
| 25 | + |
|
| 26 | + if (preg_match_all('/"([\w\-]+)\.";/', serialize(array_keys($config[$section])), $matches)) { |
|
| 27 | + $cObj = $cObj ?? $GLOBALS['TSFE']->cObj ?? GeneralUtility::makeInstance(ContentObjectRenderer::class); |
|
| 28 | + |
|
| 29 | + foreach ($matches[1] as $key) { |
|
| 30 | + $config[$section][$key] = $cObj |
|
| 31 | + ->stdWrap( |
|
| 32 | + $config[$section][$key], |
|
| 33 | + $config[$section][$key.'.'] |
|
| 34 | + ) |
|
| 35 | + ; |
|
| 36 | + unset($config[$section][$key.'.']); // keep! |
|
| 37 | + } |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + ksort($config[$section], \SORT_NATURAL); // safety |
|
| 41 | + } |
|
| 42 | + unset($cObj, $matches); // save MEM |
|
| 43 | + |
|
| 44 | + if (\TYPO3\CMS\Core\Core\Environment::getContext()->isDevelopment()) { |
|
| 45 | + foreach ($config['search.'] as $key => $val) { |
|
| 46 | + if (false === @preg_match($val, '')) { |
|
| 47 | + throw new \Exception(preg_last_error_msg().' : please check your regex syntax @ '."{$key} = {$val}"); |
|
| 48 | + } |
|
| 49 | + } |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + $arrIntersectKeysCnt = 2 * \count(array_intersect_key($config['search.'], $config['replace.'])); |
|
| 53 | + |
|
| 54 | + if ((bool) (\count($config['search.']) + \count($config['replace.']) - $arrIntersectKeysCnt)) { |
|
| 55 | + throw new \Exception('config.replacer requests have diverged'); |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + return preg_replace($config['search.'], $config['replace.'], $html); |
|
| 59 | + } |
|
| 60 | 60 | } |
@@ -18,28 +18,28 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class RegExRepMiddleware implements MiddlewareInterface |
| 20 | 20 | { |
| 21 | - /** |
|
| 22 | - * RegEx search & replace @ HTML output. |
|
| 23 | - */ |
|
| 24 | - public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
| 25 | - { |
|
| 26 | - $response = $handler->handle($request); |
|
| 21 | + /** |
|
| 22 | + * RegEx search & replace @ 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 | - && ($GLOBALS['TSFE']->config['config']['replacer.'] ?? false) |
|
| 31 | - && 'text/html' == substr($response->getHeaderLine('Content-Type'), 0, 9) |
|
| 32 | - && !empty($response->getBody()) |
|
| 33 | - ) { |
|
| 34 | - $processedHtml = GeneralUtility::makeInstance(\HTML\Sourceopt\Service\RegExRepService::class) |
|
| 35 | - ->process((string) $response->getBody()) |
|
| 36 | - ; |
|
| 28 | + if (!($response instanceof NullResponse) |
|
| 29 | + && $GLOBALS['TSFE'] instanceof TypoScriptFrontendController |
|
| 30 | + && ($GLOBALS['TSFE']->config['config']['replacer.'] ?? false) |
|
| 31 | + && 'text/html' == substr($response->getHeaderLine('Content-Type'), 0, 9) |
|
| 32 | + && !empty($response->getBody()) |
|
| 33 | + ) { |
|
| 34 | + $processedHtml = GeneralUtility::makeInstance(\HTML\Sourceopt\Service\RegExRepService::class) |
|
| 35 | + ->process((string) $response->getBody()) |
|
| 36 | + ; |
|
| 37 | 37 | |
| 38 | - $responseBody = new Stream('php://temp', 'rw'); |
|
| 39 | - $responseBody->write($processedHtml); |
|
| 40 | - $response = $response->withBody($responseBody); |
|
| 41 | - } |
|
| 38 | + $responseBody = new Stream('php://temp', 'rw'); |
|
| 39 | + $responseBody->write($processedHtml); |
|
| 40 | + $response = $response->withBody($responseBody); |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - return $response; |
|
| 44 | - } |
|
| 43 | + return $response; |
|
| 44 | + } |
|
| 45 | 45 | } |