-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
Description
Description
Is there a possibility that in Symfony 5 we can have a more powerful annotation usage? Defining routes from annot is genius, as it saves time and files. But can we benefit in other places, like in DI/Service definition?
I've recently implemented a security feature in an internal web app, so I had to tap into Doctrine events. Currently (Symfony 4.3) I have to:
- write the subscriber class, that implements EventSubscriber from
Doctrine\Commonnamespace; - edit the
config/services.yamland tag my service (~20 additional lines of code)
This could be simplified more like this, if it were annot:
<?php
declare(strict_types=1);
namespace App\EventSubscriber;
use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Event\OnFlushEventArgs;
use Doctrine\ORM\Events;
/**
* @Service(tags=[
* { name: 'doctrine.orm.entity_listener', ...other config }
* ])
*
* Or this could be "simplified" as:
* @Service(tags=['doctrine.orm.entity_listener'])
*/
class MyDoctrineSubscriber implements EventSubscriber {
/**
* Returns an array of events this subscriber wants to listen to.
*
* @return string[]
*/
public function getSubscribedEvents(): array {
return [
Events::onFlush,
Events::postLoad,
];
}
/**
* Or if we can get rid of getSubscribedEvents():
* @OnFlush
* @see https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/events.html#lifecycle-callbacks
*/
public function onFlush(OnFlushEventArgs $Event): void {
// do stuff...
}
/** @PostLoad(priority=-255) */
public function postLoad(): void {
// do stuff...
}
}Doctrine currently supports having lifecycle event annotations, as in example, but limited only to entity. the separation of concerns is having a key role here, as I don't want to mix entity def with event listening.
I think that we can all benefit from these annotation in other places than routing and event lifecycle inside doctrine entity.