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
170
Value objects in PHP
fprochazka
1
180
Jak psát testy na REST API
fprochazka
0
90
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
110
Doctrine: co dělat, když entity nestačí
fprochazka
0
55
Other Decks in Technology
See All in Technology
本部長の代わりに提案書レビュー! KDDI営業が毎日使うAIエージェント「A-BOSS」開発秘話
minorun365
PRO
14
2.2k
Amazon Bedrockで実現する 新たな学習体験
kzkmaeda
1
340
doda開発 生成AI元年宣言!自家製AIエージェントから始める生産性改革 / doda Development Declaration of the First Year of Generated AI! Productivity Reforms Starting with Home-grown AI Agents
techtekt
0
190
(非公式) AWS Summit Japan と 海浜幕張 の歩き方 2025年版
coosuke
PRO
1
320
新規プロダクト開発、AIでどう変わった? #デザインエンジニアMeetup
bengo4com
0
500
Amazon ECS & AWS Fargate 運用アーキテクチャ2025 / Amazon ECS and AWS Fargate Ops Architecture 2025
iselegant
13
4.1k
ユーザーのプロフィールデータを活用した推薦精度向上の取り組み
yudai00
0
460
実践! AIエージェント導入記
1mono2prod
0
130
原則から考える保守しやすいComposable関数設計
moriatsushi
3
500
OTFSG勉強会 / Introduction to the History of Delta Lake + Iceberg
databricksjapan
0
120
Oracle Audit Vault and Database Firewall 20 概要
oracle4engineer
PRO
2
1.6k
白金鉱業Meetup_Vol.19_PoCはデモで語れ!顧客の本音とインサイトを引き出すソリューション構築
brainpadpr
2
460
Featured
See All Featured
Fashionably flexible responsive web design (full day workshop)
malarkey
407
66k
4 Signs Your Business is Dying
shpigford
184
22k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
32
2.3k
A Tale of Four Properties
chriscoyier
159
23k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
32
5.9k
Designing for humans not robots
tammielis
253
25k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
35
2.3k
Facilitating Awesome Meetings
lara
54
6.4k
Building Adaptive Systems
keathley
43
2.6k
Speed Design
sergeychernyshev
31
1k
Building an army of robots
kneath
306
45k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
8
660
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