Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Kdyby/Events #posobota
Search
Filip Procházka
September 30, 2014
Technology
0
36
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
81
Value objects in PHP
fprochazka
1
140
Jak psát testy na REST API
fprochazka
0
49
V gitu se nic neztratí
fprochazka
0
260
Co se PHP programátor může naučit od Javy?
fprochazka
1
62
4 roky remote
fprochazka
1
62
Před čím tě Nette ani Symfony neochrání
fprochazka
0
120
Nejlepší cache je žádná cache
fprochazka
0
81
Doctrine: co dělat, když entity nestačí
fprochazka
0
40
Other Decks in Technology
See All in Technology
PHP"オレ"カンファレンスの告知
ysknsid25
0
290
なぜ NOT A HOTEL が Web3 に取り組むのか - NOT A HOTEL TECH TALK
ynunokawa
0
160
4年前、あるじゃん老害エンジニアLT合戦に登壇、米国西海岸コンピュータ歴史博物館体験記の続編
toshi_atsumi
0
180
ユーザーストーリーのレビューを自動化したみたの
bun913
1
270
[PlatformCon 24] Platform Orchestrators: The Missing Middle of Internal Developer Platforms?
danielbryantuk
0
160
転移学習とドメイン適応の基礎
kmatsui
2
570
AIQ株式会社 エンジニア向け会社紹介資料
aiqlab
0
350
社内勉強会運営のコツ
senoo
6
1.1k
SPI原点回帰論:事業課題とFour Keysの結節点を見出す実践的ソフトウェアプロセス改善 / DevOpsDays Tokyo 2024
visional_engineering_and_design
4
1.2k
疲弊しない!AWSセキュリティ統制の考え方 #devio_osakaday1
masahirokawahara
6
5.8k
メールセキュリティの DMARC周りの設定を少しずつ設定してみたお話
miu_crescent
1
260
「ふりかえりのふりかえり」をふりかえり、実のあるふりかえりにする
naitosatoshi
0
210
Featured
See All Featured
What’s in a name? Adding method to the madness
productmarketing
PRO
14
2.6k
Designing on Purpose - Digital PM Summit 2013
jponch
110
6.4k
Rebuilding a faster, lazier Slack
samanthasiow
72
8.2k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
29
6k
Keith and Marios Guide to Fast Websites
keithpitt
408
22k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
124
32k
Building an army of robots
kneath
300
41k
Fireside Chat
paigeccino
19
2.6k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
13
1.5k
No one is an island. Learnings from fostering a developers community.
thoeni
14
2.1k
A better future with KSS
kneath
230
16k
Statistics for Hackers
jakevdp
789
220k
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