Skip to content

Commit

Permalink
TASK: Introduce trait ActionToMethodDelegation instead of `SimpleAc…
Browse files Browse the repository at this point in the history
…tionController`.

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.
  • Loading branch information
mhsdesign committed Jan 30, 2024
1 parent 57170ee commit b9e1b79
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 63 deletions.
59 changes: 59 additions & 0 deletions Neos.Flow/Classes/Mvc/Controller/ActionToMethodDelegation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace Neos\Flow\Mvc\Controller;

use Neos\Flow\Mvc\ActionRequest;
use Neos\Flow\Mvc\ActionResponse;
use Neos\Flow\Mvc\Exception\NoSuchActionException;

/**
* Helps to implement a controller with direct access to flows request/response abstraction.
*
* ```
* ┌─────────────────────────────────────────────────────────────────────────────┐
* │ class MyController implements ControllerInterface │
* │ { │
* │ use ActionToMethodDelegation; │
* │ public function myAction(ActionRequest $actionRequest): ActionResponse; │
* │ } │
* └─────────────────────────────────────────────────────────────────────────────┘
* ```
*
* The request comes directly from the dispatcher and goes directly back to it.
*
* For helpers to facilitate throws, forwards, redirects: {@see SpecialResponsesSupport}
*
* Views or other processing needs to be added to your controller as needed,
* helpers will be suggested here as they become available.
* @api
*/
trait ActionToMethodDelegation
{
/**
* @internal you don't need to use this trait if you need to override this functionality.
*/
final public function processRequest(ActionRequest $request): ActionResponse
{
$request->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;
}
}
49 changes: 0 additions & 49 deletions Neos.Flow/Classes/Mvc/Controller/SimpleActionController.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit b9e1b79

Please sign in to comment.