secret
Created

Symfony Event Dispatcher Minimal Proof-of-Concept

  • Download Gist
gistfile1.php
PHP
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 32
use Symfony\Component\EventDispatcher\EventDispatcher,
Symfony\Component\EventDispatcher\Event;
 
class RandomEvent extends Event {
protected $data;
public function __construct($data) {
$this -> data = $data;
}
public function getData()
{
return $this -> data;
}
}
 
$dis = new EventDispatcher;
 
$dis -> addListener('foo.action', function (RandomEvent $e) {
// Change something about $e
$data = $e -> getData();
$data -> text = 'This text has been changed by the event listener';
});
 
$myData = new stdclass;
$myData -> text = 'This text as it was orignally';/**/
 
echo '<p>Before event dispatch: <code>' . print_r($myData, true) . '</code>';
echo '<p>Dispatching event…';
 
$event = new RandomEvent($myData);
$dis -> dispatch('foo.action', $event);
 
echo '<p>After event dispatch: <code>' . print_r($myData, true) . '</code>';
output.html
HTML
1 2 3 4 5 6 7 8 9
<p>Before event dispatch: <code>stdClass Object
(
[text] => This text as it was orignally
)
</code><p>Dispatching event…<p>After event dispatch: <code>stdClass Object
(
[text] => This text has been changed by the event listener
)
</code>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.