Skip to content

More annotations for Symfony? #34095

@hktr92

Description

@hktr92

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\Common namespace;
  • edit the config/services.yaml and 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    RFCRFC = Request For Comments (proposals about features that you want to be discussed)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions