Skip to content

Conversation

@joshtrichards
Copy link
Member

@joshtrichards joshtrichards commented Dec 29, 2025

☑️ Resolves

The existing Events chapter was showing its age and in need of a refactor.

It was a bit confusing in some spots, wasn't up-to-date with current best practices, and lacked coverage of the dispatcher.

This PR modernizes and expands the documentation for Nextcloud Events:

  • adds emission/dispatch coverage
  • clarifies listener registration / instantiation / handler execution lifecycle
  • aligns docs/examples with current best practices - e.g.,
    • use the OCP Event Dispatcher and IBootstrap patterns
    • current PHP best practices
  • adds additional code examples:
    • e.g., defining events, registering listeners, and emitting/dispatching events, usage of event payloads, and DI
  • improves coverage of event payloads
  • de-emphasize legacy approaches, referencing them only for migration purposes
    • e.g., hooks, public emitters, pre-IBootstrap listener patterns
  • improves clarity throughout the documentation
    • e.g., correcting typos, clarifying ambiguous statements, and removing (most - ahem) informal phrasing
  • new table of contents

These changes ensure app authors have clear, up-to-date, and actionable guidance for both consuming and producing events in Nextcloud apps.

🖼️ Screenshots

_home_josh_workspace_nextcloud_documentation_developer_manual__build_html_com_basics_events html

…examples, clarify listener lifecycle

Expanded the events documentation w/ detailed explanations, examples, and deprecated hooks and emitters sections.

Signed-off-by: Josh <[email protected]>
Signed-off-by: Josh <[email protected]>
@joshtrichards joshtrichards changed the title docs(dev/events): modernize event system docs, add emission/dispatch examples, clarify listener lifecycle docs(dev/events): modernize / expand event system docs Dec 29, 2025
@joshtrichards joshtrichards added this to the Nextcloud 33 milestone Dec 29, 2025
@joshtrichards joshtrichards marked this pull request as ready for review December 29, 2025 14:09
@susnux susnux requested a review from come-nc January 5, 2026 21:13
Comment on lines +143 to +144
*/
class AddTwoListener implements IEventListener {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
*/
class AddTwoListener implements IEventListener {
* @template-implements IEventListener<AddEvent>
*/
class AddTwoListener implements IEventListener {

Comment on lines +163 to +164
By default there is no persistent "listener object" kept by the dispatcher. Each time the event is fired, the DI container will lazily instantiate a new instance of the listener (the class), invoke the handle() method, and then discard the instance. There are more advanced approaches to listener registration (singleton/shared), but
that is a more advanced use case -- not the default for DI-registered event listeners in Nextcloud. You may find this approach in core, but rarely in apps.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that true? I would expect core to use DI to get the listener, so as a singleton by default. Meaning if 2 instance of the same events are fired in the same request the same instance of the listener will be used.

/**
* Listener that uses MyService (external dependency) when an AddEvent is fired.
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

template-implements here as well

/**
* Listener that logs the creation of a user when UserCreatedEvent is fired.
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

template-implements

// Logger is injected by the DI container
public function __construct(
private LoggerInterface $logger
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private LoggerInterface $logger
private LoggerInterface $logger,

The codestyle currently used forces a comma here.

// Emit an event so other apps can react
$event = new UserCreatedEvent($user);
$this->dispatcher->dispatch($event);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$this->dispatcher->dispatch($event);
$this->dispatcher->dispatchTyped($event);

dispatch is deprecated and has another signature

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants