diff --git a/src/Dispatch/Dispatcher.php b/src/Dispatch/Dispatcher.php index f24843af..e232b769 100644 --- a/src/Dispatch/Dispatcher.php +++ b/src/Dispatch/Dispatcher.php @@ -55,6 +55,7 @@ */ class Dispatcher { private const LOGIC_EXECUTION_HEADER = "X-Logic-Execution"; + private const COMPONENT_INPUT_NAME = "__component"; private Config $config; private Request $request; @@ -381,20 +382,13 @@ private function handleLogicExecution( $this->recordLogicExecution($functionReference); } -// TODO: No need to have the whole Input class. Just pass a nullable string in called $doMethod, from $input->getString("do") - $input->when("do")->call( - function(InputData $data)use($logicAssembly, $extraArgs) { - $doName = "do_" . str_replace( - "-", - "_", - $data->getString("do"), - ); + if($doAction = $this->getDoActionForLogic($input, $component)) { + $doName = "do_" . str_replace("-", "_", $doAction); - foreach($this->logicExecutor->invoke($logicAssembly, $doName, $extraArgs) as $functionReference) { - $this->recordLogicExecution($functionReference); - } + foreach($this->logicExecutor->invoke($logicAssembly, $doName, $extraArgs) as $functionReference) { + $this->recordLogicExecution($functionReference); } - ); + } foreach($this->logicExecutor->invoke($logicAssembly, "go", $extraArgs) as $functionReference) { $this->recordLogicExecution($functionReference); @@ -405,6 +399,28 @@ function(InputData $data)use($logicAssembly, $extraArgs) { } } + private function getDoActionForLogic(Input $input, ?Element $component):?string { + $doAction = $input->getString("do"); + if($doAction === null || $doAction === "") { + return null; + } + + $submittedComponent = $input->getString(self::COMPONENT_INPUT_NAME); + if($submittedComponent === null || $submittedComponent === "") { + return $doAction; + } + + if(!$component) { + return null; + } + + if(strtolower($component->tagName) !== strtolower($submittedComponent)) { + return null; + } + + return $doAction; + } + /** * @return void */ diff --git a/test/phpunit/Dispatch/DispatcherTest.php b/test/phpunit/Dispatch/DispatcherTest.php index b909960b..1f9dc104 100644 --- a/test/phpunit/Dispatch/DispatcherTest.php +++ b/test/phpunit/Dispatch/DispatcherTest.php @@ -242,6 +242,70 @@ public function testGenerateResponse_executesComponentAndPageLogicAndAppliesHead self::assertSame([], $invocations[4]["extraArgs"]); } + public function testGenerateResponse_componentFormDoActionDoesNotRunPageDoAction():void { + $html = << +
+ +