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
66 changes: 54 additions & 12 deletions src/Dispatch/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
* @SuppressWarnings("PHPMD.ExcessiveClassComplexity")
*/
class Dispatcher {
private const LOGIC_EXECUTION_HEADER = "X-Logic-Execution";

private Config $config;
private Request $request;
/** @var array<string, array<string, string|array<string, string>>> */
Expand All @@ -80,6 +82,8 @@ class Dispatcher {
private HeaderManager $headerManager;
private Closure $viewInitCb;
private bool $redirectPrepared = false;
/** @var array<int, string> */
private array $logicExecutionOrder = [];

/**
* @param array<string, array<string, string|array<string, string>>> $globals
Expand Down Expand Up @@ -369,9 +373,8 @@ private function handleLogicExecution(
$extraArgs[$legacyElementClass] = $component;
}

foreach($this->logicExecutor->invoke($logicAssembly, "go_before", $extraArgs) as $file) {
// Force generator execution even when debug output is disabled.
continue;
foreach($this->logicExecutor->invoke($logicAssembly, "go_before", $extraArgs) as $functionReference) {
$this->recordLogicExecution($functionReference);
}

// TODO: No need to have the whole Input class. Just pass a nullable string in called $doMethod, from $input->getString("do")
Expand All @@ -383,21 +386,18 @@ function(InputData $data)use($logicAssembly, $extraArgs) {
$data->getString("do"),
);

foreach($this->logicExecutor->invoke($logicAssembly, $doName, $extraArgs) as $file) {
// Force generator execution even when debug output is disabled.
continue;
foreach($this->logicExecutor->invoke($logicAssembly, $doName, $extraArgs) as $functionReference) {
$this->recordLogicExecution($functionReference);
}
}
);

foreach($this->logicExecutor->invoke($logicAssembly, "go", $extraArgs) as $file) {
// Force generator execution even when debug output is disabled.
continue;
foreach($this->logicExecutor->invoke($logicAssembly, "go", $extraArgs) as $functionReference) {
$this->recordLogicExecution($functionReference);
}

foreach($this->logicExecutor->invoke($logicAssembly, "go_after", $extraArgs) as $file) {
// Force generator execution even when debug output is disabled.
continue;
foreach($this->logicExecutor->invoke($logicAssembly, "go_after", $extraArgs) as $functionReference) {
$this->recordLogicExecution($functionReference);
}
}

Expand All @@ -408,6 +408,7 @@ function(InputData $data)use($logicAssembly, $extraArgs) {
public function processResponse(
?Throwable $errorThrowable = null,
):void {
$this->logicExecutionOrder = [];
$dynamicPath = $this->serviceContainer->get(DynamicPath::class);

$this->viewModelProcessor?->processDynamicPath(
Expand Down Expand Up @@ -460,6 +461,13 @@ public function processResponse(
}
}

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(...)
Expand All @@ -476,6 +484,40 @@ public function processResponse(
}
// phpcs:enable Generic.Metrics.CyclomaticComplexity.TooHigh

private function recordLogicExecution(string $functionReference):void {
$referenceWithoutAttributes = explode("#", $functionReference, 2)[0];
$separatorPosition = strrpos($referenceWithoutAttributes, "::");
if($separatorPosition === false) {
return;
}

$file = substr($referenceWithoutAttributes, 0, $separatorPosition);
$function = substr($referenceWithoutAttributes, $separatorPosition + 2);
$function = preg_replace('/\(\)$/', '', $function);
if($function === null || $function === "") {
return;
}

$this->logicExecutionOrder[] = sprintf(
"%s:%s",
$this->normaliseLogicExecutionFile($file),
$function,
);
}

private function normaliseLogicExecutionFile(string $file):string {
$cwd = getcwd();
if($cwd && str_starts_with($file, $cwd . DIRECTORY_SEPARATOR)) {
$file = substr($file, strlen($cwd) + 1);
}

if(str_ends_with($file, ".php")) {
$file = substr($file, 0, -4);
}

return $file;
}

private function verifyCsrfRequest(string $method, InputData $inputData):void {
if($method !== "POST" || $this->isIgnoredCsrfPath()) {
return;
Expand Down
15 changes: 12 additions & 3 deletions test/phpunit/Dispatch/DispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,7 @@ public function testGenerateResponse_executesComponentAndPageLogicAndAppliesHead
"name" => $name,
"extraArgs" => $extraArgs,
];
if(false) {
yield "never";
}
yield ($assembly === $componentAssembly ? "/tmp/component.php" : "/tmp/page.php") . "::$name()";
});

$headerManager = $this->createMock(HeaderManager::class);
Expand Down Expand Up @@ -210,6 +208,17 @@ public function testGenerateResponse_executesComponentAndPageLogicAndAppliesHead

self::assertSame(StatusCode::OK, $response->getStatusCode());
self::assertSame("applied", $response->getHeaderLine("X-Test"));
self::assertSame(
"/tmp/component:go_before;"
. "/tmp/component:do_save_item;"
. "/tmp/component:go;"
. "/tmp/component:go_after;"
. "/tmp/page:go_before;"
. "/tmp/page:do_save_item;"
. "/tmp/page:go;"
. "/tmp/page:go_after",
$response->getHeaderLine("X-Logic-Execution"),
);
self::assertSame([
["assembly" => "component", "name" => "go_before"],
["assembly" => "component", "name" => "do_save_item"],
Expand Down
Loading