XOOPS מערכת אירועים
2.5.x: טעינות מוקדמות 4.0.x: PSR-14
:::הערה[שתי מערכות אירועים ב-XOOPS]
| מערכת | גרסה | מקרה שימוש |
|---|---|---|
| מערכת טעינה מראש | ✅ XOOPS 2.5.x (נוכחי) | היכנס לאירועי ליבה באמצעות class/Preload.php |
| PSR-14 סדרן אירועים | 🚧 XOOPS 4.0 (עתיד) | שיגור אירועים מודרני עם אירועים מודפסים |
עבור XOOPS 2.5.x מודולים, השתמש בסעיף טעינה מראש למטה. החלק PSR-14 מיועד לפיתוח XOOPS 4.0. :::
סקירה כללית
Section titled “סקירה כללית”מערכת האירועים XOOPS מאפשרת צימוד רופף בין מודולים באמצעות דפוס צופה. רכיבים יכולים לפלוט אירועים שחלקים אחרים של המערכת יכולים להאזין להם ולהגיב אליהם.
סוגי אירועים
Section titled “סוגי אירועים”אירועי ליבה
Section titled “אירועי ליבה”| אירוע | נקודת טריגר |
|---|---|
core.header.start | לפני עיבוד כותרות |
core.header.end | לאחר עיבוד כותרת |
core.footer.start | לפני עיבוד כותרת תחתונה |
core.footer.end | לאחר עיבוד כותרת תחתונה |
core.exception | כאשר מתרחש חריג |
אירועי מחזור חיים של מודול
Section titled “אירועי מחזור חיים של מודול”| אירוע | נקודת טריגר |
|---|---|
module.install | לאחר התקנת מודול |
module.update | לאחר עדכון המודול |
module.uninstall | לפני הסרת מודול |
module.activate | כאשר מודול מופעל |
module.deactivate | כאשר מודול מושבת |
אירועי משתמש
Section titled “אירועי משתמש”| אירוע | נקודת טריגר |
|---|---|
user.login | לאחר כניסה מוצלחת |
user.logout | לאחר התנתקות |
user.register | לאחר ההרשמה |
user.delete | לפני מחיקת משתמש |
מערכת טעינה מראש (מדור קודם)
Section titled “מערכת טעינה מראש (מדור קודם)”יצירת טעינה מוקדמת
Section titled “יצירת טעינה מוקדמת”<?phpnamespace XoopsModules\MyModule;
use Xmf\Module\Helper\AbstractHelper;
final class Preload extends AbstractHelper{ public function eventCoreHeaderStart(array $args): void { // Runs on every page before header }
public function eventCoreFooterStart(array $args): void { // Runs before footer renders }
public function eventUserLogin(array $args): void { $userId = $args['userid']; // Handle login event }
public function eventCoreException(array $args): void { $exception = $args['exception']; // Log or handle exception }}שם שיטת אירוע
Section titled “שם שיטת אירוע”event{Category}{Action}
Examples:- eventCoreHeaderStart- eventUserLogin- eventModuleNewsArticleCreatePSR-14 משדר אירועים (XOOPS 4.0)
Section titled “PSR-14 משדר אירועים (XOOPS 4.0)”שיעור אירועים
Section titled “שיעור אירועים”<?php
declare(strict_types=1);
namespace XoopsModules\MyModule\Event;
final class ArticleCreatedEvent{ public function __construct( public readonly int $articleId, public readonly int $authorId, public readonly string $title, public readonly \DateTimeImmutable $createdAt ) {}}שיגור אירועים
Section titled “שיגור אירועים”use Psr\EventDispatcher\EventDispatcherInterface;
final class ArticleService{ public function __construct( private readonly ArticleRepository $repository, private readonly EventDispatcherInterface $dispatcher ) {}
public function create(CreateArticleDTO $dto): Article { $article = Article::create($dto); $this->repository->save($article);
// Dispatch event $this->dispatcher->dispatch(new ArticleCreatedEvent( articleId: $article->getId(), authorId: $article->getAuthorId(), title: $article->getTitle(), createdAt: new \DateTimeImmutable() ));
return $article; }}מאזין אירועים
Section titled “מאזין אירועים”<?php
declare(strict_types=1);
namespace XoopsModules\MyModule\Listener;
use XoopsModules\MyModule\Event\ArticleCreatedEvent;
final class SendNotificationOnArticleCreated{ public function __construct( private readonly NotificationService $notifications ) {}
public function __invoke(ArticleCreatedEvent $event): void { $this->notifications->notifySubscribers( 'new_article', [ 'article_id' => $event->articleId, 'title' => $event->title, ] ); }}רישום מאזינים
Section titled “רישום מאזינים”return [ ArticleCreatedEvent::class => [ SendNotificationOnArticleCreated::class, UpdateSearchIndex::class, ClearArticleCache::class, ],
ArticleUpdatedEvent::class => [ UpdateSearchIndex::class, ClearArticleCache::class, ],
ArticleDeletedEvent::class => [ RemoveFromSearchIndex::class, ClearArticleCache::class, ],];אירועים שניתנים לעצירה
Section titled “אירועים שניתנים לעצירה”use Psr\EventDispatcher\StoppableEventInterface;
final class ArticlePublishingEvent implements StoppableEventInterface{ private bool $propagationStopped = false; private ?string $rejectionReason = null;
public function __construct( public readonly Article $article ) {}
public function isPropagationStopped(): bool { return $this->propagationStopped; }
public function reject(string $reason): void { $this->propagationStopped = true; $this->rejectionReason = $reason; }
public function getRejectionReason(): ?string { return $this->rejectionReason; }}
// Listener can stop propagationfinal class ContentModerationListener{ public function __invoke(ArticlePublishingEvent $event): void { if ($this->containsProhibitedContent($event->article)) { $event->reject('Content violates community guidelines'); } }}שיטות עבודה מומלצות
Section titled “שיטות עבודה מומלצות”- אירועים בלתי ניתנים לשינוי - אירועים צריכים להיות לקריאה בלבד
- אירועים ספציפיים - צור אירועים ספציפיים, לא כלליים
- אסינכרון כשאפשר - השתמש בתורים לפעולות איטיות
- ללא תופעות לוואי במשלוח - המשלוח צריך להיות מהיר
- אירועי מסמך - רשימת אירועים זמינים עבור משתמשי מודול
תיעוד קשור
Section titled “תיעוד קשור”- פיתוח מודול - פיתוח מודול
- Event-System-Guide - מדריך PSR-14
- Hooks-Events - הוקס מדור קודם
- Events-and-Hooks - דוגמאות לאירועים