Skip to content

Commit

Permalink
Streamline code
Browse files Browse the repository at this point in the history
  • Loading branch information
lochmueller committed Oct 23, 2024
1 parent 5cc52f3 commit d03d2a6
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 70 deletions.
44 changes: 44 additions & 0 deletions Classes/Middleware/AbstractMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace HTML\Sourceopt\Middleware;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Server\MiddlewareInterface;
use TYPO3\CMS\Core\Http\NullResponse;
use TYPO3\CMS\Core\Http\Stream;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

abstract class AbstractMiddleware implements MiddlewareInterface
{
protected function responseIsAlterable(ResponseInterface $response): bool
{
if (!$response instanceof NullResponse) {
return false;
}

if (!$GLOBALS['TSFE'] instanceof TypoScriptFrontendController) { // need for configuration
return false;
}

if ('text/html' !== substr($response->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;
}
}
35 changes: 12 additions & 23 deletions Classes/Middleware/CleanHtmlMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,30 @@

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.
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$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;
Expand Down
28 changes: 9 additions & 19 deletions Classes/Middleware/RegExRepMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,27 @@

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.
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$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;
Expand Down
28 changes: 9 additions & 19 deletions Classes/Middleware/SvgStoreMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,27 @@

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.
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$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;
Expand Down
8 changes: 8 additions & 0 deletions Configuration/Services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
services:
_defaults:
autowire: true
autoconfigure: true
public: false

HTML\Sourceopt\:
resource: '../Classes/*'
9 changes: 9 additions & 0 deletions Tests/Unit/AbstractUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace HTML\Sourceopt\Tests\Unit;

abstract class AbstractUnitTest extends \PHPUnit\Framework\TestCase
{
}
59 changes: 59 additions & 0 deletions Tests/Unit/Manipulation/RemoveCommentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace HTML\Sourceopt\Tests\Unit\Service;

use HTML\Sourceopt\Manipulation\RemoveComments;
use HTML\Sourceopt\Tests\Unit\AbstractUnitTest;

/**
* @internal
*
* @coversNothing
*/
class RemoveCommentTest extends AbstractUnitTest
{
/**
* @dataProvider generatorProvider
*/
public function testRemoveComment($before, $after): void
{
$cleanService = new RemoveComments();
$result = $cleanService->manipulate($before);

$this->assertEquals($after, $result);
}

public static function generatorProvider(): array
{
return [
[
'<head>
<meta name="Regisseur" content="Peter Jackson">
<meta name="generator" content="Tester">
<!-- Ich bin ein Test -->
</head>',
'<head>
<meta name="Regisseur" content="Peter Jackson">
<meta name="generator" content="Tester">
</head>',
],
[
'<head>
<!-- Ich bin ein Test -->
<meta name="Regisseur" content="Peter Jackson">
<meta name="generator" content="Tester">
<!-- Ich bin ein Test -->
</head>',
'<head>
<meta name="Regisseur" content="Peter Jackson">
<meta name="generator" content="Tester">
</head>',
],
];
}
}
10 changes: 2 additions & 8 deletions Tests/Unit/Manipulation/RemoveGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
<?php

declare(strict_types=1);
/**
* RemoveGeneratorTest.
*
* @author Tim Lochmüller
*/

namespace HTML\Sourceopt\Tests\Unit\Service;

use HTML\Sourceopt\Manipulation\RemoveGenerator;
use HTML\Sourceopt\Tests\Unit\AbstractUnitTest;

/**
* RemoveGeneratorTest.
*
* @internal
*
* @coversNothing
*/
class RemoveGeneratorTest extends \PHPUnit\Framework\TestCase
class RemoveGeneratorTest extends AbstractUnitTest
{
/**
* @dataProvider generatorProvider
Expand Down
4 changes: 3 additions & 1 deletion Tests/Unit/Service/CleanHtmlServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

namespace HTML\Sourceopt\Service;

use HTML\Sourceopt\Tests\Unit\AbstractUnitTest;

/**
* @internal
*
* @coversNothing
*/
class CleanHtmlServiceTest extends \PHPUnit\Framework\TestCase
class CleanHtmlServiceTest extends AbstractUnitTest
{
public function testFormatHtml(): void
{
Expand Down

0 comments on commit d03d2a6

Please sign in to comment.