Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [SF2][SF]Symfony2 Events
Matrix12
post 21.07.2015, 13:04:30
Post #1





Grupa: Zarejestrowani
Postów: 144
Pomógł: 0
Dołączył: 22.03.2015

Ostrzeżenie: (0%)
-----


Witam,

Szukam jakiegoś dobego tutorialu z których móglbym się nauczyć eventów..

Np. mam taki kod :
  1. <?php
  2.  
  3. use Symfony\Component\EventDispatcher\Event;
  4.  
  5. class AwesomeWorkEvent extends Event
  6. {
  7.  
  8. /**
  9.   * @var OneDataClass
  10.   */
  11. protected $data;
  12.  
  13. public function __construct(OneDataClass $data)
  14. {
  15. $this->data = $data;
  16. }
  17.  
  18. public function getData()
  19. {
  20. return $this->data;
  21. }
  22. }


Potem tworzymy listnera
  1.  
  2.  
  3. class AwesomeWorkListener
  4. {
  5. public function doYourJob(AwesomeWorkEvent $event)
  6. {
  7. /** Do the right job **/
  8. }
  9. }


Czy listner to serwis obsługujacy eventy ?
Czy zawsze w evencie dodaje metode getData? Dlaczego ? Po co eventowi te dane? Czy event działa na zasadzie : informuje event o jakimś zdarzeniu, even pobiera sobie dane i sam przekazuje do listnera?


Pomóżcie mi to ogarnąć smile.gif
Go to the top of the page
+Quote Post
Crozin
post 21.07.2015, 14:21:57
Post #2





Grupa: Zarejestrowani
Postów: 6 476
Pomógł: 1306
Dołączył: 6.08.2006
Skąd: Kraków

Ostrzeżenie: (0%)
-----


Cytat
Czy listner to serwis obsługujacy eventy ?
Jest to ogóle coś co można "uruchomić" (PHP-owe callable): http://symfony.com/doc/current/components/...cting-listeners Nie musi to być koniecznie obiekt będący serwisem z punktu widzenia DIC-a.
Cytat
Czy zawsze w evencie dodaje metode getData?
Nie, nie musisz mieć tej metody. Zdarzenie musi jednak być potomkiem klasy \Symfony]Component\EventDispatcher\Event.
Cytat
Czy event działa na zasadzie : informuje event o jakimś zdarzeniu, even pobiera sobie dane i sam przekazuje do listnera?
Nie. Zdarzenie (event) jest informacją. EventDispatcher wywołuje odpowiednie listenery i przekazuje im informację właśnie w postaci zdarzenia.
Go to the top of the page
+Quote Post
Matrix12
post 21.07.2015, 18:55:22
Post #3





Grupa: Zarejestrowani
Postów: 144
Pomógł: 0
Dołączył: 22.03.2015

Ostrzeżenie: (0%)
-----


A możesz mi to przykładowo przedstawić? Bo nie mam żadnego przykładu z którego mógłbym to ogarnąć
Go to the top of the page
+Quote Post
Crozin
post 21.07.2015, 19:07:29
Post #4





Grupa: Zarejestrowani
Postów: 6 476
Pomógł: 1306
Dołączył: 6.08.2006
Skąd: Kraków

Ostrzeżenie: (0%)
-----


Ale co, całe przykłady wykorzystania EventDispatchera? Zacznij od http://symfony.com/doc/current/cookbook/ev...cher/index.html - masa przykładów.
Go to the top of the page
+Quote Post
Matrix12
post 22.07.2015, 18:04:55
Post #5





Grupa: Zarejestrowani
Postów: 144
Pomógł: 0
Dołączył: 22.03.2015

Ostrzeżenie: (0%)
-----


A czym się różni eventlistner od subscriber? Bo w niektórych przykładach dziedziczy się event a po niektórych EventListnerInterface
Go to the top of the page
+Quote Post
blahy
post 22.07.2015, 22:21:34
Post #6





Grupa: Zarejestrowani
Postów: 82
Pomógł: 22
Dołączył: 20.07.2010

Ostrzeżenie: (0%)
-----


Dla listenera w konfiguracji podaje sie jakich eventow slucha (w przypadku wystapienia wybranych eventow jakie metody wywolywac).
Subscriber ma metode getSubscribedEvents(), w ktorej sam informuje jakich eventow slucha (czyli sam decyduje na co bedzie reagowal).

Roznica jest dosc subtelna.

Caly komponent event dispatcher opisany jest tu: http://symfony.com/doc/current/components/...troduction.html
Odpowiedz stofa na takie pytanie jak Twoje: https://github.com/symfony/symfony-docs/issues/3987
A tu w dokumentacji tworzy sie wpis o tym jaka jest roznica (nieduza, jesli nie tworzysz zewnetrznego bundla/biblioteki): https://github.com/symfony/symfony-docs/pull/5377/files

+Events or Subscribers
+---------------------
+
+Listeners and subscribers can be used in the same application indistinctly. The
+decision to use either of them is usually a matter of personal taste. However,
+there are some minor advantages for each of them:
+
+* **Subscribers are easier to reuse** because the knowledge of the events is kept
+ in the class rather than in the service definition. This is the reason why
+ Symfony uses subscribers internally;
+* **Listeners are more flexible** because bundles can enable or disable each of
+ them conditionally depending on some configuration value.
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 26.06.2025 - 03:24