Skip to content

Commit

Permalink
Merge pull request #127 from daniellienert/task/refactor-to-middlewar…
Browse files Browse the repository at this point in the history
…e-for-neos-7

!!! TASK: Refactor to middleware for Flow 7
  • Loading branch information
Sebobo authored Dec 22, 2020
2 parents 4e974ce + f6a568f commit 9052ab4
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace t3n\Neos\Debug\Http;
namespace t3n\Neos\Debug\Http\Middleware;

/**
* This file is part of the t3n.Neos.Debugger package.
Expand All @@ -15,14 +15,16 @@
*/

use Neos\Flow\Annotations as Flow;
use Neos\Flow\Http\Component\ComponentContext;
use Neos\Flow\Http\Component\ComponentInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use t3n\Neos\Debug\Service\DebugService;

/**
* Cache control header component
*/
class AddServerTimingHeaderComponent implements ComponentInterface
class AddServerTimingMiddleware implements MiddlewareInterface
{
/**
* @Flow\InjectConfiguration(path="serverTimingHeader.enabled")
Expand All @@ -38,17 +40,14 @@ class AddServerTimingHeaderComponent implements ComponentInterface
*/
protected $debugService;

/**
* @inheritDoc
*/
public function handle(ComponentContext $componentContext)
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$response = $handler->handle($request);

if (! $this->enabled) {
return;
return $response;
}

$response = $componentContext->getHttpResponse();

$serverTiming = '';
$metrics = $this->debugService->getMetrics();
foreach ($metrics as $key => ['value' => $value, 'description' => $description]) {
Expand All @@ -61,8 +60,6 @@ public function handle(ComponentContext $componentContext)
}
}

$modifiedResponse = $response->withAddedHeader('Server-Timing', $serverTiming);

$componentContext->replaceHttpResponse($modifiedResponse);
return $response->withAddedHeader('Server-Timing', $serverTiming);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace t3n\Neos\Debug\Http;
namespace t3n\Neos\Debug\Http\Middleware;

/**
* This file is part of the t3n.Neos.Debugger package.
Expand All @@ -15,11 +15,13 @@
*/

use Neos\Flow\Annotations as Flow;
use Neos\Flow\Http\Component\ComponentContext;
use Neos\Flow\Http\Component\ComponentInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use t3n\Neos\Debug\Service\DebugService;

class MeasureServerTimingComponent implements ComponentInterface
class MeasureServerTimingMiddleware implements MiddlewareInterface
{
/**
* @Flow\InjectConfiguration(path="serverTimingHeader.enabled")
Expand All @@ -35,15 +37,14 @@ class MeasureServerTimingComponent implements ComponentInterface
*/
protected $debugService;

/**
* @inheritDoc
*/
public function handle(ComponentContext $componentContext)
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
if (! $this->enabled) {
return;
$response = $handler->handle($request);

if ($this->enabled) {
$this->debugService->startRequestTimer();
}

$this->debugService->startRequestTimer();
return $response;
}
}
16 changes: 7 additions & 9 deletions Configuration/Settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@ Neos:

Flow:
http:
chain:
'process':
chain:
t3n.Neos.Debug:MeasureServerTiming:
position: 'start'
component: 't3n\Neos\Debug\Http\MeasureServerTimingComponent'
t3n.Neos.Debug:AddServerTimingHeader:
position: 'after setHeader'
component: 't3n\Neos\Debug\Http\AddServerTimingHeaderComponent'
middlewares:
t3n.Neos.Debug:MeasureServerTiming:
position: 'start'
middleware: 't3n\Neos\Debug\Http\Middleware\MeasureServerTimingMiddleware'
t3n.Neos.Debug:AddServerTimingHeader:
position: 'after dispatch'
middleware: 't3n\Neos\Debug\Http\Middleware\AddServerTimingMiddleware'

reflection:
ignoredTags:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"license": "MIT",
"type": "neos-package",
"require": {
"neos/neos": "~3.3.0 || ~4.0 || ~5.0"
"neos/neos": "^7.0"
},
"autoload": {
"psr-4": {
Expand Down

0 comments on commit 9052ab4

Please sign in to comment.