Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up for free
Kdyby/Events #posobota
Filip Procházka
September 30, 2014
Technology
0
26
Kdyby/Events #posobota
The Kdyby/Events extension provides a robust events system for the Nette Framework.
Filip Procházka
September 30, 2014
Tweet
Share
More Decks by Filip Procházka
See All by Filip Procházka
Evolving architecture
fprochazka
0
52
Value objects in PHP
fprochazka
1
130
Jak psát testy na REST API
fprochazka
0
39
V gitu se nic neztratí
fprochazka
0
180
Co se PHP programátor může naučit od Javy?
fprochazka
1
52
4 roky remote
fprochazka
1
39
Před čím tě Nette ani Symfony neochrání
fprochazka
0
85
Nejlepší cache je žádná cache
fprochazka
0
53
Doctrine: co dělat, když entity nestačí
fprochazka
0
30
Other Decks in Technology
See All in Technology
- Rでオブジェクト指向プログラミング- クラス設計入門の入門
kotatyamtema
1
700
グローバルチームことはじめ / Bootstrapping a global team
tasshi
1
630
ECテックカンファレンス2023 EC事業部のモバイル開発2023
tatsumi0000
0
180
データベースの発表には RDBMS 以外もありますよ
maroon1st
0
230
2年で10→70人へ! スタートアップの 情報セキュリティ課題と施策
miekobayashi
1
200
20230121_BuriKaigi
oyakata2438
0
170
400種類のWeb APIをサポートしているデータパイプラインツールにおけるWeb APIとの共存戦略
cdataj
0
150
立ち止まっても、寄り道しても / even if I stop, even if I take a detour
katoaz
0
150
OCIコンテナサービス関連の技術詳細 /oke-ocir-details
oracle4engineer
PRO
0
750
もし本番ネットワークをまるごと仮想環境に”コピー”できたらうれしいですか? / janog51
corestate55
0
150
チケットNFTの仕組み
sbtechnight
0
330
SmartHRからOktaへのSCIM連携で作り出すHRドリブンのアカウント管理
jousysmiler
1
110
Featured
See All Featured
Clear Off the Table
cherdarchuk
79
290k
Art Directing for the Web. Five minutes with CSS Template Areas
malarkey
196
9.8k
No one is an island. Learnings from fostering a developers community.
thoeni
12
1.5k
Imperfection Machines: The Place of Print at Facebook
scottboms
254
12k
Fireside Chat
paigeccino
16
1.8k
Designing on Purpose - Digital PM Summit 2013
jponch
108
5.9k
The MySQL Ecosystem @ GitHub 2015
samlambert
240
11k
Pencils Down: Stop Designing & Start Developing
hursman
114
10k
Producing Creativity
orderedlist
PRO
335
37k
Automating Front-end Workflow
addyosmani
1351
200k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
109
16k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
31
20k
Transcript
Kdyby/Events @ProchazkaFilip
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!
Teorie kolem eventů
Co jsou eventy/hooky? • modulárnost • rozšiřitelnost • znovupoužitelnost
Eventy v Nette? class Circle extends Nette\Object { public $onChange
= []; public function setRadius($radius) { $this->radius = max(0, $radius); $this->onChange($this, $this->radius); }
Eventy v Nette? $circle = new Circle; $circle->onChange[] = function($circle,
$newValue) { echo 'there was a change!'; }; $circle->setRadius(10);
Nette magic class Nette\Object { public function __call($name, $args) {
foreach ($this->{"on$name"} as $cb) { call_user_func_array($cb, $args); } }
Eventy v komponentách
Co je problém? class MyControl extends UI\Control { public function
handleFoo() { // logic $this->presenter->flashMessage('Yatta!'); $this->redirect('Foo:'); } }
Řešení? class MyControl extends UI\Control { public $onSuccess = [];
public function handleFoo() { // logic $this->onSuccess($this, $arg); } }
Řešení? $control->onSuccess[] = function () { $this->presenter->flashMessage('Yatta!'); $this->redirect('Foo:'); };
Kdyby/Events
Naco další event systém? • není to lazy • kompatibilita
s otatními systémy ◦ doctrine\orm ◦ symfony\event-dispatcher
Lazy eventy? $circle->onChange[] = function($arg) use ($foo) { $foo->hardWork($arg); };
$circle->onChange[] = function($arg) use ($bar) { $bar->hardWork($arg); };
Doctrine ORM? class Listener implements EventSubscriber { function getSubscribedEvents() {
return ['onFoo', 'onBar']; } function onFoo($args) { // ... }
Doctrine ORM? $evm = new EventManager(); $evm->addEventSubscriber( new Listener() );
Doctrine ORM? $evm->dispatch( 'onFoo', new Args($foo, $radius) );
Symfony? class Listener implements EventSubscriberInterface { function getSubscribedEvents() { return
['onFoo', 'onBar']; } function onFoo($args) { // ... }
Symfony? $evd = new EventDispatcher(); $evd->addSubscriber( new Listener() );
Symfony? $evm->dispatch( 'onFoo', new Event($foo, $radius) );
Co takhle, Kdyby se všechny systémy daly používat dohromady?
xkcd.com/927
Kdyby/Events = Nette events (+ Doctrine EventManager)? (+ Symfony EventDispatcher)?
Pojďme vyřešit problém z praxe class OrderProcess { function openOrder();
function addItem($item); function finish(Order $order);
Nějaké ty basic závislosti public function __construct( EntityManager $em, Nette\Security\User
$user, Nette\Http\Session $session ){
Požadavek: “Po dokončení objednávky se bude posílat email”
Přidáme posílání emailů... public function __construct( EntityManager $em, Nette\Security\User $user,
Nette\Http\Session $session, Nette\Mail\IMailer $mailer ){
Požadavek: “Přidej mi tam kredity za objednávky”
Přidáme kredity.. public function __construct( EntityManager $em, Nette\Security\User $user, Nette\Http\Session
$session, Nette\Mail\IMailer $mailer, My\CreditsRewarder $rewarder ){
Požadavek: “Jeden partner chce objednávky posílat do svého pokladního systému”
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 ){
Požadavek: “Budeme posílat smsky”
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 ){
Požadavek: “V příštích 6 hodinách vracej 50% hodnoty objednávky v
kreditech, za všechny objednané burgery”
SRP (Single Responsibility Principle)
Vraťme se na začátek... public function __construct( EntityManager $em, Nette\Security\User
$user, Nette\Http\Session $session ){
… a přidejme si jeden event class OrderProcess public $onFinish
= []; function finish(Order $order) { // ... $this->onFinish($this, $order); }
… a napíšeme si listenery class OrderMailerListener implements Subscriber {
function getSubscribedEvents() { return [ 'OrderProcess::onFinish' ]; }
… a napíšeme si listenery class OrderMailerListener function __construct(IMailer $mailer);
function onFinish(Order $order) { // .. $this->mailer->send($message); }
… 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); }
… a napíšeme si listenery class SmsSenderListener function __construct(Sms\Sender $sender);
function onFinish(Order $order) { // .. $this->sender->send($message); }
Nezapomenout registrovat events: subscribers: - OrderMailerListener - CreditsRewardListener - SmsSenderListener
Profit!
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?
A co nějaké nevýhody? • Nevíme vůbec co se zavolá
• IDE s tím neumí pracovat
PhpStorm ❤ Kdyby/Events
None
None
None
Go follow @juznacz & @matej_21
A co ty message queue? Dáme demo?
Závěrem? Eventy nejsou silver bullet, užívejte s rozumem.
Dotazy?
Díky za pozornost! filip-prochazka.com Follow me maybe? @ProchazkaFilip