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
49
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
180
Value objects in PHP
fprochazka
1
190
Jak psát testy na REST API
fprochazka
0
93
V gitu se nic neztratí
fprochazka
0
370
Co se PHP programátor může naučit od Javy?
fprochazka
1
110
4 roky remote
fprochazka
1
79
Před čím tě Nette ani Symfony neochrání
fprochazka
0
150
Nejlepší cache je žádná cache
fprochazka
0
120
Doctrine: co dělat, když entity nestačí
fprochazka
0
58
Other Decks in Technology
See All in Technology
新規事業におけるAIリサーチの活用例
ranxxx
0
140
複数のGemini CLIが同時開発する狂気 - Jujutsuが実現するAIエージェント協調の新世界
gunta
12
3.2k
Talk to Someone At Delta Airlines™️ USA Contact Numbers
travelcarecenter
0
170
2025-07-25 NOT A HOTEL TECH TALK ━ スマートホーム開発の最前線 ━ SOFTWARE
wakinchan
0
130
そもそも AWS FIS について。なぜ今 FIS のハンズオンなのか?などなど
kazzpapa3
2
120
Step Functions First - サーバーレスアーキテクチャの新しいパラダイム
taikis
1
280
東京海上日動におけるセキュアな開発プロセスの取り組み
miyabit
0
130
P2P通信の標準化 WebRTCを知ろう
faithandbrave
6
2.3k
AI工学特論: MLOps・継続的評価
asei
10
1.6k
PHPからはじめるコンピュータアーキテクチャ / From Scripts to Silicon: A Journey Through the Layers of Computing
tomzoh
2
380
DatabricksのOLTPデータベース『Lakebase』に詳しくなろう!
inoutk
0
110
スプリントゴール未達症候群に送る処方箋
kakehashi
PRO
1
190
Featured
See All Featured
Imperfection Machines: The Place of Print at Facebook
scottboms
267
13k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.4k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
26k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
47
9.6k
How to train your dragon (web standard)
notwaldorf
96
6.1k
Building an army of robots
kneath
306
45k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
16k
Stop Working from a Prison Cell
hatefulcrawdad
271
21k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
8
850
ReactJS: Keep Simple. Everything can be a component!
pedronauck
667
120k
Designing for humans not robots
tammielis
253
25k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
53k
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