QM_Util::populate_callback PHP Method

populate_callback() public static method

public static populate_callback ( array $callback )
$callback array
        public static function populate_callback(array $callback)
        {
            if (is_string($callback['function']) and false !== strpos($callback['function'], '::')) {
                $callback['function'] = explode('::', $callback['function']);
            }
            try {
                if (is_array($callback['function'])) {
                    if (is_object($callback['function'][0])) {
                        $class = get_class($callback['function'][0]);
                        $access = '->';
                    } else {
                        $class = $callback['function'][0];
                        $access = '::';
                    }
                    $callback['name'] = $class . $access . $callback['function'][1] . '()';
                    $ref = new ReflectionMethod($class, $callback['function'][1]);
                } else {
                    if (is_object($callback['function'])) {
                        if (is_a($callback['function'], 'Closure')) {
                            $ref = new ReflectionFunction($callback['function']);
                            $file = QM_Util::standard_dir($ref->getFileName(), '');
                            $callback['name'] = sprintf(__('Closure on line %1$d of %2$s', 'query-monitor'), $ref->getStartLine(), $file);
                        } else {
                            // the object should have a __invoke() method
                            $class = get_class($callback['function']);
                            $callback['name'] = $class . '->__invoke()';
                            $ref = new ReflectionMethod($class, '__invoke');
                        }
                    } else {
                        $callback['name'] = $callback['function'] . '()';
                        $ref = new ReflectionFunction($callback['function']);
                    }
                }
                $callback['file'] = $ref->getFileName();
                $callback['line'] = $ref->getStartLine();
                // https://github.com/facebook/hhvm/issues/5856
                $name = trim($ref->getName());
                if ('__lambda_func' === $name || 0 === strpos($name, 'lambda_')) {
                    if (preg_match('|(?P<file>.*)\\((?P<line>[0-9]+)\\)|', $callback['file'], $matches)) {
                        $callback['file'] = $matches['file'];
                        $callback['line'] = $matches['line'];
                        $file = trim(QM_Util::standard_dir($callback['file'], ''), '/');
                        $callback['name'] = sprintf(__('Anonymous function on line %1$d of %2$s', 'query-monitor'), $callback['line'], $file);
                    } else {
                        // https://github.com/facebook/hhvm/issues/5807
                        unset($callback['line'], $callback['file']);
                        $callback['name'] = $name . '()';
                        $callback['error'] = new WP_Error('unknown_lambda', __('Unable to determine source of lambda function', 'query-monitor'));
                    }
                }
                if (!empty($callback['file'])) {
                    $callback['component'] = self::get_file_component($callback['file']);
                }
            } catch (ReflectionException $e) {
                $callback['error'] = new WP_Error('reflection_exception', $e->getMessage());
            }
            return $callback;
        }

Usage Example

Beispiel #1
0
 protected function process_action($name, array $wp_filter)
 {
     $actions = $components = array();
     if (isset($wp_filter[$name])) {
         # http://core.trac.wordpress.org/ticket/17817
         $action = $wp_filter[$name];
         foreach ($action as $priority => $callbacks) {
             foreach ($callbacks as $callback) {
                 $callback = QM_Util::populate_callback($callback);
                 if (isset($callback['component'])) {
                     if ($this->hide_qm and 'query-monitor' === $callback['component']->context) {
                         continue;
                     }
                     $components[$callback['component']->name] = $callback['component']->name;
                 }
                 $actions[] = array('priority' => $priority, 'callback' => $callback);
             }
         }
     }
     $parts = array_filter(preg_split('#[_/-]#', $name));
     return array('name' => $name, 'actions' => $actions, 'parts' => $parts, 'components' => $components);
 }
All Usage Examples Of QM_Util::populate_callback