Tovább a tartalomhoz

XOOPS eseményrendszer

2.5.x: Előtöltések 4.0.x: PSR-14

:::megjegyzés[Két eseményrendszer a XOOPS-ban]

RendszerVerzióHasználati eset
Előfeszítő rendszer✅ XOOPS 2.5.x (aktuális)Csatlakozzon az alapvető eseményekhez a class/Preload.php
PSR-14 Esemény diszpécser🚧 XOOPS 4.0 (jövő)Modern rendezvényküldés gépelt eseményekkel

A XOOPS 2.5.x modulokhoz használja az alábbi Preload System részt. A PSR-14 rész a XOOPS 4.0 fejlesztéshez való. :::

A XOOPS eseményrendszer lehetővé teszi a modulok közötti laza csatolást egy megfigyelő mintán keresztül. Az összetevők olyan eseményeket bocsáthatnak ki, amelyeket a rendszer többi része meghallgathat és reagálhat rájuk.

EseményTrigger Point
core.header.startA fejléc feldolgozása előtt
core.header.endA fejléc feldolgozása után
core.footer.startA lábléc megjelenítése előtt
core.footer.endA lábléc megjelenítése után
core.exceptionKivétel esetén
EseményTrigger Point
module.installmodul telepítése után
module.updatemodulfrissítés után
module.uninstallA modul eltávolítása előtt
module.activateAmikor a modul aktiválva van
module.deactivateAmikor a modul deaktivált
EseményTrigger Point
user.loginSikeres bejelentkezés után
user.logoutKijelentkezés után
user.registerRegisztráció után
user.deleteA felhasználó törlése előtt
class/Preload.php
<?php
namespace 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
}
}
event{Category}{Action}
Examples:
- eventCoreHeaderStart
- eventUserLogin
- eventModuleNewsArticleCreate
<?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
) {}
}
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;
}
}
<?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,
]
);
}
}
config/events.php
return [
ArticleCreatedEvent::class => [
SendNotificationOnArticleCreated::class,
UpdateSearchIndex::class,
ClearArticleCache::class,
],
ArticleUpdatedEvent::class => [
UpdateSearchIndex::class,
ClearArticleCache::class,
],
ArticleDeletedEvent::class => [
RemoveFromSearchIndex::class,
ClearArticleCache::class,
],
];
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 propagation
final class ContentModerationListener
{
public function __invoke(ArticlePublishingEvent $event): void
{
if ($this->containsProhibitedContent($event->article)) {
$event->reject('Content violates community guidelines');
}
}
}
  1. Megváltozhatatlan események – Az eseményeknek csak olvashatónak kell lenniük
  2. Speciális események – Konkrét eseményeket hozhat létre, nem általános eseményeket
  3. Aszinkronizálás, amikor lehetséges – Használjon sorokat a lassú műveletekhez
  4. Nincsenek mellékhatások a kiszállításban – A kiszállításnak gyorsnak kell lennie
  5. Dokumentum események - A modul felhasználói számára elérhető események listája