Creative Commons License
This work is licensed under a
Creative Commons
Attribution-Share Alike 3.0
Unported License.

Master Symfony2 fundamentals

Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).
trainings.sensiolabs.com

Symfony hosting done right

ServerGrove, outstanding support at the right price for your Symfony hosting needs.
servergrove.com

Discover the SensioLabs Support

Access to the SensioLabs Competency Center for an exclusive and tailor-made support on Symfony
sensiolabs.com

Il distributore consapevole del contenitore

Il distributore consapevole del contenitore

New in version 2.1: Questa caratteristiche è stata spostata nel componente EventDispatcher in Symfony 2.1.

Introduzione

La classe ContainerAwareEventDispatcher è una speciale implementazione di distributore di eventi, accoppiata con il contenitore di servizi, che fa parte del the Dependency Injection component. Questo consente di specificare i servizi come ascoltatori di eventi, rendendo il distributore di eventi molto potente.

Si servizi sono caricati in modo pigro, il che vuol dire che i servizi allegati come ascoltatori saranno creato solo se viene distribuito un evento che richieda tali ascoltatori.

Preparazione

La preparazione è molto semplice, basta iniettare un ContainerInterface in ContainerAwareEventDispatcher:

1
2
3
4
5
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher;

$container = new ContainerBuilder();
$dispatcher = new ContainerAwareEventDispatcher($container);

Aggiungere ascoltatori

Il distributore di eventi consapevole del contenitore può caricare direttamente servizi specifici, oppure servizi che implementino EventSubscriberInterface.

Gli esempi seguenti presumo che il DIC sia stato caricato con i servizi che vengono menzionati.

Note

I servizi devono essere segnati come pubblici nel DIC.

Aggiungere servizi

Per collegare definizioni di servizi esistenti, usare il metodo addListenerService(), dove $callback è un array array($idServizio, $nomeMetodo):

1
$dispatcher->addListenerService($eventName, array('pippo', 'logListener'));

Aggiungere servizi sottoscrittori

Si possono aggiungere degli EventSubscribers, usando il metodo addSubscriberService(), dove il primo parametro è l'ID del servizio sottoscrittore e il secondo parametro è il nome della classe del servizio (che deve implementare EventSubscriberInterface), come segue:

1
2
3
4
$dispatcher->addSubscriberService(
    'kernel.store_subscriber',
    'StoreSubscriber'
);

EventSubscriberInterface sarà esattamente come ci si può aspettare:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
// ...

class StoreSubscriber implements EventSubscriberInterface
{
    static public function getSubscribedEvents()
    {
        return array(
            'kernel.response' => array(
                array('onKernelResponsePre', 10),
                array('onKernelResponsePost', 0),
            ),
            'store.order'     => array('onStoreOrder', 0),
        );
    }

    public function onKernelResponsePre(FilterResponseEvent $event)
    {
        // ...
    }

    public function onKernelResponsePost(FilterResponseEvent $event)
    {
        // ...
    }

    public function onStoreOrder(FilterOrderEvent $event)
    {
        // ...
    }
}