PHP में Event Bus और Observer पैटर्न
PHP में वेब एप्लिकेशन के आधुनिक विकास में, अक्सर विभिन्न घटनाओं पर लचीले ढंग से प्रतिक्रिया करने की आवश्यकता होती है: पंजीकरण के बाद ईमेल भेजना, उपयोगकर्ता क्रियाओं को लॉग करना, डेटा बदलने पर कैश अपडेट करना। इन उद्देश्यों के लिए, Observer पैटर्न और इसका विस्तार — Event Bus (ईवेंट बस) पूरी तरह से उपयुक्त हैं।
Observer पैटर्न: मूल बातें
Observer पैटर्न (प्रेक्षक) एक व्यवहारिक डिज़ाइन पैटर्न है जो एक सब्सक्रिप्शन तंत्र बनाता है, जो एक वस्तु को दूसरी वस्तुओं में होने वाली घटनाओं पर नज़र रखने और प्रतिक्रिया करने की अनुमति देता है। मुख्य घटक:
- Subject (विषय) — घटनाएँ उत्पन्न करने वाली वस्तु
- Observer (प्रेक्षक) — घटनाओं पर प्रतिक्रिया करने वाली वस्तु
- Event (घटना) — हुई क्रिया के बारे में जानकारी
Event Bus: Observer का विकास
Event Bus एक केंद्रीकृत बस है जो एप्लिकेशन में सभी घटनाओं का प्रबंधन करती है। क्लासिक Observer के विपरीत, जहाँ सब्सक्रिप्शन विषय से कड़ाई से जुड़ा होता है, Event Bus प्रदान करता है:
- प्रेषकों और प्राप्तकर्ताओं का कमजोर जुड़ाव
- रनटाइम के दौरान हैंडलर जोड़ने/हटाने की क्षमता
- एप्लिकेशन के किसी भी भाग से बस तक वैश्विक पहुँच
- सरलीकृत परीक्षण और डिबगिंग
PHP में Event Bus कार्यान्वयन का उदाहरण
आइए PSR-14 (Event Dispatcher) के साथ संगत एक सरल लेकिन कार्यात्मक Event Bus कार्यान्वयन बनाएँ।
<?php// इवेंट इंटरफ़ेसinterface EventInterface{ public function getName(): string;}
// बेस इवेंटclass UserRegisteredEvent implements EventInterface{ public function __construct( private string $email, private string $username ) {}
public function getName(): string { return 'user.registered'; }
public function getEmail(): string { return $this->email; }
public function getUsername(): string { return $this->username; }}
// हैंडलर इंटरफ़ेसinterface EventHandlerInterface{ public function handle(EventInterface $event): void;}
// Event Busclass EventBus{ private array $listeners = [];
public function addListener(string $eventName, EventHandlerInterface $handler): void { $this->listeners[$eventName][] = $handler; }
public function removeListener(string $eventName, EventHandlerInterface $handler): void { if (!isset($this->listeners[$eventName])) { return; }
$this->listeners[$eventName] = array_filter( $this->listeners[$eventName], fn($h) => $h !== $handler ); }
public function dispatch(EventInterface $event): void { $eventName = $event->getName();
if (!isset($this->listeners[$eventName])) { return; }
foreach ($this->listeners[$eventName] as $handler) { $handler->handle($event); } }}?>व्यावहारिक उपयोग
अब हैंडलर बनाते हैं और बस के काम को प्रदर्शित करते हैं:
<?php// ईमेल भेजने का हैंडलरclass SendWelcomeEmailHandler implements EventHandlerInterface{ public function handle(EventInterface $event): void { if (!$event instanceof UserRegisteredEvent) { return; }
echo "{$event->getEmail()} पर स्वागत ईमेल भेजा जा रहा है"; // mail($event->getEmail(), 'स्वागत है!', '...'); }}
// लॉगिंग हैंडलरclass LogUserRegistrationHandler implements EventHandlerInterface{ public function handle(EventInterface $event): void { if (!$event instanceof UserRegisteredEvent) { return; }