aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/src/event_dispatcher/event_subscriber_interface.rs
blob: 0a5d88d51862341dcc6b72b697c832812cdb75db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//! ref: composer/src/Composer/EventDispatcher/EventSubscriberInterface.php

use indexmap::IndexMap;
use shirabe_php_rpc::PhpObjHandle;

/// Represents one event's subscriber info: method name only, method+priority, or multiple handlers.
#[derive(Debug)]
pub enum SubscribedEventEntry {
    Method(String),
    MethodWithPriority(String, Option<i64>),
    Methods(Vec<(String, Option<i64>)>),
}

// The sole implementor is the PHP plugin proxy (plugins are the only subscribers in Composer
// itself), so the trait deviates from the PHP shape in two deliberate ways: the PHP-side static
// `getSubscribedEvents()` takes `&self` here (the receiver carries which PHP class to call, and
// an associated function would not be dyn-compatible), and it is fallible because the answer
// crosses the RPC boundary.
pub trait EventSubscriberInterface {
    /// Returns an array of event names this subscriber wants to listen to.
    fn get_subscribed_events(&self) -> anyhow::Result<IndexMap<String, SubscribedEventEntry>>;

    /// The subscriber as it crosses the wire: PHP's `[$subscriber, $method]` array callables
    /// capture the subscriber object itself, represented here by its P-table handle.
    fn subscriber_handle(&self) -> PhpObjHandle;
}