diff --git a/Classes/Middleware/AbstractMiddleware.php b/Classes/Middleware/AbstractMiddleware.php new file mode 100644 index 0000000..95aa6ef --- /dev/null +++ b/Classes/Middleware/AbstractMiddleware.php @@ -0,0 +1,44 @@ +getHeaderLine('Content-Type'), 0, 9)) { + return false; + } + + if (empty($response->getBody())) { + return false; + } + + return true; + } + + protected function getStringStream(string $content): StreamInterface + { + $body = new Stream('php://temp', 'rw'); + $body->write($content); + + return $body; + } +} diff --git a/Classes/Middleware/CleanHtmlMiddleware.php b/Classes/Middleware/CleanHtmlMiddleware.php index e1be761..35f0105 100644 --- a/Classes/Middleware/CleanHtmlMiddleware.php +++ b/Classes/Middleware/CleanHtmlMiddleware.php @@ -4,17 +4,17 @@ namespace HTML\Sourceopt\Middleware; +use HTML\Sourceopt\Service\CleanHtmlService; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; -use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; -use TYPO3\CMS\Core\Http\NullResponse; -use TYPO3\CMS\Core\Http\Stream; -use TYPO3\CMS\Core\Utility\GeneralUtility; -use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController; -class CleanHtmlMiddleware implements MiddlewareInterface +class CleanHtmlMiddleware extends AbstractMiddleware { + public function __construct(protected CleanHtmlService $cleanHtmlService) + { + } + /** * Clean the HTML output. */ @@ -22,23 +22,12 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface { $response = $handler->handle($request); - if (!($response instanceof NullResponse) - && $GLOBALS['TSFE'] instanceof TypoScriptFrontendController - && ($GLOBALS['TSFE']->config['config']['sourceopt.']['enabled'] ?? false) - && 'text/html' == substr($response->getHeaderLine('Content-Type'), 0, 9) - && !empty($response->getBody()) - ) { - $processedHtml = GeneralUtility::makeInstance(\HTML\Sourceopt\Service\CleanHtmlService::class) - ->clean( - (string) $response->getBody(), - (array) $GLOBALS['TSFE']->config['config']['sourceopt.'] - ) - ; - - // Replace old body with $processedHtml - $responseBody = new Stream('php://temp', 'rw'); - $responseBody->write($processedHtml); - $response = $response->withBody($responseBody); + if ($this->responseIsAlterable($response) && $GLOBALS['TSFE']->config['config']['sourceopt.']['enabled'] ?? false) { + $processedHtml = $this->cleanHtmlService->clean( + (string) $response->getBody(), + (array) $GLOBALS['TSFE']->config['config']['sourceopt.'] + ); + $response = $response->withBody($this->getStringStream($processedHtml)); } return $response; diff --git a/Classes/Middleware/RegExRepMiddleware.php b/Classes/Middleware/RegExRepMiddleware.php index 5df8c13..45cab86 100644 --- a/Classes/Middleware/RegExRepMiddleware.php +++ b/Classes/Middleware/RegExRepMiddleware.php @@ -4,17 +4,17 @@ namespace HTML\Sourceopt\Middleware; +use HTML\Sourceopt\Service\RegExRepService; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; -use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; -use TYPO3\CMS\Core\Http\NullResponse; -use TYPO3\CMS\Core\Http\Stream; -use TYPO3\CMS\Core\Utility\GeneralUtility; -use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController; -class RegExRepMiddleware implements MiddlewareInterface +class RegExRepMiddleware extends AbstractMiddleware { + public function __construct(protected RegExRepService $regExRepService) + { + } + /** * RegEx search & replace @ HTML output. */ @@ -22,19 +22,9 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface { $response = $handler->handle($request); - if (!($response instanceof NullResponse) - && $GLOBALS['TSFE'] instanceof TypoScriptFrontendController - && ($GLOBALS['TSFE']->config['config']['replacer.'] ?? false) - && 'text/html' == substr($response->getHeaderLine('Content-Type'), 0, 9) - && !empty($response->getBody()) - ) { - $processedHtml = GeneralUtility::makeInstance(\HTML\Sourceopt\Service\RegExRepService::class) - ->process((string) $response->getBody()) - ; - - $responseBody = new Stream('php://temp', 'rw'); - $responseBody->write($processedHtml); - $response = $response->withBody($responseBody); + if ($this->responseIsAlterable($response) && $GLOBALS['TSFE']->config['config']['replacer.'] ?? false) { + $processedHtml = $this->regExRepService->process((string) $response->getBody()); + $response = $response->withBody($this->getStringStream($processedHtml)); } return $response; diff --git a/Classes/Middleware/SvgStoreMiddleware.php b/Classes/Middleware/SvgStoreMiddleware.php index f4d5bf5..8a1395c 100644 --- a/Classes/Middleware/SvgStoreMiddleware.php +++ b/Classes/Middleware/SvgStoreMiddleware.php @@ -4,17 +4,17 @@ namespace HTML\Sourceopt\Middleware; +use HTML\Sourceopt\Service\SvgStoreService; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; -use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; -use TYPO3\CMS\Core\Http\NullResponse; -use TYPO3\CMS\Core\Http\Stream; -use TYPO3\CMS\Core\Utility\GeneralUtility; -use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController; -class SvgStoreMiddleware implements MiddlewareInterface +class SvgStoreMiddleware extends AbstractMiddleware { + public function __construct(protected SvgStoreService $svgStoreService) + { + } + /** * Search/Extract/Merge SVGs @ HTML output. */ @@ -22,19 +22,9 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface { $response = $handler->handle($request); - if (!($response instanceof NullResponse) - && $GLOBALS['TSFE'] instanceof TypoScriptFrontendController - && ($GLOBALS['TSFE']->config['config']['svgstore.']['enabled'] ?? false) - && 'text/html' == substr($response->getHeaderLine('Content-Type'), 0, 9) - && !empty($response->getBody()) - ) { - $processedHtml = GeneralUtility::makeInstance(\HTML\Sourceopt\Service\SvgStoreService::class) - ->process((string) $response->getBody()) - ; - - $responseBody = new Stream('php://temp', 'rw'); - $responseBody->write($processedHtml); - $response = $response->withBody($responseBody); + if ($this->responseIsAlterable($response) && $GLOBALS['TSFE']->config['config']['svgstore.']['enabled'] ?? false) { + $processedHtml = $this->svgStoreService->process((string) $response->getBody()); + $response = $response->withBody($this->getStringStream($processedHtml)); } return $response; diff --git a/Configuration/Services.yaml b/Configuration/Services.yaml new file mode 100644 index 0000000..88ef30e --- /dev/null +++ b/Configuration/Services.yaml @@ -0,0 +1,8 @@ +services: + _defaults: + autowire: true + autoconfigure: true + public: false + + HTML\Sourceopt\: + resource: '../Classes/*' diff --git a/Tests/Unit/AbstractUnitTest.php b/Tests/Unit/AbstractUnitTest.php new file mode 100644 index 0000000..d584b06 --- /dev/null +++ b/Tests/Unit/AbstractUnitTest.php @@ -0,0 +1,9 @@ +manipulate($before); + + $this->assertEquals($after, $result); + } + + public static function generatorProvider(): array + { + return [ + [ + '
+ + + +', + ' + + + +', + ], + [ + ' + + + + +', + ' + + + + +', + ], + ]; + } +} diff --git a/Tests/Unit/Manipulation/RemoveGeneratorTest.php b/Tests/Unit/Manipulation/RemoveGeneratorTest.php index ac53d55..d2d2848 100644 --- a/Tests/Unit/Manipulation/RemoveGeneratorTest.php +++ b/Tests/Unit/Manipulation/RemoveGeneratorTest.php @@ -1,24 +1,18 @@