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