06/10/2015 - SYMFONY
Event dispatcher ile kendi eventimizi yaratıp, yine kendi yarattığımız bir event listener ile yakalayacağız. Bu örnek bir mesaj yaratıp, bir mesajı update edecek. Her iki durumda da birer event gönderilir. Daha fazla bilgi için The EventDispatcher Component linkini ziyaret edin.
services:
application_backend.controller.message:
class: Application\BackendBundle\Controller\MessageController
arguments:
- @event_dispatcher
namespace Application\BackendBundle\Controller;
use Application\BackendBundle\Event\MessageEvent;
use Application\BackendBundle\Model\Message;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
/**
* @Route("message", service="application_backend.controller.message")
*/
class MessageController extends Controller
{
private $eventDispatcher;
public function __construct(EventDispatcherInterface $eventDispatcher)
{
$this->eventDispatcher = $eventDispatcher;
}
/**
* @Method({"GET"})
* @Route("/create")
*/
public function createAction()
{
foreach ([1 => 'Hello', 2 => 'Bye'] as $id => $body) {
$message = new Message();
$message->id = $id;
$message->body = $body;
$this->eventDispatcher->dispatch(
MessageEvent::EVENT_NAME_PREFIX.'create_message',
new MessageEvent($message)
);
}
return new Response('Create done');
}
/**
* @Method({"GET"})
* @Route("/update")
*/
public function updateAction()
{
foreach ([1 => 'Welcome'] as $id => $body) {
$message = new Message();
$message->id = $id;
$message->body = $body;
$this->eventDispatcher->dispatch(
MessageEvent::EVENT_NAME_PREFIX.'update_message',
new MessageEvent($message)
);
}
return new Response('Update done');
}
}
namespace Application\BackendBundle\Model;
class Message
{
public $id;
public $body;
}
namespace Application\BackendBundle\Event;
use Application\BackendBundle\Model\Message;
use Symfony\Component\EventDispatcher\Event;
class MessageEvent extends Event
{
const EVENT_NAME_PREFIX = 'application_backend.event.';
private $message;
public function __construct(Message $message)
{
$this->message = $message;
}
public function getMessage()
{
return $this->message;
}
}
services:
application_backend.event_listener.message:
class: Application\BackendBundle\EventListener\MessageListener
tags:
- { name: kernel.event_listener, event: application_backend.event.create_message, method: onCreateMessage }
- { name: kernel.event_listener, event: application_backend.event.update_message, method: onUpdateMessage }
arguments: [@logger]
namespace Application\BackendBundle\EventListener;
use Application\BackendBundle\Event\MessageEvent;
use Psr\Log\LoggerInterface;
class MessageListener
{
private $logger;
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
public function onCreateMessage(MessageEvent $messageEvent)
{
$message = $messageEvent->getMessage();
$this->logger->info(
sprintf(
'Create Message: ID [%s] BODY [%s] @ %s',
$message->id,
$message->body,
date('d/m/Y H:i:s')
)
);
$messageEvent->stopPropagation();
}
public function onUpdateMessage(MessageEvent $messageEvent)
{
$message = $messageEvent->getMessage();
$this->logger->info(
sprintf(
'Update Message: ID [%s] BODY [%s] @ %s',
$message->id,
$message->body,
date('d/m/Y H:i:s')
)
);
$messageEvent->stopPropagation();
}
}
Eğer http://football.local/app_dev.php/backend/message/create
ve http://football.local/app_dev.php/backend/message/update
adreslerini ziyaret ederseniz, aşağıdaki dev.log bilgilerini göreceksiniz.
--------------------- CREATE
[2015-10-06 20:53:11] request.INFO: Matched route "application_backend_message_create"
(parameters:
"_controller": "application_backend.controller.message:createAction",
"_route": "application_backend_message_create") [] []
[2015-10-06 20:53:11] app.INFO: Create Message: ID [1] BODY [Hello] @ 06/10/2015 20:53:11 [] []
[2015-10-06 20:53:11] event.DEBUG: Notified event "application_backend.event.create_message" to listener
"Application\BackendBundle\EventListener\MessageListener::onCreateMessage". [] []
[2015-10-06 21:07:57] event.DEBUG: Listener "Application\BackendBundle\EventListener\MessageListener::onCreateMessage"
stopped propagation of the event "application_backend.event.create_message". [] []
[2015-10-06 20:53:11] app.INFO: Create Message: ID [2] BODY [Bye] @ 06/10/2015 20:53:11 [] []
[2015-10-06 20:53:11] event.DEBUG: Notified event "application_backend.event.create_message" to listener
"Application\BackendBundle\EventListener\MessageListener::onCreateMessage". [] []
[2015-10-06 20:53:11] event.DEBUG: Listener "Application\BackendBundle\EventListener\MessageListener::onCreateMessage"
stopped propagation of the event "application_backend.event.create_message". [] []
--------------------- UPDATE
[2015-10-06 20:53:18] request.INFO: Matched route "application_backend_message_update"
(parameters:
"_controller": "application_backend.controller.message:updateAction",
"_route": "application_backend_message_update") [] []
[2015-10-06 20:53:18] app.INFO: Update Message: ID [1] BODY [Welcome] @ 06/10/2015 20:53:18 [] []
[2015-10-06 20:53:18] event.DEBUG: Notified event "application_backend.event.update_message" to listener
"Application\BackendBundle\EventListener\MessageListener::onUpdateMessage". [] []
[2015-10-06 20:53:18] event.DEBUG: Listener "Application\BackendBundle\EventListener\MessageListener::onUpdateMessage"
stopped propagation of the event "application_backend.event.update_message". [] []