Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 66 additions & 52 deletions src/Dispatch/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class Dispatcher {
private HeaderManager $headerManager;
private Closure $viewInitCb;
private bool $redirectPrepared = false;
private bool $interruptResponseFlow = false;
/** @var array<int, string> */
private array $logicExecutionOrder = [];

Expand Down Expand Up @@ -342,6 +343,9 @@ private function setupResponse():Response {
$response = new Response(null, null, $this->request);
$response->setExitCallback(function() {
($this->finishCallback)($this->response);
if($this->interruptResponseFlow && $this->isRedirectPrepared()) {
throw new ResponseInterrupt();
}
});
return $response;
}
Expand Down Expand Up @@ -409,78 +413,88 @@ public function processResponse(
?Throwable $errorThrowable = null,
):void {
$this->logicExecutionOrder = [];
$dynamicPath = $this->serviceContainer->get(DynamicPath::class);
$this->interruptResponseFlow = true;

$this->viewModelProcessor?->processDynamicPath(
$this->viewModel,
$dynamicPath,
);
try {
$dynamicPath = $this->serviceContainer->get(DynamicPath::class);

$componentList = $this->viewModelProcessor?->processPartialContent(
$this->viewModel,
$this->viewAssembly,
);
$this->viewModelProcessor?->processDynamicPath(
$this->viewModel,
$dynamicPath,
);

$this->verifyCsrfRequest(
$this->request->getMethod(),
$this->input->getAll(Input::DATA_BODY),
);
($this->viewInitCb)();
if($errorThrowable) {
$this->bindErrorDetails($errorThrowable);
}
$componentList = $this->viewModelProcessor?->processPartialContent(
$this->viewModel,
$this->viewAssembly,
);

$this->verifyCsrfRequest(
$this->request->getMethod(),
$this->input->getAll(Input::DATA_BODY),
);
($this->viewInitCb)();
if($errorThrowable) {
$this->bindErrorDetails($errorThrowable);
}

foreach($componentList ?? [] as $componentLogic) {
$assembly = $componentLogic->assembly;
$componentElement = $componentLogic->component;
$this->serviceContainer->set($componentElement);

foreach($componentList ?? [] as $componentLogic) {
$assembly = $componentLogic->assembly;
$componentElement = $componentLogic->component;
$this->serviceContainer->set($componentElement);
try {
$this->handleLogicExecution(
$assembly,
$this->input,
$componentElement,
);
}
catch(Throwable $throwable) {
if(!$errorThrowable) {
throw $throwable;
}
}
}

try {
$this->handleLogicExecution(
$assembly,
$this->logicAssembly,
$this->input,
$componentElement,
);
}
catch(Throwable $throwable) {
if(!$errorThrowable) {
throw $throwable;
}
}
}

try {
$this->handleLogicExecution(
$this->logicAssembly,
$this->input,
);
}
catch(Throwable $throwable) {
if(!$errorThrowable) {
throw $throwable;
if($this->logicExecutionOrder) {
$this->response = $this->response->withHeader(
self::LOGIC_EXECUTION_HEADER,
implode(";", $this->logicExecutionOrder),
);
}
}

if($this->logicExecutionOrder) {
$this->response = $this->response->withHeader(
self::LOGIC_EXECUTION_HEADER,
implode(";", $this->logicExecutionOrder),
);
}

if($responseWithHeader = $this->headerManager->applyWithHeader(
$this->response->getResponseHeaders(),
$this->response->withHeader(...)
)) {
$this->response = $responseWithHeader;
}
if($responseWithHeader = $this->headerManager->applyWithHeader(
$this->response->getResponseHeaders(),
$this->response->withHeader(...)
)) {
$this->response = $responseWithHeader;
}

$documentBinder = $this->serviceContainer->get(Binder::class);
assert($documentBinder instanceof DocumentBinder);
$documentBinder->cleanupDocument();
$this->protectHtmlDocumentFromCsrf();
$documentBinder = $this->serviceContainer->get(Binder::class);
assert($documentBinder instanceof DocumentBinder);
$documentBinder->cleanupDocument();
$this->protectHtmlDocumentFromCsrf();

$this->viewStreamer->stream($this->view, $this->viewModel);
$this->viewStreamer->stream($this->view, $this->viewModel);
}
catch(ResponseInterrupt) {
return;
}
finally {
$this->interruptResponseFlow = false;
}
}
// phpcs:enable Generic.Metrics.CyclomaticComplexity.TooHigh

Expand Down
7 changes: 7 additions & 0 deletions src/Dispatch/ResponseInterrupt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
namespace GT\WebEngine\Dispatch;

use RuntimeException;

class ResponseInterrupt extends RuntimeException {
}
44 changes: 44 additions & 0 deletions test/phpunit/Dispatch/DispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,50 @@ public function testProcessResponse_rethrowsPageThrowableOutsideErrorMode():void
$sut->processResponse();
}

public function testGenerateResponse_stopsProcessingAfterRedirectFromLogic():void {
$processor = $this->createMock(ViewModelProcessor::class);
$processor->method("processDynamicPath");
$processor->method("processPartialContent")
->willReturn(new LogicAssemblyComponentList());

$viewStreamer = $this->createMock(ViewStreamer::class);
$viewStreamer->expects(self::never())
->method("stream");

$invocations = [];
$response = null;
$logicExecutor = $this->createMock(LogicExecutor::class);
$logicExecutor->method("invoke")
->willReturnCallback(function(Assembly $assembly, string $name)use(&$invocations, &$response):\Generator {
$invocations[] = [$assembly->current(), $name];
if($assembly->current() === "/tmp/page.php" && $name === "go") {
$response->redirect("./?new-state");
}
if(false) {
yield "";
}
});

$sut = $this->createDispatcher(
viewAssembly: $this->createAssembly("/tmp/page.html"),
logicAssembly: $this->createAssembly("/tmp/page.php"),
viewModelProcessor: $processor,
logicExecutor: $logicExecutor,
viewStreamer: $viewStreamer,
);
$response = $this->getPrivateProperty($sut, "response");
self::assertInstanceOf(Response::class, $response);

$generatedResponse = $sut->generateResponse();

self::assertSame(StatusCode::SEE_OTHER, $generatedResponse->getStatusCode());
self::assertSame("./?new-state", $generatedResponse->getHeaderLine("Location"));
self::assertSame([
["/tmp/page.php", "go_before"],
["/tmp/page.php", "go"],
], $invocations);
}

public function testProcessResponse_swallowsThrowablesDuringErrorModeAndBindsTrace():void {
$viewModel = new HTMLDocument(
'<!doctype html><body>'
Expand Down
Loading