PHP code example of event-band / symfony-bundle

1. Go to this page and download the library: Download event-band/symfony-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

event-band / symfony-bundle example snippets



namespace Acme\EventBundle\Event;

class EchoEventListener
{
    public function onEchoEvent(EchoEvent $event)
    {
        // don't do such things on production
        echo $event->getMessage();
        echo "\n";
    }
}

$dispatcher = $this->getContainer()->get('event_dispatcher');
$dispatcher->dispatch('event.echo', new EchoEvent('Hi, guys!'));
 php

namespace Acme\EventBundle\Event;
use EventBand\Adapter\Symfony\SerializableSymfonyEvent;

class EchoEvent extends SerializableSymfonyEvent
{
    /**
     * @var string
     **/
    protected $message;

    public function __construct($message)
    {
        $this->message = $message;
    }

    public funciton getMessage()
    {
        return $this->message;
    }
    
    protected function toSerializableArray()
    {
        $array = parent::toSerializableArray();
        $array['message'] = $this->message;

        return $array;
    }
    
    protected function fromUnserializedArray(array $data)
    {
        parent::fromUnserializedArray($data);
        $this->message = $data['message'];
    }
}
xml
<service id="acme.event_bundle.event.event_listener" class="Acme\EventBundle\EchoEventListener">
     <tag name="kernel.event_listener" event="event.echo" method="onEchoEvent"/>
</service>
yml
event_band:
     publishers:
         acme.echo.event.publisher:
             events: ["event.echo"]
             transport:
                 amqp:
                     exchange: acme.echo.event.exchange
     consumers:
         acme.echo.event: ~
xml
<service id="acme.event_bundle.event.event_listener" class="Acme\EventBundle\EchoEventListener">
     <tag name="kernel.event_listener" event="event.echo" method="onEchoEvent" band="acme.echo.event"/>
</service>
yml
    transports:
         amqp:
             driver: amqplib
             connections:
                 default:
                     exchanges:
                         acme.echo.event.exchange: ~
                     queues:
                         acme.echo.event:
                             bind:
                                 acme.echo.event.exchange: ['event.echo']
bash
sh$ app/cosole event-band:dispatch acme.echo.event
Hi, guys!
 php

namespace Acme\EventBundle\Event;

class EchoEvent implements \EventBand\Event
{
    /**
     * @var array
     * @JMS\Serializer\Annotation\Type("array")
     */
    private $data;

    public function __construct($message)
    {
        $this->date['message'] = $message;
    }

    public function getMessage()
    {
        return $this->data['message'];
    }
}