\Phabstractic\EventHandler

The Handler Class

This class takes an object, a function, and a namespace. It uses these to encapsulate a callable that can be triggered. It's the action part of an event, when triggered it does something. It can be paired with a filter to enable proper hooks and such. Without a paired filter, (an object external to this class) it acts when notified by any event.

NOTE: The filter is separate from the handler so that one filter can be used for multiple handlers ala conduit.

IMPORTANT! - To pass a closure to handler pass it as the function parameter

CHANGELOG

1.0: Created Handler - August 16th, 2013 1.1: Updated getDestination to handle function names - October 7th, 2013 2.0: Integrated with Primus 2 in mind - October 13th, 2015 3.0: integrated observer trait decoupled from priority modified constructor to default arguments set funcitons now determine if exists on call reformatted for inclusion in phabstractic - July 29th, 2016

Summary

Methods
Properties
Constants
detachPublisher()
attachPublisher()
unlinkFromPublishers()
getPublishers()
notifyObserver()
__debugInfo()
__construct()
getObject()
setObject()
getNamespace()
setNamespace()
getFunction()
setFunction()
handle()
getDestination()
buildFromArray()
buildFromClosure()
No public properties found
No constants found
constructObservedSubjects()
$observedSubjects
N/A
No private methods found
$object
$namespace
$function
N/A

Properties

$observedSubjects

$observedSubjects : \Phabstractic\Patterns\Phabstractic\Patterns\Resource\PublisherInterface

The subjects that this observer is listening to

For an observer that listens to multiple publishers with some added functionality see Phabstractic\Event\Aggregator

Type

\Phabstractic\Patterns\Phabstractic\Patterns\Resource\PublisherInterface

$object

$object : object

The object upon which the function (method) will be called

Type

object

$namespace

$namespace : string

The namespace of the object, or function to be called

Type

string

$function

$function : string

The name of the function or method to be called

Type

string

Methods

detachPublisher()

detachPublisher(\Phabstractic\Patterns\Resource\PublisherInterface  $publisher) 

This detaches the observer from its subject

Reciprocal methods exist in the publisher

Parameters

\Phabstractic\Patterns\Resource\PublisherInterface $publisher

attachPublisher()

attachPublisher(\Phabstractic\Patterns\Resource\PublisherInterface  $publisher) 

Attach this observer to a new publisher

This also acts like a 'set' function, and null is an acceptable value to be passed

Parameters

\Phabstractic\Patterns\Resource\PublisherInterface $publisher

unlinkFromPublishers()

unlinkFromPublishers() 

Remove this observer from all publishers

getPublishers()

getPublishers() : array

Return array of publishers

Returns

array

notifyObserver()

notifyObserver(\Phabstractic\Patterns\Resource\PublisherInterface  $target, \Phabstractic\Patterns\Resource\StateInterface  $state) : boolean

Notify the object of an event

This causes the object to 'fire' on any event passed to it.

Don't subclass this class to build a filter in this area, use a filter object instead. (See above)

Parameters

\Phabstractic\Patterns\Resource\PublisherInterface $target
\Phabstractic\Patterns\Resource\StateInterface $state

Returns

boolean —

Return true if handled (instanceof AbstractEvent)

__debugInfo()

__debugInfo() 

Debug Info (var_dump)

Display debug info

Requires PHP 5.6+

__construct()

__construct(object  $object = null, string  $function = null, string  $namespace = null) 

The Handler Constructor

This takes a destination (presumably an object, where our code is going to GO [forward]), a function/method name and some options

Parameters

object $object

The destination object

string $function

The function/method name to be called

string $namespace

The namespace definition of the destination

getObject()

getObject() : object

Return the destination object

Returns

object

setObject()

setObject(object  $object) 

Set the destination object

CAREFUL: Make sure your method is still valid

Parameters

object $object

getNamespace()

getNamespace() : string

Get the apprioriate namespace for the object

If applicable

Returns

string

setNamespace()

setNamespace(string  $namespace) 

Set the appropriate namespace for the object

If applicable

Parameters

string $namespace

getFunction()

getFunction() : string

Get the function/method name of the destination

Returns

string

setFunction()

setFunction(string  $function) 

Set the function/method name of the destination

Parameters

string $function

handle()

handle(\Phabstractic\Event\Resource\AbstractEvent  $e) : mixed

Handle the event

This method takes care of the details of getting an event ($e) to whatever needs to be called. The function/method/class is called passing the event along.

This works with instance methods, class methods (static), and normal defined functions, even closures, as long as they accept an AbstractEvent $e.

NOTE: To use a class method (static), $this->object must be a string

Parameters

\Phabstractic\Event\Resource\AbstractEvent $e

Returns

mixed —

The result of the given function

getDestination()

getDestination() : string|array|\Closure

Return the callback in whatever form it is

string for function or static function array for object / class Closure for, well, closures

Returns

string|array|\Closure

buildFromArray()

buildFromArray(array  $array) 

Build From Object-Method Array

Parameters

array $array

buildFromClosure()

buildFromClosure(  $closure) 

Build from Closure

Parameters

$closure

constructObservedSubjects()

constructObservedSubjects() 

Make sure $this->observedSubjects has been constructed