הוקס ואירועים
סקירה כללית
Section titled “סקירה כללית”XOOPS מספקת הוקס ואירועים כנקודות הרחבה המאפשרות למודולים לקיים אינטראקציה עם פונקציונליות ליבה ואחד עם השני ללא תלות ישירה.
הוקס לעומת אירועים
Section titled “הוקס לעומת אירועים”| היבט | ווים | אירועים |
|---|---|---|
| מטרה | שנה behavior/data | להגיב להתרחשויות |
| חזור | יכול להחזיר נתונים שהשתנו | בדרך כלל בטל |
| תזמון | Before/during פעולה | לאחר פעולה |
| דפוס | שרשרת סינון | Observer/pub-sub |
מערכת הוק
Section titled “מערכת הוק”רישום הוקס
Section titled “רישום הוקס”// Register a hook in xoops_version.php$modversion['hooks'][] = [ 'name' => 'user.profile.display', 'callback' => 'mymodule_hook_user_profile', 'priority' => 10,];התקשר להתקשרות חזרה
Section titled “התקשר להתקשרות חזרה”function mymodule_hook_user_profile(array $data): array{ $userId = $data['user_id'];
// Add custom profile fields $data['fields']['reputation'] = mymodule_get_user_reputation($userId); $data['fields']['badges'] = mymodule_get_user_badges($userId);
return $data;}ווי ליבה זמינים
Section titled “ווי ליבה זמינים”| שם הוק | נתונים | תיאור |
|---|---|---|
user.profile.display | מערך נתוני משתמש | שנה תצוגת פרופיל |
content.render | תוכן HTML | סינון פלט תוכן |
form.submit | נתוני טופס | Validate/modify נתוני טופס |
search.results | מערך תוצאות | סנן תוצאות חיפוש |
menu.main | פריטי תפריט | שנה תפריט ראשי |
מערכת אירועים
Section titled “מערכת אירועים”שיגור אירועים
Section titled “שיגור אירועים”// In your module code$eventHandler = xoops_getHandler('event');
$eventHandler->trigger('mymodule.article.created', [ 'article_id' => $article->id(), 'author_id' => $article->authorId(), 'title' => $article->title(),]);האזנה לאירועים
Section titled “האזנה לאירועים”class MyModulePreload extends \Xmf\Module\Helper\AbstractHelper{ public function eventMymoduleArticleCreated(array $args): void { $articleId = $args['article_id'];
// Notify subscribers $this->notifyNewArticle($articleId); }
public function eventUserLogin(array $args): void { $userId = $args['userid'];
// Update last login for module $this->updateUserActivity($userId); }}טען מראש הפניה לאירועים
Section titled “טען מראש הפניה לאירועים”אירועי ליבה
Section titled “אירועי ליבה”// Header/Footerpublic function eventCoreHeaderStart(array $args): void {}public function eventCoreHeaderEnd(array $args): void {}public function eventCoreFooterStart(array $args): void {}public function eventCoreFooterEnd(array $args): void {}
// Includespublic function eventCoreIncludeCommonStart(array $args): void {}public function eventCoreIncludeCommonEnd(array $args): void {}
// Exceptionspublic function eventCoreException(array $args): void {}אירועי משתמש
Section titled “אירועי משתמש”public function eventUserLogin(array $args): void {}public function eventUserLogout(array $args): void {}public function eventUserRegister(array $args): void {}public function eventUserActivate(array $args): void {}public function eventUserDelete(array $args): void {}אירועי מודול
Section titled “אירועי מודול”public function eventSystemModuleInstall(array $args): void {}public function eventSystemModuleUninstall(array $args): void {}public function eventSystemModuleUpdate(array $args): void {}public function eventSystemModuleActivate(array $args): void {}public function eventSystemModuleDeactivate(array $args): void {}אירועי מודול מותאם אישית
Section titled “אירועי מודול מותאם אישית”הגדרת אירועים
Section titled “הגדרת אירועים”// Define event constantsclass ArticleEvents{ public const CREATED = 'mymodule.article.created'; public const UPDATED = 'mymodule.article.updated'; public const DELETED = 'mymodule.article.deleted'; public const PUBLISHED = 'mymodule.article.published';}מפעילים אירועים
Section titled “מפעילים אירועים”class ArticleService{ public function publish(Article $article): void { $article->publish(); $this->repository->save($article);
// Trigger event $GLOBALS['xoopsPreload']->triggerEvent( ArticleEvents::PUBLISHED, ['article' => $article] ); }}האזנה לאירועי מודול
Section titled “האזנה לאירועי מודול”// In another module's Preload.php
public function eventMymoduleArticlePublished(array $args): void{ $article = $args['article'];
// Index for search $this->searchIndexer->index($article);
// Update sitemap $this->sitemapGenerator->addUrl($article->url());}שיטות עבודה מומלצות
Section titled “שיטות עבודה מומלצות”- השתמש בשמות ספציפיים - פורמט
module.entity.action - העברת נתונים מינימליים - רק מה שמאזינים צריכים
- אירועי מסמך - רשימת אירועים במסמכי מודול
- הימנע מתופעות לוואי - שמור על מיקוד המאזינים
- טפל בשגיאות - אל תיתן לשגיאות מאזינים לשבור את הזרימה
תיעוד קשור
Section titled “תיעוד קשור”- Event-System - תיעוד אירועים מפורט
- ../03-Module-Development/Module-Development - פיתוח מודול
- ../07-XOOPS-4.0/Implementation-Guides/Event-System-Guide - PSR-14 אירועים