Loading…

PHP PubSub documentation in progress

Overview

Quick Start

Installable via Composer as bdk/pubsub

composer.json
{
    "require": {
        "bdk/pubsub": "^3.2"
    }
}

Quality / Badges

  • No Dependencies
  • Supported PHP versions
  • Build Status
  • Maintainability
  • Coverage

Classes

bdk\PubSub\Event

Represents a basic event

Events are passed to event subscribers/listeners

Extends

    bdk\PubSub\ValueStore

Implements

  • Serializable
  • JsonSerializable
  • IteratorAggregate
    • Traversable
  • ArrayAccess

Methods

__construct(Subject $subject = null[, array<TKey,TValue> $values = array()])

Construct an event with optional subject and values

Parameters
subject
The subject of the event (usually an object)
values
Values to store in the event
__debugInfo(): array{ propagationStopped: bool, subject: class-string|mixed, values: array<TKey, TValue>, }

Magic Method

Return Value
array{ propagationStopped: bool, subject: class-string|mixed, values: array<TKey, TValue>, }
getSubject(): Subject

Get Event's "subject"

Return Value
Subject
Usually the object that published the event
isPropagationStopped(): bool

Has propagation been stopped?

Description

If stopped, no further event subscribers will be called

Return Value
bool
Whether propagation is stopped for this event
phpDoc
see: Event::stopPropagation()
stopPropagation()

Stops the propagation of the event

Description

No further event subscribers will be called

Inherited from bdk\PubSub\ValueStore
__serialize(): array<TKey,TValue>

Serialize magic method (since php 7.4)

Return Value
array<TKey,TValue>
__unserialize(array<TKey,TValue> $data)

Unserialize

Parameters
data
serialized data
getIterator(): ArrayIterator<TKey,TValue>

IteratorAggregate interface

Description

Iterate over the object like an array.

Return Value
ArrayIterator<TKey,TValue>
getValue(TKey $key): TValue|null

Get value by key.

Parameters
key
Value name
Return Value
TValue|null
getValues(): array<TKey,TValue>

Get all stored values

Return Value
array<TKey,TValue>
hasValue(TKey $key): bool

Does specified key have a value?

Parameters
key
Value name
Return Value
bool
jsonSerialize(): array<TKey,TValue>

Implements JsonSerializable

Return Value
array<TKey,TValue>
offsetExists(TKey $key): bool

ArrayAccess hasValue.

Parameters
key
Array key
Return Value
bool
offsetGet(TKey $key): TValue|null

ArrayAccess getValue.

Parameters
key
Array key
Return Value
TValue|null
offsetSet(TKey $offset, TValue $value)

ArrayAccess setValue

Parameters
offset
Array key to set
value
Value
offsetUnset(TKey $key)

ArrayAccess interface

Parameters
key
Array key
serialize(): string

Implements Serializable

Return Value
string
setValue(TKey $key, TValue $value): $this

Set value

Parameters
key
Value name
value
Value
Return Value
$this
setValues(array<TKey,TValue> $values = array()): $this

Clears existing values and sets new values

Parameters
values
key=>value array of values
Return Value
$this
unserialize(string $data)

Implements Serializable

Parameters
data
serialized data

bdk\PubSub\Manager

Event publish/subscribe event manager

Constants

public EVENT_PHP_SHUTDOWN = php.shutdown
public DEFAULT_PRIORITY = 0

Methods

__construct()

Constructor

addSubscriberInterface(bdk\PubSub\SubscriberInterface $interface): array<string,list<SubscriberInfoRaw>>

Subscribe to all of the event subscribers provided by passed object

Description

Calls $interface's getInterfaceSubscribers method and subscribes accordingly

Parameters
interface
object implementing subscriber interface
Return Value
array<string,list<SubscriberInfoRaw>>
A normalized list of subscriptions added.
getSubscribers(string $eventName = null): array

Gets the subscribers of a specific event or all subscribers sorted by descending priority.

Description

If event name is not specified, subscribers for all events will be returned

Parameters
eventName
The name of the event
Return Value
array
The event subscribers for the specified event, or all event subscribers by event name
phpDoc
psalm-return: ($eventName is string ? list<SubscriberInfo> : array<string, list<SubscriberInfo>>)
hasSubscribers(string $eventName = null): bool

Checks whether an event has any registered subscribers.

Parameters
eventName
The name of the event
Return Value
bool
publish(string $eventName[, mixed $eventOrSubject = null[, array $values = array()]]): bdk\PubSub\Event

Publish/Trigger/Dispatch event

Parameters
eventName
event name
eventOrSubject
passed to subscribers
values
values to attach to event
Return Value
bdk\PubSub\Event
removeSubscriberInterface(bdk\PubSub\SubscriberInterface $interface): array[]

Unsubscribe from all of the event subscribers provided by passed object

Description

Calls $interface's getInterfaceSubscribers method and unsubscribes accordingly

Parameters
interface
object implementing subscriber interface
Return Value
array[]
normalized list of subscriptions removed.
subscribe(string $eventName, callable|ClosureFactory $callable[, int $priority = 0[, bool $onlyOnce = false]])

Subscribe to event

Description

Callable will receive 3 params:

  • Event
  • (string) eventName
  • EventManager

Lazy-load the subscriber

It's possible to lazy load the subscriber object via a "closure factory" array(Closure, 'methodName') - closure returns object array(Closure) - closure returns object that is callable (ie has __invoke method) The closure will be called the first time the event occurs

Parameters
eventName
event name
callable
callable or closure factory
priority
The higher this value, the earlier we handle event
onlyOnce
(false) Auto-unsubscribe after first invocation
phpDoc
throws: InvalidArgumentException
unsubscribe(string $eventName, callable|ClosureFactory $callable)

Removes an event subscriber from the specified event.

Parameters
eventName
The event we're unsubscribing from
callable
The subscriber to remove

bdk\PubSub\SubscriberInterface

Provide event subscribers

modifiers
abstract
interface

Methods

getSubscriptions(): array<string,string|array>

Return event subscribers

modifiers
abstract
Description

The array keys are event names and the value can be:

method: priority defaults to 0, onlyOnce defaults to false
array: (required) method, (optional) int priority, (optional) bool onlyOnce
array: any combination of the above

method = string|Callable name of public method or Closure

Return Value
array<string,string|array>

bdk\PubSub\ValueStore

Value store

Note:

  • The Serializable interface - since PHP 5.1. Deprecated in php 8.1
  • serialize and unserialize magic methods : since PHP 7.4

Implements

  • ArrayAccess
  • IteratorAggregate
    • Traversable
  • JsonSerializable
  • Serializable

Methods

__construct(array<TKey,TValue> $values = array())

Constructor

Parameters
values
Values to store
__debugInfo(): array

Magic Method

Return Value
array
__serialize(): array<TKey,TValue>

Serialize magic method (since php 7.4)

Return Value
array<TKey,TValue>
__unserialize(array<TKey,TValue> $data)

Unserialize

Parameters
data
serialized data
getIterator(): ArrayIterator<TKey,TValue>

IteratorAggregate interface

Description

Iterate over the object like an array.

Return Value
ArrayIterator<TKey,TValue>
getValue(TKey $key): TValue|null

Get value by key.

Parameters
key
Value name
Return Value
TValue|null
getValues(): array<TKey,TValue>

Get all stored values

Return Value
array<TKey,TValue>
hasValue(TKey $key): bool

Does specified key have a value?

Parameters
key
Value name
Return Value
bool
jsonSerialize(): array<TKey,TValue>

Implements JsonSerializable

Return Value
array<TKey,TValue>
offsetExists(TKey $key): bool

ArrayAccess hasValue.

Parameters
key
Array key
Return Value
bool
offsetGet(TKey $key): TValue|null

ArrayAccess getValue.

Parameters
key
Array key
Return Value
TValue|null
offsetSet(TKey $offset, TValue $value)

ArrayAccess setValue

Parameters
offset
Array key to set
value
Value
offsetUnset(TKey $key)

ArrayAccess interface

Parameters
key
Array key
serialize(): string

Implements Serializable

Return Value
string
setValue(TKey $key, TValue $value): $this

Set value

Parameters
key
Value name
value
Value
Return Value
$this
setValues(array<TKey,TValue> $values = array()): $this

Clears existing values and sets new values

Parameters
values
key=>value array of values
Return Value
$this
unserialize(string $data)

Implements Serializable

Parameters
data
serialized data
onSet(array<TKey,TValue> $values = array())

Extend me to perform action after setting value/values

modifiers
protected
Parameters
values
key => values being set
phpDoc
psalm-suppress: PossiblyUnusedParam