$30 off During Our Annual Pro Sale. View Details »

Kdyby/Events #posobota

Kdyby/Events #posobota

The Kdyby/Events extension provides a robust events system for the Nette Framework.

Filip Procházka

September 30, 2014
Tweet

More Decks by Filip Procházka

Other Decks in Technology

Transcript

  1. Co si povíme? • teorie kolem eventů • jak to

    funguje v Nette • eventy v komponentách • Kdyby/Events prakticky • diskuze? připravte si otázky!
  2. Eventy v Nette? class Circle extends Nette\Object { public $onChange

    = []; public function setRadius($radius) { $this->radius = max(0, $radius); $this->onChange($this, $this->radius); }
  3. Eventy v Nette? $circle = new Circle; $circle->onChange[] = function($circle,

    $newValue) { echo 'there was a change!'; }; $circle->setRadius(10);
  4. Nette magic class Nette\Object { public function __call($name, $args) {

    foreach ($this->{"on$name"} as $cb) { call_user_func_array($cb, $args); } }
  5. Co je problém? class MyControl extends UI\Control { public function

    handleFoo() { // logic $this->presenter->flashMessage('Yatta!'); $this->redirect('Foo:'); } }
  6. Řešení? class MyControl extends UI\Control { public $onSuccess = [];

    public function handleFoo() { // logic $this->onSuccess($this, $arg); } }
  7. Naco další event systém? • není to lazy • kompatibilita

    s otatními systémy ◦ doctrine\orm ◦ symfony\event-dispatcher
  8. Lazy eventy? $circle->onChange[] = function($arg) use ($foo) { $foo->hardWork($arg); };

    $circle->onChange[] = function($arg) use ($bar) { $bar->hardWork($arg); };
  9. Doctrine ORM? class Listener implements EventSubscriber { function getSubscribedEvents() {

    return ['onFoo', 'onBar']; } function onFoo($args) { // ... }
  10. Pojďme vyřešit problém z praxe class OrderProcess { function openOrder();

    function addItem($item); function finish(Order $order);
  11. Externí pokladní systém.. public function __construct( EntityManager $em, Nette\Security\User $user,

    Nette\Http\Session $session, Nette\Mail\IMailer $mailer, My\CreditsRewarder $rewarder, Partner\CashRegisterClient $partner ){
  12. Posílání smsek... public function __construct( EntityManager $em, Nette\Security\User $user, Nette\Http\Session

    $session, Nette\Mail\IMailer $mailer, My\CreditsRewarder $rewarder, Partner\CashRegisterClient $partner, My\Sms\Sender $smsSender ){
  13. … a přidejme si jeden event class OrderProcess public $onFinish

    = []; function finish(Order $order) { // ... $this->onFinish($this, $order); }
  14. … a napíšeme si listenery class OrderMailerListener implements Subscriber {

    function getSubscribedEvents() { return [ 'OrderProcess::onFinish' ]; }
  15. … a napíšeme si listenery class OrderMailerListener function __construct(IMailer $mailer);

    function onFinish(Order $order) { // .. $this->mailer->send($message); }
  16. … a napíšeme si listenery class CreditsRewardListener function __construct( Rewarder

    $r, User $user); function onFinish(Order $order) { $this->rewarder->reward( $this->user->id, $order->price * 0.05); }
  17. … a napíšeme si listenery class SmsSenderListener function __construct(Sms\Sender $sender);

    function onFinish(Order $order) { // .. $this->sender->send($message); }
  18. Poslední nejasnosti • Jak se $onFinish dostane k listenerům? •

    Nemělo to být lazy? • Můžu naslouchat na více událostí v jednom listeneru?