Async Symfony events - php

I'm trying to use https://github.com/fervo/FervoDeferredEventBundle in order to have async events to store data in my DB. The event is added to the RabbitMQ Queue correctly but the event listener and the event itself is never executed.
I'm trying in both ways: first letting the listener to do the job and also dispatching manually the deferevent.
Any idea?
Thank you!
My code:
in my Controller:
$event = new DeferEvent('save.data', new SaveDataEvent($data));
$this->get('event_dispatcher')->dispatch('fervo.defer', $event);
SaveDataEvent.php
<?php
namespace AppBundle\Event;
use Symfony\Component\EventDispatcher\Event;
class SaveDataEvent extends Event
{
protected $data;
public function __construct($data)
{
$this->data = $data;
}
public function getData()
{
return $this->data;
}
public function saveData()
{
$data = $this->getData();
// do more stuff
}
}
SaveDataListener.php
<?php
namespace AppBundle\EventListener;
use AppBundle\Event\SaveDataEvent;
class SaveDataListener
{
/**
* #var SaveDataEvent
*/
public function onSendData(SaveDataEvent $event)
{
$data = $event->saveData();
}
}
services.yml
app.save_data_listener:
class: AppBundle\EventListener\SaveDataListener
tags:
-
name: kernel.event_listener #fervo_deferred_event.listener
event: send.data

Reading the docs, you should fix the services.yml:
app.save_data_listener:
class: AppBundle\EventListener\SaveDataListener
tags:
name: fervo_deferred_event.listener
event: fervo.defer
Replace fervo.defer with the first value passed to dispatch() method if you want, it's the name of the dispatched event.

Related

Symfony - Event not being dispatched

This is the first time ever I am working with creating custom event dispatcher and subscriber so I am trying to wrap my head around it and I cant seem to find out why my custom event is not being dispatched.
I am following the documentation and in my case I need to dispatch an event as soon as someone registers on the site.
so inside my registerAction() I am trying to dispatch an event like this
$dispatcher = new EventDispatcher();
$event = new RegistrationEvent($user);
$dispatcher->dispatch(RegistrationEvent::NAME, $event);
This is my RegistrationEvent class
namespace AppBundle\Event;
use AppBundle\Entity\User;
use Symfony\Component\EventDispatcher\Event;
class RegistrationEvent extends Event
{
const NAME = 'registration.complete';
protected $user;
public function __construct(User $user)
{
$this->user = $user;
}
public function getUser(){
return $this->user;
}
}
This is my RegistrationSubscriber class
namespace AppBundle\Event;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class RegistrationSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return array(
KernelEvents::RESPONSE => array(
array('onKernelResponsePre', 10),
array('onKernelResponsePost', -10),
),
RegistrationEvent::NAME => 'onRegistration'
);
}
public function onKernelResponsePre(FilterResponseEvent $event)
{
// ...
}
public function onKernelResponsePost(FilterResponseEvent $event)
{
// ...
}
public function onRegistration(RegistrationEvent $event){
var_dump($event);
die;
}
}
After doing this, I was hoping that the registration process would stop at the function onRegistration but that did not happen, I then looked at the Events tab of the profiler and I do not see my Event listed their either.
What am I missing here? A push in right direction will really be appreciated.
Update:
I thought i need to register a service for the custom event so I added the following code inside services.yml
app.successfull_registration_subscriber:
class: AppBundle\Event\RegistrationSubscriber
arguments: ["#doctrine.orm.entity_manager"]
tags:
- { name: kernel.event_subscriber}
Inside the Event tab of profiler I do see my custom event being listed but it still does not dispatch.
By creating your own EventDispatcher instance you dispatch an event that can never be listened to by other listeners (they are not attached to this dispatcher instance). You need to use the event_dispatcher service to notify all listeners you have tagged with the kernel.event_listener and kernel.event_subscriber tags:
// ...
class RegistrationController extends Controller
{
public function registerAction()
{
// ...
$this->get('event_dispatcher')->dispatch(RegistrationEvent::NAME, new RegistrationEvent($user););
}
}
Duplicate of dispatcher doesn't dispatch my event symfony
With auto-wiring, it is now better to inject the EventDispatcherInterface
<?php
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
//...
class DefaultController extends Controller
{
public function display(Request $request, EventDispatcherInterface $dispatcher)
{
//Define your event
$event = new YourEvent($request);
$dispatcher->dispatch(YourEvent::EVENT_TO_DISPATCH, $event);
}
}

How to send var to view from event listener in symfony2?

I am trying to send var to view from event listener in symfony2 but I am stacked.
1) Is this possible?
2) What kernel event to use (kernel.view or kernel.request)?
3) Finally how to send some var to view?
My best guess is that I have to extend return from controller and then to let controller do his job.
I need to send some array of objects (entities).
I see several ways to handle this.
Adding a global variable from a kernel.request listener
The idea is to add a global variable straight after the kernel.request event.
services.yml
services:
class: Acme\FooBundle\Listener\MyListener
arguments:
- #twig
tags:
-
name: kernel.event_listener
event: kernel.request
method: onKernelRequest
MyListener
class MyListener
{
protected $twig;
public function __construct(\Twig_Environment $twig)
{
$this->twig = $twig;
}
public function onKernelRequest(GetResponseEvent $event)
{
$myVar = 'foo'; // Process data
$this->twig->addGlobal('myvar', $myVar);
}
}
You can now use it at any time by doing
{{ myvar }}
From a kernel.view listener
First, you need to understand when kernel.view is called. It's only called when the return of the controller is not an instance of Response object.
That said, doing
// Acme/FooBundle/FooController#fooAction
return $this->render();
returns a Response object, so kernel.view is not called.
Defining controllers
The idea is to make all controller returns an array of data, just like #Template requirements.
// Acme/FooBundle/FooController#fooAction
return array(
'template' => 'AcmeFooBundle:Foo:foo.html.twig',
'data' => array(
'entity' => $entity
)
);
Defining the service
Since you already have your service definition, you just need to add some requirements in your service declaration.
You need the #templating service to render the data.
You need to set itself as a kernel.view listener
// Acme/FooBundle/Resources/config/services.yml
services:
acme_foo.my_listener:
class: Acme\FooBundle\Listener\MyListener
arguments:
- #templating
tags:
-
name: kernel.event_listener
event: kernel.request
method: onKernelRequest
-
name: kernel.event_listener
event: kernel.view
method: onKernelView
Creating the service
// Acme/FooBundle/Listener/MyListener.php
use Symfony\Component\Templating\EngineInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
class MyListener
{
protected $templating;
protected $myVar;
public function __construct(EngineInterface $templating)
{
$this->templating = $templating;
}
public function getMyVar()
{
return $this->myVar;
}
public function onKernelRequest(GetResponseEvent $event)
{
$this->myVar = ""; // Process MyVar data
}
public function onKernelView(GetResponseForControllerResultEvent $event)
{
$result = $event->getControllerResult();
if (null === $this->myVar || !isset($result['template']) || !isset($result['data'])) {
return;
}
$data = array_merge($result['data'], array('myvar' => $this->myVar));
$rendered = $this->templating->render($result['template'], $data);
$event->setResponse(new Response($rendered));
}
}
And there you are. The listener is creating a new response, adding your custom definition of myvar to any template rendered by him.
From a TWIG extension
An alternative is to create a TWIG extension. In the following example, I'm assuming the MyListener definition is the same as above.
Defining services
As per the documentation given above, you just have to create a simple extension class.
// Acme/FooBundle/Resources/config/services.yml
services:
acme_foo.my_listener:
class: Acme\FooBundle\Listener\MyListener
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
acme_foo.my_extension:
class: Acme\FooBundle\Extension\MyExtension
arguments:
- #acme_foo.my_listener
tags:
- { name: twig.extension }
Defining the service
Just like in documentation, we'll create a simple function.
// Acme/FooBundle/Extension/MyExtension.php
use Acme\FooBundle\Listener\MyListener;
class MyExtension extends \Twig_Extension
{
protected $listener;
public function __construct(MyListener $listener)
{
$this->listener = $listener;
}
public function getName()
{
return 'my_extension';
}
public function getFunctions()
{
return array(
'myvar' => new \Twig_Function_Method($this, 'getMyVar')
);
}
public function getMyVar()
{
return $this->listener->getMyVar();
}
}
Usage
Then you can use it in any view by doing
{{ myvar() }}
From a common controller
I don't like this idea, but this is an alternative. You just have to create a BaseController which will override the default render method.
// Acme/FooBundle/Controller/BaseController.php
abstract class BaseController extends Controller
{
public function render($view, array $parameters = array(), Response $response = null)
{
$parameters = array_merge(
$parameters,
array(
'myvar' => $this->get('my_listener')->getMyVar()
)
);
return parent::render($view, $parameters, $response);
}
}
There's an alternative method here that I've had to do. I wanted to get some data, run it through json_encode(), then add that as a JavaScript variable to the response. Here's what I ended up doing.
I'm subscribing to kernel.response:
public static function getSubscribedEvents()
{
return [
KernelEvents::RESPONSE => 'onKernelResponse'
];
}
public function onKernelResponse(FilterResponseEvent $event)
{
/** -- SNIP -- Cutting out how I get my serialised data **/
$serialized = json_encode($data);
/** Shove it into the response as some JS at the bottom **/
$dom = new \DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTML($event->getResponse()->getContent());
libxml_use_internal_errors(false);
$node = $dom->createElement('script', "var data = $serialized;");
$dom->getElementsByTagName('body')->item(0)->appendChild($node);
$event->getResponse()->setContent($dom->saveHTML());
}
This is one way of doing it. Honestly, I don't like any of the methods described on this page. There should be a better way, but there isn't. This is what I'm using, though, and it works well. Just make sure you don't call your variable "data"; use something that won't be taken up elsewhere and preferably shove it in it's own (function() { } JS namespace.
I don't know how to pass variables directly to view, but you can change response object, with kernel.response event listener. Take a look how symfony debug toolbar injects bottom bar, you could use similar technique.
https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/WebProfilerBundle/EventListener/WebDebugToolbarListener.php#L106

Why is symfony2 not calling my event listeners?

I have a program with two bundles. One of them (CommonBundle) dispatches an event "common.add_channel", while a service on the other one (FetcherBundle) was supposed to be listening to it. On the profiler, I can see the event common.add_channel in the "Not Called Listeners" section. I don't get why symfony is not registering my listener.
This is my action, inside CommonBundle\Controller\ChannelController::createAction:
$dispatcher = new EventDispatcher();
$event = new AddChannelEvent($entity);
$dispatcher->dispatch("common.add_channel", $event);
This is my AddChannelEvent:
<?php
namespace Naroga\Reader\CommonBundle\Event;
use Symfony\Component\EventDispatcher\Event;
use Naroga\Reader\CommonBundle\Entity\Channel;
class AddChannelEvent extends Event {
protected $_channel;
public function __construct(Channel $channel) {
$this->_channel = $channel;
}
public function getChannel() {
return $this->_channel;
}
}
This was supposed to be my listener (FetcherService.php):
<?php
namespace Naroga\Reader\FetcherBundle\Service;
class FetcherService {
public function onAddChannel(AddChannelEvent $event) {
die("It's here!");
}
}
And here's where I register my listener (services.yml):
kernel.listener.add_channel:
class: Naroga\Reader\FetcherBundle\Service\FetcherService
tags:
- { name: kernel.event_listener, event: common.add_channel, method: onAddChannel }
What am I doing wrong? Why isn't symfony calling the event listener when I dispatch common.add_channel?
The new event dispatcher doesn't know anything about the listeners set on another dispatcher.
In your controller, you need to access the event_dispatcher service. A Compiler Pass of the Framework Bundle attached all listeners to this dispatcher. To get the service, use the Controller#get() shortcut:
// ...
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class ChannelController extends Controller
{
public function createAction()
{
$dispatcher = $this->get('event_dispatcher');
// ...
}
}

How to use SwitchUserListener in Symfony2?

I'm trying to do a redirect when a user impersonates another user.
For this I registered a service:
ACME_listener.security_switch_user:
class: ACME\CustomerLoginBundle\Listener\SecuritySwitchUserListener
arguments: [#service_container, #router, #security.context]
tags:
- { name: kernel.event_listener, event: security.switch_user, method: onSecuritySwitchUser }
My listener class looks like this:
namespace ACME\CustomerLoginBundle\Listener;
use Symfony\Component\Security\Http\Event\SwitchUserEvent;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\Security\Http\Firewall\ListenerInterface;
class SecuritySwitchUserListener implements ListenerInterface {
public function __construct($appContainer, $router) {
$this->router = $router;
$this->appContainer = $appContainer;
}
public function onSecuritySwitchUser(SwitchUserEvent $event) {
echo "im in here!";
// this does get called
}
public function handle(GetResponseEvent $event) {
echo "but not here :(";
// this does not get called!
}
}
Now the problem is that I can not redirect the user from within the onSecuritySwitchUser method. Returning a RedirectResponse does NOT work and the SwitchUserEvent does NOT have a setResponse() method.
What do I have to do so that the handle() method does get called?
I think that handle() is called from onSecuritySwitchUser(). But I can be wrong.
UPDATE
You can overwrite the event with your own request :)
Look at:
Symfony\Component\Security\Http\Firewall\SwitchUserListener
And then Dispach new SwitchUserEvent with overwritten request
if (null !== $this->dispatcher) {
$switchEvent = new SwitchUserEvent($request, $token->getUser());
$this->dispatcher->dispatch(SecurityEvents::SWITCH_USER, $switchEvent);
}
Maybe that will help you.

How to create custom event in symfony2

I want to create custom events called user_logged so that i can attach my listeners to those events.
I want to execute few functions whenever user has logged in.
Create a class which extends Symfony\Component\EventDispatcher\Event.
Then, use the event dispatcher service to dispatch the event:
$eventDispatcher = $container->get('event_dispatcher');
$eventDispatcher->dispatch('custom.event.identifier', $event);
You can register your event listener service like so:
tags:
- { name: kernel.event_listener, event: custom.event.identifier, method: onCustomEvent }
This answer is little bit extend answer.
services.yml
custom.event.home_page_event:
class: AppBundle\EventSubscriber\HomePageEventSubscriber
tags:
- { name: kernel.event_listener, event: custom.event.home_page_event, method: onCustomEvent }
AppBundle/EventSubscriber/HomePageEventSubscriber.php
namespace AppBundle\EventSubscriber;
class HomePageEventSubscriber
{
public function onCustomEvent($event)
{
var_dump($event->getCode());
}
}
AppBundle/Event/HomePageEvent.php
namespace AppBundle\Event;
use Symfony\Component\EventDispatcher\Event;
class HomePageEvent extends Event
{
private $code;
public function setCode($code)
{
$this->code = $code;
}
public function getCode()
{
return $this->code;
}
}
anywhere you wish, for example in home page controller
use AppBundle\Event\HomePageEvent;
// ...
$eventDispatcher = $this->get('event_dispatcher');
$event = new HomePageEvent();
$event->setCode(200);
$eventDispatcher->dispatch('custom.event.home_page_event', $event);

Categories