From b9e1b799d4f522a4801aa678ea1970ba25137f82 Mon Sep 17 00:00:00 2001 From: mhsdesign <85400359+mhsdesign@users.noreply.github.com> Date: Tue, 30 Jan 2024 11:53:41 +0100 Subject: [PATCH] TASK: Introduce trait `ActionToMethodDelegation` instead of `SimpleActionController`. The name of the previously suggested `SimpleActionController` looked for improvement. The `Simple*` prefix suggest, that the initial implementation blew up too much. But ideally a name would be more obvious and not suggest that there is another _complex_ action controller. I could not come up with a better name, but we can make the controllers more composable. This change introduces a trait which implements the logic of the previous `SimpleActionController`. That way people can implement the `ControllerInterface` directly and use a trait that will map action to method name. --- .../Controller/ActionToMethodDelegation.php | 59 +++++++++++++++++++ .../Mvc/Controller/SimpleActionController.php | 49 --------------- .../SimpleActionControllerTestController.php | 10 ++-- ...t.php => ActionToMethodDelegationTest.php} | 7 +-- .../Fixtures/SimpleActionTestController.php | 10 ++-- 5 files changed, 72 insertions(+), 63 deletions(-) create mode 100644 Neos.Flow/Classes/Mvc/Controller/ActionToMethodDelegation.php delete mode 100644 Neos.Flow/Classes/Mvc/Controller/SimpleActionController.php rename Neos.Flow/Tests/Unit/Mvc/Controller/{SimpleActionControllerTest.php => ActionToMethodDelegationTest.php} (93%) diff --git a/Neos.Flow/Classes/Mvc/Controller/ActionToMethodDelegation.php b/Neos.Flow/Classes/Mvc/Controller/ActionToMethodDelegation.php new file mode 100644 index 0000000000..cfc326a9f0 --- /dev/null +++ b/Neos.Flow/Classes/Mvc/Controller/ActionToMethodDelegation.php @@ -0,0 +1,59 @@ +setDispatched(true); + $actionMethodName = $this->resolveActionMethodName($request); + return $this->$actionMethodName($request); + } + + /** + * Resolves and checks the current action method name + * + * @return string Method name of the current action + * @throws NoSuchActionException + */ + private function resolveActionMethodName(ActionRequest $request): string + { + $actionMethodName = $request->getControllerActionName() . 'Action'; + if (!is_callable([$this, $actionMethodName])) { + throw new NoSuchActionException(sprintf('An action "%s" does not exist in controller "%s".', $actionMethodName, get_class($this)), 1186669086); + } + + return $actionMethodName; + } +} diff --git a/Neos.Flow/Classes/Mvc/Controller/SimpleActionController.php b/Neos.Flow/Classes/Mvc/Controller/SimpleActionController.php deleted file mode 100644 index 35b2e3e596..0000000000 --- a/Neos.Flow/Classes/Mvc/Controller/SimpleActionController.php +++ /dev/null @@ -1,49 +0,0 @@ -setDispatched(true); - $actionMethodName = $this->resolveActionMethodName($request); - return $this->$actionMethodName($request); - } - - /** - * Resolves and checks the current action method name - * - * @return string Method name of the current action - * @throws NoSuchActionException - */ - protected function resolveActionMethodName(ActionRequest $request): string - { - $actionMethodName = $request->getControllerActionName() . 'Action'; - if (!is_callable([$this, $actionMethodName])) { - throw new NoSuchActionException(sprintf('An action "%s" does not exist in controller "%s".', $actionMethodName, get_class($this)), 1186669086); - } - - return $actionMethodName; - } -} diff --git a/Neos.Flow/Tests/Functional/Mvc/Fixtures/Controller/SimpleActionControllerTestController.php b/Neos.Flow/Tests/Functional/Mvc/Fixtures/Controller/SimpleActionControllerTestController.php index 990b6c2256..1904a393df 100644 --- a/Neos.Flow/Tests/Functional/Mvc/Fixtures/Controller/SimpleActionControllerTestController.php +++ b/Neos.Flow/Tests/Functional/Mvc/Fixtures/Controller/SimpleActionControllerTestController.php @@ -3,13 +3,13 @@ use Neos\Flow\Mvc\ActionRequest; use Neos\Flow\Mvc\ActionResponse; -use Neos\Flow\Mvc\Controller\SimpleActionController; +use Neos\Flow\Mvc\Controller\ActionToMethodDelegation; +use Neos\Flow\Mvc\Controller\ControllerInterface; -/** - * - */ -class SimpleActionControllerTestController extends SimpleActionController +class SimpleActionControllerTestController implements ControllerInterface { + use ActionToMethodDelegation; + public function indexAction(ActionRequest $actionRequest): ActionResponse { $response = new ActionResponse(); diff --git a/Neos.Flow/Tests/Unit/Mvc/Controller/SimpleActionControllerTest.php b/Neos.Flow/Tests/Unit/Mvc/Controller/ActionToMethodDelegationTest.php similarity index 93% rename from Neos.Flow/Tests/Unit/Mvc/Controller/SimpleActionControllerTest.php rename to Neos.Flow/Tests/Unit/Mvc/Controller/ActionToMethodDelegationTest.php index ba7461a455..51313c3cd0 100644 --- a/Neos.Flow/Tests/Unit/Mvc/Controller/SimpleActionControllerTest.php +++ b/Neos.Flow/Tests/Unit/Mvc/Controller/ActionToMethodDelegationTest.php @@ -13,15 +13,14 @@ use Neos\Flow\Mvc\ActionRequest; use Neos\Flow\Mvc\ActionResponse; -use Neos\Flow\Mvc\Controller\SimpleActionController; +use Neos\Flow\Mvc\Controller\ActionToMethodDelegation; use Neos\Flow\Mvc\Exception\NoSuchActionException; use Neos\Flow\Tests\UnitTestCase; /** - * Tests for - * @see SimpleActionController + * Tests for @see ActionToMethodDelegation */ -class SimpleActionControllerTest extends UnitTestCase +class ActionToMethodDelegationTest extends UnitTestCase { /** * Note: additional checks like "123" or "Foo" might seem sensible but those cases are prevented in the ActionRequest already. diff --git a/Neos.Flow/Tests/Unit/Mvc/Controller/Fixtures/SimpleActionTestController.php b/Neos.Flow/Tests/Unit/Mvc/Controller/Fixtures/SimpleActionTestController.php index 4670322eb7..c0dfdd5701 100644 --- a/Neos.Flow/Tests/Unit/Mvc/Controller/Fixtures/SimpleActionTestController.php +++ b/Neos.Flow/Tests/Unit/Mvc/Controller/Fixtures/SimpleActionTestController.php @@ -3,13 +3,13 @@ use Neos\Flow\Mvc\ActionRequest; use Neos\Flow\Mvc\ActionResponse; -use Neos\Flow\Mvc\Controller\SimpleActionController; +use Neos\Flow\Mvc\Controller\ActionToMethodDelegation; +use Neos\Flow\Mvc\Controller\ControllerInterface; -/** - * - */ -class SimpleActionTestController extends SimpleActionController +class SimpleActionTestController implements ControllerInterface { + use ActionToMethodDelegation; + public function addTestContentAction(ActionRequest $actionRequest): ActionResponse { $response = new ActionResponse();