Phalcon events
NOTE
All classes are prefixed with Phalcon
Events\AbstractEventsAware¶
Abstract Source on GitHub
This abstract class offers access to the events manager
Phalcon\Events\AbstractEventsAware
Method Summary¶
public
ManagerInterface|null
getEventsManager()
Returns the internal event manager
public
void
setEventsManager( ManagerInterface $eventsManager )
Sets the events manager
protected
mixed
fireManagerEvent(string $eventName,mixed $data = null,bool $cancellable = true)
Helper method to fire an event
Properties¶
protected
ManagerInterface|null
$eventsManager = null
Methods¶
getEventsManager()¶
Returns the internal event manager
setEventsManager()¶
Sets the events manager
fireManagerEvent()¶
protected function fireManagerEvent(
string $eventName,
mixed $data = null,
bool $cancellable = true
): mixed;
Helper method to fire an event
Events\Event¶
Class Source on GitHub
Phalcon\Events\Event
This class offers contextual information of a fired event in the EventsManager
Phalcon\Events\Event;
$event = new Event("db:afterQuery", $this, ["data" => "mydata"], true);
if ($event->isCancelable()) {
$event->stop();
}
Phalcon\Events\Event- implementsPhalcon\Events\EventInterface,Phalcon\Contracts\Events\Stoppable
Uses Phalcon\Contracts\Events\Stoppable · Phalcon\Events\Exceptions\EventNotCancelable
Method Summary¶
public
__construct(string $type,object|null $source = null,mixed $data = null,bool $cancelable = true)
Event constructor.
public
mixed
getData()
public
object|null
getSource()
public
string
getType()
public
bool
isCancelable()
Check whether the event is cancelable.
public
bool
isPropagationStopped()
Returns whether propagation must stop. PSR-14 alias backed by the same
public
bool
isStopped()
Check whether the event is currently stopped.
public
EventInterface
setData( mixed $data = null )
Sets event data.
public
EventInterface
setType( string $type )
Sets event type.
public
EventInterface
stop()
Stops the event preventing propagation.
Properties¶
protected
bool
$cancelable = true
protected
mixed
$data = null
protected
object|null
$source = null
protected
bool
$stopped = false
Is event propagation stopped?
protected
string
$type
Methods¶
__construct()¶
public function __construct(
string $type,
object|null $source = null,
mixed $data = null,
bool $cancelable = true
);
Event constructor.
getData()¶
getSource()¶
getType()¶
isCancelable()¶
Check whether the event is cancelable.
isPropagationStopped()¶
Returns whether propagation must stop. PSR-14 alias backed by the same
stopped flag as isStopped(); calling stop() flips both.
isStopped()¶
Check whether the event is currently stopped.
setData()¶
Sets event data.
setType()¶
Sets event type.
stop()¶
Stops the event preventing propagation.
Events\EventInterface¶
Interface Source on GitHub
Phalcon\Events\EventInterface
Phalcon\Contracts\Events\EventPhalcon\Events\EventInterface
Uses Phalcon\Contracts\Events\Event
Events\EventsAwareInterface¶
Interface Source on GitHub
Phalcon\Events\EventsAwareInterface
Phalcon\Contracts\Events\EventsAwarePhalcon\Events\EventsAwareInterface
Uses Phalcon\Contracts\Events\EventsAware
Events\Exception¶
Class Source on GitHub
Phalcon\Events\Exception
Exceptions thrown in Phalcon\Events will use this class
\ExceptionPhalcon\Events\Exception
Events\Exceptions\EventNotCancelable¶
Class Source on GitHub
\ExceptionPhalcon\Events\ExceptionPhalcon\Events\Exceptions\EventNotCancelable
Uses Phalcon\Events\Exception
Method Summary¶
Methods¶
__construct()¶
Events\Exceptions\InvalidEventHandler¶
Class Source on GitHub
\ExceptionPhalcon\Events\ExceptionPhalcon\Events\Exceptions\InvalidEventHandler
Uses Phalcon\Events\Exception
Method Summary¶
Methods¶
__construct()¶
Events\Exceptions\InvalidEventSource¶
Class Source on GitHub
\ExceptionPhalcon\Events\ExceptionPhalcon\Events\Exceptions\InvalidEventSource
Uses Phalcon\Events\Exception
Method Summary¶
Methods¶
__construct()¶
Events\Exceptions\InvalidEventType¶
Class Source on GitHub
\ExceptionPhalcon\Events\ExceptionPhalcon\Events\Exceptions\InvalidEventType
Uses Phalcon\Events\Exception
Method Summary¶
Methods¶
__construct()¶
Events\Exceptions\InvalidSubscriberConfiguration¶
Class Source on GitHub
\ExceptionPhalcon\Events\ExceptionPhalcon\Events\Exceptions\InvalidSubscriberConfiguration
Uses Phalcon\Events\Exception
Method Summary¶
Methods¶
__construct()¶
Events\Exceptions\NoListenersForEvent¶
Class Source on GitHub
\ExceptionPhalcon\Events\ExceptionPhalcon\Events\Exceptions\NoListenersForEvent
Uses Phalcon\Events\Exception
Method Summary¶
Methods¶
__construct()¶
Events\Manager¶
Class Source on GitHub
Phalcon Events Manager, offers an easy way to intercept and manipulate, if needed, the normal flow of operation. With the EventsManager the developer can create hooks or plugins that will offer monitoring of data, manipulation, conditional execution and much more.
Phalcon\Events\Manager- implementsPhalcon\Events\ManagerInterface,\Psr\EventDispatcher\EventDispatcherInterface,Phalcon\Contracts\Events\Enumerable
Uses Closure · Phalcon\Contracts\Events\Enumerable · Phalcon\Contracts\Events\Stoppable · Phalcon\Contracts\Events\Subscriber · Phalcon\Db\Event\AbstractModelEvent · Phalcon\Db\Event\ModelEventNameEnum · Phalcon\Events\Exceptions\InvalidEventHandler · Phalcon\Events\Exceptions\InvalidEventType · Phalcon\Events\Exceptions\InvalidSubscriberConfiguration · Phalcon\Events\Exceptions\NoListenersForEvent · Psr\EventDispatcher\EventDispatcherInterface · Psr\EventDispatcher\StoppableEventInterface · Throwable
Method Summary¶
public
void
addSubscriber( Subscriber $subscriber )
Registers an event subscriber. The subscriber's getSubscribedEvents()
public
bool
arePrioritiesEnabled()
Returns if priorities are enabled
public
void
attach(array|string $eventType,callable|object $handler,int $priority = self::DEFAULT_PRIORITY)
Attach a listener to the events manager
public
void
clearSubscribers()
Removes every registered subscriber and detaches each listener they
public
void
collectResponses( bool $collect )
Tells the event manager if it needs to collect all the responses returned
public
void
detach(string $eventType,callable|object $handler)
Detach the listener from the events manager
public
void
detachAll( string|null $type = null )
Removes all events from the EventsManager
public
mixed
dispatch(object $event,array|string|null $name = null,object|null $source = null)
Dispatches an object event to the appropriate event listeners.
public
void
enablePriorities( bool $enablePriorities )
Set if priorities are enabled in the EventsManager.
public
mixed
fire(string $eventType,object $source,mixed $data = null,bool $cancelable = true)
Fires an event in the events manager causing the active listeners to be
public
array
fireAll(string $eventType,object $source,mixed $data = null,bool $cancelable = true)
Fires an event and returns every listener's return value as an indexed
public
mixed
fireQueue(array $queue,EventInterface $event)
Internal handler to call a queue of events.
public
array
getListenerMap()
Returns every event type that currently has at least one listener,
public
array
getListeners( string $type )
Returns all the attached listeners of a certain type
public
int
getMethodExistsCacheLimit()
Returns the configured method_exists-cache cap (0 = unlimited).
public
array
getResponses()
Returns all the responses returned by every handler executed by the last
public
array
getSubscribers()
Returns the list of registered subscriber instances.
public
void
halt()
Manager-level kill switch. After halt(), every fire()/fireAll()/
public
bool
hasListeners( string $type )
Check whether certain type of event has listeners
public
bool
isCollecting()
Check if the events manager is collecting all the responses returned by
public
bool
isHalted()
Returns whether the manager-level kill switch is engaged. See halt().
public
bool
isStopOnFalse()
Returns whether the stop-on-false short-circuit is enabled.
public
bool
isStrict()
Returns whether strict mode is enabled.
public
bool
isValidHandler( mixed $handler )
public
void
removeSubscriber( Subscriber $subscriber )
Removes a previously registered subscriber. Detaches every listener the
public
void
resume()
Clears the manager-level kill switch set by halt().
public
void
setMethodExistsCacheLimit( int $methodExistsCacheLimit )
Caps the number of distinct handler classes retained in the
public
void
setStopOnFalse( bool $flag )
Enables/disables the stop-on-false short-circuit. Default off.
public
void
setStrict( bool $strict )
Enables/disables strict mode. When true, fire()/fireAll() throw when
protected
mixed
afterFire(mixed $status,string $eventType,object $source,mixed $data = null,bool $cancelable = true)
Extension seam invoked after an event has been dispatched to its
protected
bool
beforeFire(string $eventType,object $source,mixed $data = null,bool $cancelable = true)
Extension seam invoked before an event is dispatched. The base
Properties¶
protected
bool
$collect = false
protected
bool
$enablePriorities = false
protected
array
$eventNameCache = []
Parsed-eventType cache. Memoizes the strpos + substr work done in
fire() so the same event name fired repeatedly collapses to a single
hash lookup.
Shape: eventNameCache[$eventType] = [typePrefix, eventName]
protected
array
$events = []
Listener storage. Shape:
events[$eventType] = [
[handler, type, priority] // types 0, 1, 3
[handler, type, priority, className] // type 2 carries
// resolved class name
...
]
type is classified once at attach() time so the dispatch loop can
route via a simple branch:
0 - Closure
1 - [obj, method] array callable
2 - plain object: method named after the event
3 - generic callable (string fn name, invokable object, etc.)
protected
int
$fireDepth = 0
Re-entrancy depth of fire()/fireAll(). 0 means no fire is in progress.
Used to keep nested fire() calls from clobbering the outer caller's
$this->responses accumulator.
protected
bool
$halted = false
Manager-level kill switch. When true, every fire()/fireAll()/
fireQueue() call returns immediately without dispatching. Cleared by
resume().
protected
array
$methodExistsCache = []
Memoized method_exists() results for the plain-object dispatch path.
Keyed by handlerClass => [methodName => bool].
protected
int
$methodExistsCacheLimit = 0
Maximum number of distinct handler classes retained in
methodExistsCache. 0 (default) keeps the unbounded behavior.
protected
array<array-key, mixed>
$responses = []
protected
bool
$stopOnFalse = false
When true, a listener returning literal false (with the event's
cancelable flag on) short-circuits the dispatch loop and pins the
fire() return as false. Default off.
protected
bool
$strict = false
When true, fire()/fireAll() throw on dispatch of an event that has zero
matching listeners. Default off.
protected
array
$subscriberEventsCache = []
Memoized getSubscribedEvents() maps keyed by Subscriber class name.
protected
array
$subscribers = []
Methods¶
addSubscriber()¶
Registers an event subscriber. The subscriber's getSubscribedEvents() map is parsed and each entry is attached through the regular listener pipeline.
arePrioritiesEnabled()¶
Returns if priorities are enabled
attach()¶
final public function attach(
array|string $eventType,
callable|object $handler,
int $priority = self::DEFAULT_PRIORITY
): void;
Attach a listener to the events manager
clearSubscribers()¶
Removes every registered subscriber and detaches each listener they contributed. Listeners attached via attach() are untouched.
collectResponses()¶
Tells the event manager if it needs to collect all the responses returned by every registered listener in a single fire
detach()¶
Detach the listener from the events manager
detachAll()¶
Removes all events from the EventsManager
dispatch()¶
public function dispatch(
object $event,
array|string|null $name = null,
object|null $source = null
): mixed;
Dispatches an object event to the appropriate event listeners.
PSR-14 shaped: listeners receive the (possibly mutated) event object. Propagation stops when the event implements {@see StoppableEventInterface} and reports it is stopped.
enablePriorities()¶
Set if priorities are enabled in the EventsManager.
fire()¶
public function fire(
string $eventType,
object $source,
mixed $data = null,
bool $cancelable = true
): mixed;
Fires an event in the events manager causing the active listeners to be notified about it
fireAll()¶
public function fireAll(
string $eventType,
object $source,
mixed $data = null,
bool $cancelable = true
): array;
Fires an event and returns every listener's return value as an indexed
array. Independent of collectResponses(); the caller's collected state
on $this->responses is preserved (stashed and restored).
fireQueue()¶
Internal handler to call a queue of events.
Kept as a thin BC wrapper around the private dispatch loop.
getListenerMap()¶
Returns every event type that currently has at least one listener, mapped to that type's listeners. Types contributed by subscribers are included, because addSubscriber() attaches through the regular listener pipeline.
Unwrapping is delegated to getListeners() so the internal shape of $this->events is read in exactly one place.
getListeners()¶
Returns all the attached listeners of a certain type
getMethodExistsCacheLimit()¶
Returns the configured method_exists-cache cap (0 = unlimited).
getResponses()¶
Returns all the responses returned by every handler executed by the last 'fire' executed
getSubscribers()¶
Returns the list of registered subscriber instances.
halt()¶
Manager-level kill switch. After halt(), every fire()/fireAll()/ fireQueue() call returns immediately without dispatching, until resume() is called.
hasListeners()¶
Check whether certain type of event has listeners
isCollecting()¶
Check if the events manager is collecting all the responses returned by every registered listener in a single fire
isHalted()¶
Returns whether the manager-level kill switch is engaged. See halt().
isStopOnFalse()¶
Returns whether the stop-on-false short-circuit is enabled.
isStrict()¶
Returns whether strict mode is enabled.
isValidHandler()¶
removeSubscriber()¶
Removes a previously registered subscriber. Detaches every listener the subscriber declared via getSubscribedEvents(). Idempotent.
resume()¶
Clears the manager-level kill switch set by halt().
setMethodExistsCacheLimit()¶
Caps the number of distinct handler classes retained in the method_exists memoization cache. 0 disables the cap.
setStopOnFalse()¶
Enables/disables the stop-on-false short-circuit. Default off.
setStrict()¶
Enables/disables strict mode. When true, fire()/fireAll() throw when dispatching an event with zero matching listeners.
afterFire()¶
protected function afterFire(
mixed $status,
string $eventType,
object $source,
mixed $data = null,
bool $cancelable = true
): mixed;
Extension seam invoked after an event has been dispatched to its
listener queues. The base implementation returns status unchanged.
beforeFire()¶
protected function beforeFire(
string $eventType,
object $source,
mixed $data = null,
bool $cancelable = true
): bool;
Extension seam invoked before an event is dispatched. The base implementation returns true, so dispatch proceeds. Returning false aborts the dispatch entirely.
Events\ManagerInterface¶
Interface Source on GitHub
Phalcon\Events\ManagerInterface
Phalcon\Contracts\Events\ManagerPhalcon\Events\ManagerInterface
Uses Phalcon\Contracts\Events\Manager
Events\PsrEventInterface¶
Interface Source on GitHub
Phalcon\Events\PsrEventInterface
Events\Traits\EventsAwareTrait¶
Trait Source on GitHub
Phalcon\Events\Traits\EventsAwareTrait
Uses Phalcon\Events\Exception · Phalcon\Events\ManagerInterface · Phalcon\Events\PsrEventInterface
Used by Phalcon\Application\AbstractApplication · Phalcon\Auth\Guard\AbstractGuard · Phalcon\Autoload\Loader · Phalcon\Cache\AbstractCache · Phalcon\Cli\Task · Phalcon\Db\Adapter\AbstractAdapter · Phalcon\Di\Di · Phalcon\Dispatcher\AbstractDispatcher · Phalcon\Http\Request · Phalcon\Http\Response · Phalcon\Mvc\Controller · Phalcon\Mvc\Dispatcher · Phalcon\Mvc\Micro · Phalcon\Mvc\Model\Manager · Phalcon\Mvc\Router · Phalcon\Mvc\View · Phalcon\Mvc\View\Engine\AbstractEngine · Phalcon\Mvc\View\Simple · Phalcon\Storage\Adapter\AbstractAdapter
Method Summary¶
public
ManagerInterface|null
getEventsManager()
Returns the internal event manager
public
void
setEventsManager( ManagerInterface $eventsManager )
Sets the events manager
protected
fireManagerEvent(string $eventName,mixed $data = null,bool $cancellable = true)
Helper method to fire an event
protected
mixed
firePsrEvent(PsrEventInterface $event,string|null $name = null)
Properties¶
protected
ManagerInterface|null
$eventsManager = null
Methods¶
getEventsManager()¶
Returns the internal event manager
setEventsManager()¶
Sets the events manager
fireManagerEvent()¶
protected function fireManagerEvent(
string $eventName,
mixed $data = null,
bool $cancellable = true
);
Helper method to fire an event