Class Event
Event is the base class for all event classes.
It encapsulates the parameters associated with an event.
The [[sender]] property describes who raises the event.
And the [[handled]] property indicates if the event is handled.
If an event handler sets [[handled]] to be true, the rest of the
uninvoked handlers will no longer be called to handle the event.
Additionally, when attaching an event handler, extra data may be passed
and be available via the [[data]] property when the event handler is invoked.
-
Maslosoft\Mangan\Events\Event
implements
Maslosoft\Mangan\Interfaces\Events\EventInterface
Methods summary
public static
|
#
on( Maslosoft\Addendum\Interfaces\AnnotatedInterface|object|string $model, string $name, callable $handler, mixed $data = null, boolean $append = true )
Attaches an event handler to a class-level event.
Attaches an event handler to a class-level event.
When a class-level event is triggered, event handlers attached
to that class and all parent classes will be invoked.
For example, the following code attaches an event handler to document's
afterInsert event:
Event::on($model, EntityManager::EventAfterInsert, function ($event) {
var_dump(get_class($event->sender) . ' is inserted.');
});
The handler will be invoked for every successful document insertion.
NOTE: Each call will attach new event handler. When placing event
initialization in class constructors etc. ensure that it is evaluated once,
or it might trigger same event handler multiple times.
Parameters
- $model
- the object specifying the class-level event.
- $name
- the event name.
- $handler
- the event handler.
- $data
the data to be passed to the event handler when the event is triggered.
When the event handler is invoked, this data can be accessed via [[Event::data]].
- $append
whether to append new event handler to the end of the existing
handler list. If false, the new handler will be inserted at the beginning of the existing
handler list.
See
|
public static
boolean
|
#
off( Maslosoft\Addendum\Interfaces\AnnotatedInterface|object|string $model, string $name, callable $handler = null )
Detaches an event handler from a class-level event.
Detaches an event handler from a class-level event.
This method is the opposite of [[on()]].
Parameters
- $model
- the object specifying the class-level event.
- $name
- the event name.
- $handler
the event handler to be removed.
If it is null, all handlers attached to the named event will be removed.
Returns
boolean whether a handler is found and detached.
See
|
public static
boolean
|
#
trigger( Maslosoft\Addendum\Interfaces\AnnotatedInterface $model, string $name, Maslosoft\Mangan\Events\ModelEvent & $event = null )
Triggers a class-level event.
This method will cause invocation of event handlers that are attached to the named event
for the specified class and all its parent classes.
Triggers a class-level event.
This method will cause invocation of event handlers that are attached to the named event
for the specified class and all its parent classes.
Parameters
- $model
- the object specifying the class-level event.
- $name
- the event name.
- $event
- the event parameter. If not set, a default
ModelEvent object will be created.
Returns
boolean True if event was triggered.
|
public static
boolean
|
#
valid( Maslosoft\Addendum\Interfaces\AnnotatedInterface $model, string $name, Maslosoft\Mangan\Events\ModelEvent $event = null )
Triggers a class-level event and checks if it's valid.
If don't have event handler returns true. If event handler is set, return true if Event::isValid .
This method will cause invocation of event handlers that are attached to the named event
for the specified class and all its parent classes.
Triggers a class-level event and checks if it's valid.
If don't have event handler returns true. If event handler is set, return true if Event::isValid .
This method will cause invocation of event handlers that are attached to the named event
for the specified class and all its parent classes.
Parameters
- $model
- the object specifying the class-level event.
- $name
- the event name.
- $event
- the event parameter. If not set, a default [[ModelEvent]] object will be created.
Returns
boolean True if event was triggered and is valid.
|
public static
boolean|null
|
#
handled( Maslosoft\Addendum\Interfaces\AnnotatedInterface $model, string $name, Maslosoft\Mangan\Events\ModelEvent $event = null )
Triggers a class-level event and checks if it's handled.
If don't have event handler returns true. If event handler is set, return true if Event::handled .
This method will cause invocation of event handlers that are attached to the named event
for the specified class and all its parent classes.
Triggers a class-level event and checks if it's handled.
If don't have event handler returns true. If event handler is set, return true if Event::handled .
This method will cause invocation of event handlers that are attached to the named event
for the specified class and all its parent classes.
Parameters
- $model
- the object specifying the class-level event.
- $name
- the event name.
- $event
- the event parameter. If not set, a default [[Event]] object will be created.
Returns
boolean|null True if handled, false otherways, null if not triggered
|
public static
boolean
|
#
hasHandler( Maslosoft\Addendum\Interfaces\AnnotatedInterface|string $class, string $name )
Check if model has event handler.
IMPORTANT: It does not check for propagated events
Check if model has event handler.
IMPORTANT: It does not check for propagated events
Parameters
- $class
- the object specifying the class-level event
- $name
- the event name.
Returns
boolean True if has handler
|
public static
array
|
#
getPartials( string $className )
Get class/interface/trait names from which class is composed.
Get class/interface/trait names from which class is composed.
Parameters
Returns
array
|
Properties summary
public
string
|
$name
the event name. This property is set by [[Component::trigger()]] and [[trigger()]].
Event handlers may use this property to check what event it is handling.
the event name. This property is set by [[Component::trigger()]] and [[trigger()]].
Event handlers may use this property to check what event it is handling.
|
|
public
object
|
$sender
the sender of this event. If not set, this property will be
set as the object whose "trigger()" method is called.
This property may also be a null when this event is a
class-level event which is triggered in a static context.
the sender of this event. If not set, this property will be
set as the object whose "trigger()" method is called.
This property may also be a null when this event is a
class-level event which is triggered in a static context.
|
|
public
boolean
|
$handled
whether the event is handled. Defaults to false.
When a handler sets this to be true, the event processing will stop and
ignore the rest of the uninvoked event handlers.
whether the event is handled. Defaults to false.
When a handler sets this to be true, the event processing will stop and
ignore the rest of the uninvoked event handlers.
|
|
public
mixed
|
$data
the data that is passed to [[Component::on()]] when attaching an event handler.
Note that this varies according to which event handler is currently executing.
the data that is passed to [[Component::on()]] when attaching an event handler.
Note that this varies according to which event handler is currently executing.
|
|