Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Adr] Kernel events vs controllers #500

Open
wants to merge 2 commits into
base: 1.10
Choose a base branch
from
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
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/adr export-ignore
70 changes: 70 additions & 0 deletions adr/0000-kernel-events.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Kernel events

* Status: new
* Deciders: @lchrusciel, @loic425, @Zales0123
* External advices: @soyuka, @dunglas, @mtarld

## Context and Problem Statement

ResourceController has all the actions, a lot of pretty similar code on each action and hard to maintain/test.
and it is not flexible enough in its behavior.
Comment on lines +9 to +10
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ResourceController has all the actions, a lot of pretty similar code on each action and hard to maintain/test.
and it is not flexible enough in its behavior.
Over the years ResourceController become really bloated. It has all the actions, and a lot of pretty similar code on each of them and the default approach to extend its logic is to extend the class. This class has 17 dependencies, which makes it hard to maintain and test. Moreover, some dependencies are not needed for every method call. As a result, this class does not follows SOLID principles and we want to improve its desing


## Considered Options

* Use Kernel events
* Use one controller per action

## PreWriteListener

### With kernel events

```php
$resourceEvent = $this->eventDispatcher->dispatchPreEvent($operation->getName(), $configuration, $controllerResult);
$request->attributes->set('resource_event', $resourceEvent);

if (!$resourceEvent->isStopped()) {
return;
}

if (null !== $resourceEventResponse = $resourceEvent->getResponse()) {
$event->setControllerResult($resourceEventResponse);

return;
}

if ($operation instanceof CreateOperationInterface) {
$event->setControllerResult($this->redirectHandler->redirectToIndex($configuration, $controllerResult));

return;
}

$event->setControllerResult($this->redirectHandler->redirectToResource($configuration, $controllerResult));
```

Moreover, it allows to use a custom controller free of dependencies injection.

```php
#[Resource(alias: 'app.book')]
#[Create(
factory: false, // to disable resource factory
controller: CreateBookAction::class,
template: 'book/create.html.twig',
)]
class Book implements ResourceInterface
```

```php
class CreateBookAction
{
public function __invoke(#[CurrentUser] $user): Book
{
return (new Book())->setCreatedBy($user);
}
}
```

For end-user, he could also disable the existing listeners and build its own way to implement it.

### With controllers

With controllers, to have a quick look for all these little differences, we have to create a new service with this code and it creates another service injected in our controller.