+ All Categories
Home > Technology > Kdyby/Events #posobota

Kdyby/Events #posobota

Date post: 02-Jul-2015
Category:
Upload: filip-prochazka
View: 249 times
Download: 5 times
Share this document with a friend
Description:
The Kdyby/Events extension provides a robust events system for the Nette Framework.
54
Kdyby/Events @ProchazkaFilip
Transcript
Page 1: Kdyby/Events #posobota

Kdyby/Events@ProchazkaFilip

Page 2: Kdyby/Events #posobota

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!

Page 3: Kdyby/Events #posobota

Teorie kolem eventů

Page 4: Kdyby/Events #posobota

Co jsou eventy/hooky?● modulárnost● rozšiřitelnost● znovupoužitelnost

Page 5: Kdyby/Events #posobota

Eventy v Nette?class Circle extends Nette\Object{ public $onChange = []; public function setRadius($radius)

{ $this->radius = max(0, $radius);

$this->onChange($this, $this->radius); }

Page 6: Kdyby/Events #posobota

Eventy v Nette?$circle = new Circle;

$circle->onChange[] =function($circle, $newValue) { echo 'there was a change!';};

$circle->setRadius(10);

Page 7: Kdyby/Events #posobota

Nette magicclass Nette\Object{

public function __call($name, $args){

foreach ($this->{"on$name"} as $cb){

call_user_func_array($cb, $args);}

}

Page 8: Kdyby/Events #posobota

Eventyv komponentách

Page 9: Kdyby/Events #posobota

Co je problém?class MyControl extends UI\Control {

public function handleFoo() {

// logic

$this->presenter->flashMessage('Yatta!');$this->redirect('Foo:');

}

}

Page 10: Kdyby/Events #posobota

Řešení?class MyControl extends UI\Control {

public $onSuccess = [];public function handleFoo() {

// logic

$this->onSuccess($this, $arg);}

}

Page 11: Kdyby/Events #posobota

Řešení?$control->onSuccess[] = function () {

$this->presenter->flashMessage('Yatta!');

$this->redirect('Foo:');

};

Page 12: Kdyby/Events #posobota

Kdyby/Events

Page 13: Kdyby/Events #posobota

Naco další event systém?● není to lazy● kompatibilita s otatními systémy

○ doctrine\orm○ symfony\event-dispatcher

Page 14: Kdyby/Events #posobota

Lazy eventy?$circle->onChange[] =

function($arg) use ($foo) { $foo->hardWork($arg);};

$circle->onChange[] =function($arg) use ($bar) { $bar->hardWork($arg);};

Page 15: Kdyby/Events #posobota

Doctrine ORM?class Listener implements EventSubscriber {

function getSubscribedEvents() {

return ['onFoo', 'onBar'];}

function onFoo($args) {// ...

}

Page 16: Kdyby/Events #posobota

Doctrine ORM?

$evm = new EventManager();$evm->addEventSubscriber(new Listener()

);

Page 17: Kdyby/Events #posobota

Doctrine ORM?

$evm->dispatch('onFoo',new Args($foo, $radius)

);

Page 18: Kdyby/Events #posobota

Symfony?class Listener

implements EventSubscriberInterface{

function getSubscribedEvents() {return ['onFoo', 'onBar'];

}

function onFoo($args) {// ...

}

Page 19: Kdyby/Events #posobota

Symfony?

$evd = new EventDispatcher();$evd->addSubscriber(new Listener()

);

Page 20: Kdyby/Events #posobota

Symfony?

$evm->dispatch('onFoo',new Event($foo, $radius)

);

Page 21: Kdyby/Events #posobota

Co takhle, Kdyby se všechny systémy daly používat dohromady?

Page 22: Kdyby/Events #posobota

xkcd.com/927

Page 23: Kdyby/Events #posobota

Kdyby/Events= Nette events (+ Doctrine EventManager)?(+ Symfony EventDispatcher)?

Page 24: Kdyby/Events #posobota

Pojďme vyřešit problém z praxe

class OrderProcess{function openOrder();function addItem($item);function finish(Order $order);

Page 25: Kdyby/Events #posobota

Nějaké ty basic závislosti

public function __construct(EntityManager $em,Nette\Security\User $user,Nette\Http\Session $session

){

Page 26: Kdyby/Events #posobota

Požadavek:“Po dokončení objednávky

se bude posílat email”

Page 27: Kdyby/Events #posobota

Přidáme posílání emailů...

public function __construct(EntityManager $em,Nette\Security\User $user,Nette\Http\Session $session,Nette\Mail\IMailer $mailer

){

Page 28: Kdyby/Events #posobota

Požadavek:“Přidej mi tam kredity za

objednávky”

Page 29: Kdyby/Events #posobota

Přidáme kredity..public function __construct(EntityManager $em,Nette\Security\User $user,Nette\Http\Session $session,Nette\Mail\IMailer $mailer,My\CreditsRewarder $rewarder

){

Page 30: Kdyby/Events #posobota

Požadavek:“Jeden partner chce

objednávky posílat do svého pokladního systému”

Page 31: Kdyby/Events #posobota

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){

Page 32: Kdyby/Events #posobota

Požadavek:“Budeme posílat smsky”

Page 33: Kdyby/Events #posobota

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

){

Page 34: Kdyby/Events #posobota

Požadavek:“V příštích 6 hodinách vracej

50% hodnoty objednávkyv kreditech, za všechny

objednané burgery”

Page 35: Kdyby/Events #posobota

SRP(Single Responsibility Principle)

Page 36: Kdyby/Events #posobota

Vraťme se na začátek...public function __construct(EntityManager $em,Nette\Security\User $user,Nette\Http\Session $session

){

Page 37: Kdyby/Events #posobota

… a přidejme si jeden eventclass OrderProcesspublic $onFinish = [];function finish(Order $order) {// ...$this->onFinish($this, $order);

}

Page 38: Kdyby/Events #posobota

… a napíšeme si listeneryclass OrderMailerListenerimplements Subscriber {function getSubscribedEvents() {return ['OrderProcess::onFinish'

];}

Page 39: Kdyby/Events #posobota

… a napíšeme si listeneryclass OrderMailerListener

function __construct(IMailer $mailer);

function onFinish(Order $order) {// ..$this->mailer->send($message);

}

Page 40: Kdyby/Events #posobota

… a napíšeme si listeneryclass CreditsRewardListener

function __construct(Rewarder $r, User $user);

function onFinish(Order $order) {$this->rewarder->reward(

$this->user->id,

$order->price * 0.05);

}

Page 41: Kdyby/Events #posobota

… a napíšeme si listeneryclass SmsSenderListener

function __construct(Sms\Sender $sender);

function onFinish(Order $order) {// ..

$this->sender->send($message);

}

Page 42: Kdyby/Events #posobota

Nezapomenout registrovatevents:subscribers:- OrderMailerListener- CreditsRewardListener- SmsSenderListener

Page 43: Kdyby/Events #posobota

Profit!

Page 44: Kdyby/Events #posobota

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?

Page 45: Kdyby/Events #posobota

A co nějaké nevýhody?● Nevíme vůbec co se zavolá● IDE s tím neumí pracovat

Page 46: Kdyby/Events #posobota

PhpStorm❤

Kdyby/Events

Page 47: Kdyby/Events #posobota
Page 48: Kdyby/Events #posobota
Page 49: Kdyby/Events #posobota
Page 50: Kdyby/Events #posobota

Go follow@juznacz

& @matej_21

Page 51: Kdyby/Events #posobota

A co ty message queue?Dáme demo?

Page 52: Kdyby/Events #posobota

Závěrem?Eventy nejsou silver bullet,

užívejte s rozumem.

Page 53: Kdyby/Events #posobota

Dotazy?

Page 54: Kdyby/Events #posobota

Díky za pozornost!filip-prochazka.com

Follow me maybe? @ProchazkaFilip


Recommended