Symfony 2 - пример реализации событийного управления

Отправлено admin от ср, 25.05.2016, 16:59
  1. use Symfony\Component\EventDispatcher\EventDispatcher;
  2. use Symfony\Component\Debug\Debug;
  3. use Symfony\Component\EventDispatcher\Event;
  4.  
  5.  
  6. $loader = require_once __DIR__.'/../app/bootstrap.php.cache';
  7. Debug::enable();
  8.  
  9.  
  10. $EventDispatcher = new EventDispatcher();
  11.  
  12.  
  13. class TestListener
  14. {
  15. public function onTestAction(Event $event)
  16. {
  17. echo $event->getValue();
  18. }
  19. }
  20.  
  21. $TestListener = new TestListener();
  22.  
  23. $EventDispatcher->addListener('a1', array(
  24. $TestListener,
  25. 'onTestAction'
  26. ));
  27.  
  28. class TestEvent extends Event
  29. {
  30.  
  31. private $value;
  32.  
  33. public function setValue($value)
  34. {
  35. $this->value = $value;
  36. }
  37.  
  38. public function getValue()
  39. {
  40. return $this->value;
  41. }
  42. }
  43.  
  44. $event = new TestEvent();
  45. $event->setValue('Hello world!');
  46.  
  47. $EventDispatcher->dispatch('a1', $event);

Комментарии