Wire::getHooks PHP Method

getHooks() public method

Return all hooks associated with this class instance or method (if specified)
public getHooks ( string $method = '', integer $type ) : array
$method string Optional method that hooks will be limited to. Or specify '*' to return all hooks everywhere.
$type integer Type of hooks to return: 0=all, 1=local only, 2=static only
return array
    public function getHooks($method = '', $type = 0)
    {
        $hooks = array();
        // first determine which local hooks when should include
        if ($type !== 2) {
            if ($method && $method !== '*') {
                // populate all local hooks for given method
                if (isset($this->localHooks[$method])) {
                    $hooks = $this->localHooks[$method];
                }
            } else {
                // populate all local hooks, regardless of method
                // note: sort of return hooks is no longer priority based
                // @todo account for '*' method, which should return all hooks regardless of instance
                foreach ($this->localHooks as $method => $methodHooks) {
                    $hooks = array_merge(array_values($hooks), array_values($methodHooks));
                }
            }
        }
        // if only local hooks requested, we can return them now
        if ($type === 1) {
            return $hooks;
        }
        $needSort = false;
        // join in static hooks
        foreach (self::$staticHooks as $className => $staticHooks) {
            if (!$this instanceof $className && $method !== '*') {
                continue;
            }
            // join in any related static hooks to the local hooks
            if ($method && $method !== '*') {
                // retrieve all static hooks for method
                if (!empty($staticHooks[$method])) {
                    if (count($hooks)) {
                        $collisions = array_intersect_key($hooks, $staticHooks[$method]);
                        $hooks = array_merge($hooks, $staticHooks[$method]);
                        if (count($collisions)) {
                            // identify and resolve priority collisions
                            foreach ($collisions as $priority => $hook) {
                                $n = 0;
                                while (isset($hooks["{$priority}.{$n}"])) {
                                    $n++;
                                }
                                $hooks["{$priority}.{$n}"] = $hook;
                            }
                        }
                        $needSort = true;
                    } else {
                        $hooks = $staticHooks[$method];
                    }
                }
            } else {
                // no method specified, retrieve all for class
                // note: priority-based array indexes are no longer in tact
                $hooks = array_values($hooks);
                foreach ($staticHooks as $_method => $methodHooks) {
                    $hooks = array_merge($hooks, array_values($methodHooks));
                }
            }
        }
        if ($needSort && count($hooks) > 1) {
            defined("SORT_NATURAL") ? ksort($hooks, SORT_NATURAL) : uksort($hooks, "strnatcmp");
        }
        return $hooks;
    }

Usage Example

 /**
  * Get hooks debug info for the given Wire object
  * 
  * @param Wire $obj
  * @return array
  * 
  */
 public function getHooksInfo(Wire $obj)
 {
     $hooks = array();
     foreach ($obj->getHooks() as $hook) {
         list($class, $priority) = explode(':', $hook['id']);
         $key = '';
         $value = '';
         if ($hook['options']['before']) {
             $key .= "before ";
         }
         if ($hook['options']['type'] == 'property') {
             $key .= "property ";
         } else {
             if ($hook['options']['after']) {
                 if (method_exists($class, $hook['method']) || method_exists($class, '___' . $hook['method'])) {
                     $key .= "after ";
                 }
             }
         }
         if ($hook['options']['type'] == 'property' || !$hook['options']['allInstances']) {
             $key .= "{$class}" . '->' . "{$hook['method']}";
         } else {
             $key .= "{$class}::{$hook['method']}";
         }
         $filename = '';
         if (!empty($hook['toObject'])) {
             $value .= $hook['toObject']->className() . "->";
             $ref = new ReflectionClass($hook['toObject']);
             $filename = $ref->getFileName();
         }
         if (!empty($hook['toMethod'])) {
             if (is_string($hook['toMethod'])) {
                 $value .= "{$hook['toMethod']}()";
             } else {
                 if (is_callable($hook['toMethod'])) {
                     $ref = new ReflectionFunction($hook['toMethod']);
                     $filename = $ref->getFileName();
                     $value = "anonymous function()";
                 }
             }
         }
         if ($filename) {
             $value .= " in " . basename($filename);
         }
         if (!isset($hooks[$key])) {
             $hooks[$key] = $value;
         } else {
             if (!is_array($hooks[$key])) {
                 $hooks[$key] = array($hooks[$key]);
             }
             $hooks[$key][] = $value;
         }
     }
     return $hooks;
 }