yf_core_events class

Core events/observer handler.

Examples of usage:
_class('core_events')->listen('class.*',function() { return 'event #0 wildcarded'; });
_class('core_events')->listen('class.action',function($args) { return 'event # 1'.$args; },1);
_class('core_events')->listen('class.action',function() { return 'event # 2'; },2);
_class('core_events')->listen('class.action',function() { return 1; });
_class('core_events')->listen('class.*',function() { return 2; },5);
_class('core_events')->listen('class.action2',function() { return 1; });
_class('core_events')->listen('class2.action',function() { return 1; });
$r = "<pre>".print_r(_class('core_events')->fire('class.action','test'),1)."</pre>";

Fileplugins/sys/classes/yf_core_events.class.php
Use_class('core_events')

Public properties

PropertyDefaultDescription
None declared in this class.

Public methods 10

fire

public function fire($event, $payload = [], $halt = false)

Fire an event and call the listeners.

  • $event string
  • $halt bool

Returns array|null

    public function fire($event, $payload = [], $halt = false)
    {
        if (DEBUG_MODE) {
            debug('events_' . __FUNCTION__ . '[]', [
                'name' => $event,
                'payload_len' => count((array) $payload) . ' items',
                'halt' => $halt,
                'time_offset' => microtime(true),
                'trace' => trace(),
            ]);
        }
        $responses = [];
        // If an array is not given to us as the payload, we will turn it into one so
        // we can easily use call_user_func_array on the listeners, passing in the
        // payload to each of them so that they receive each of these arguments.
        if ( ! is_array($payload)) {
            $payload = [$payload];
        }
        $this->firing[] = $event;
        foreach ($this->get_listeners($event) as $listener) {
            $response = call_user_func_array($listener, $payload);
            // If a response is returned from the listener and event halting is enabled
            // we will just return this response, and not call the rest of the event
            // listeners. Otherwise we will add the response on the response list.
            if ($response !== null && $halt) {
                array_pop($this->firing);
                return $response;
            }
            // If a boolean false is returned from a listener, we will stop propagating
            // the event to any further listeners down in the chain, else we keep on
            // looping through the listeners and firing every one in our sequence.
            if ($response === false) {
                break;
            }
            $responses[] = $response;
        }
        array_pop($this->firing);
        return $halt ? null : $responses;
    }

firing

public function firing()

Get the event that is currently firing.

Returns string

    public function firing()
    {
        return last($this->firing);
    }

flush

public function flush($event)

Flush a set of queued events.

  • $event string
    public function flush($event)
    {
        $this->fire($event . '_queue');
    }

forget

public function forget($event)

Remove a set of listeners from the dispatcher.

  • $event string
    public function forget($event)
    {
        unset($this->listeners[$event]);
        unset($this->sorted[$event]);
    }

get_listeners

public function get_listeners($event_name)

Get all of the listeners for a given event name.

Returns array

    public function get_listeners($event_name)
    {
        $wildcards = $this->_get_wildcard_listeners($event_name);
        if ( ! isset($this->sorted[$event_name])) {
            $this->_sort_listeners($event_name);
        }
        return array_merge($this->sorted[$event_name], $wildcards);
    }

has_listeners

public function has_listeners($event_name)

Determine if a given event has listeners.

Returns bool

    public function has_listeners($event_name)
    {
        return isset($this->listeners[$event_name]);
    }

listen

public function listen($events, $listener, $priority = 0)

Register an event listener with the dispatcher.

  • $priority int
    public function listen($events, $listener, $priority = 0)
    {
        if (DEBUG_MODE) {
            debug('events_' . __FUNCTION__ . '[]', [
                'name' => $events,
                'listener' => is_callable($listener) ? 'Closure' : $listener,
                'priority' => $priority,
                'time_offset' => microtime(true),
                'trace' => trace(),
            ]);
        }
        foreach ((array) $events as $event) {
            if ($this->_str_contains($event, '*')) {
                return $this->_setup_wildcard_listen($event, $listener);
            }
            $this->listeners[$event][$priority][] = $listener;
            unset($this->sorted[$event]);
        }
    }

queue

public function queue($event, $payload = [])

Register a queued event and payload.

  • $event string
  • $payload array
    public function queue($event, $payload = [])
    {
        if (DEBUG_MODE) {
            debug('events_' . __FUNCTION__ . '[]', [
                'name' => $event,
                'payload_len' => count((array) $payload) . ' items',
                'time_offset' => microtime(true),
                'trace' => trace(),
            ]);
        }
        $this->listen($event . '_queue', function () use ($event, $payload) {
            $this->fire($event, $payload);
        });
    }

until

public function until($event, $payload = [])

Fire an event until the first non-null response is returned.

  • $event string
  • $payload array
    public function until($event, $payload = [])
    {
        return $this->fire($event, $payload, true);
    }

Methods starting with _ hooks and helpers; not routable as page actions

__call

public function __call($name, $args)

Catch missing method call.

    public function __call($name, $args)
    {
        return main()->extend_call($this, $name, $args);
    }