ChainHelper

Last updated: August 12th, 2023
Release:

The ChainHelper class provides the basic functionality for the Chain system in Stoic:PHP. Specifically, it allows developers to link nodes together and pass a DispatchBase object to the traverse() method, so that the nodes can attempt to process the data.

Events vs Chains

The ChainHelper class was built to operate with multiple linked nodes at any given time, but easily lends itself to use as an event dispatcher. By insantiating a ChainHelper and telling it to function as an 'event' chain, the instance will only allow a single node to be linked at all times. Additional nodes that are linked to the chain will simply overwrite the previously linked node and take its place as the event processor.

The ChainHelper class provides the following properties and methods:

Properties

protected array $_nodes Internal collection of linked nodes protected boolean $_isEvent Whether or not the instance is an 'event' chain protected boolean $_doDebug Whether or not the instance should record debug messages protected callable $_logger Optional callback that receives debug messages

Methods

public ChainHelper __construct(boolean $isEvent = false, boolean $doDebug = false) Constructor with ability to set event and debug flags for new instance public ChainHelper toggleDebug(boolean $doDebug) Toggles use of debug messages/callback for instance public array getNodeList() Returns full list of nodes linked to chain public void hookLogger(callable $callback) Attaches the given callback to the instance so it can receive debug messages, if toggled public boolean isEvent() Returns whether or not the instance is setup as an 'event' chain public ChainHelper linkNode(NodeBase $node) Attaches a valid node to the chain public boolean traverse(DispatchBase &$dispatch, mixed $sender = null) Begins to distribute the given dispatch to every linked node on the chain, optionally with a sender argument for additional context protected void log(string $message) Conditionally sends the message to the registered logger callback

Examples

For examples, see the 'ChainHelper' section of the Examples page.

Further Reading

Next Up

Continue to read about examples, or visit the Table of Contents.