Elgg  Version 4.3
HandlersService.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg;
4 
8 
18 
19  use Loggable;
20 
21  protected const CLASS_NAME_PATTERN_53 = '/^(\\\\?[a-z_\x7f-\xff][a-z0-9_\x7f-\xff]*)+$/i';
22 
32  public function call($callable, $object, $args) {
33  $original = $callable;
34 
35  $callable = $this->resolveCallable($callable);
36  if (!is_callable($callable)) {
37  $type = is_string($object) ? $object : $object::EVENT_TYPE;
38  $description = $type . " [{$args[0]}, {$args[1]}]";
39 
40  $this->getLogger()->warning("Handler for {$description} is not callable: " . $this->describeCallable($original));
41 
42  return [false, null, $object];
43  }
44 
45  if (is_string($object)) {
46  switch ($object) {
47  case 'hook' :
48  $object = new HrsHook(elgg(), $args[0], $args[1], $args[2], $args[3]);
49  break;
50 
51  case 'event' :
52  $object = new HrsEvent(elgg(), $args[0], $args[1], $args[2]);
53  break;
54 
55  case 'middleware' :
56  case 'controller' :
57  case 'action' :
58  $object = new Request(elgg(), $args[0]);
59  break;
60  }
61  }
62 
63  $result = call_user_func($callable, $object);
64 
65  return [true, $result, $object];
66  }
67 
77  public function isCallable($callback) {
78  $callback = $this->resolveCallable($callback);
79  return $callback && is_callable($callback);
80  }
81 
89  public function resolveCallable($callable) {
90  if (is_callable($callable)) {
91  return $callable;
92  }
93 
94  if (is_string($callable)
95  && preg_match(self::CLASS_NAME_PATTERN_53, $callable)
96  && class_exists($callable)) {
97  $callable = new $callable;
98  }
99 
100  return is_callable($callable) ? $callable : null;
101  }
102 
112  public function describeCallable($callable, $file_root = '') {
113  if (is_string($callable)) {
114  return $callable;
115  }
116  if (is_array($callable) && array_keys($callable) === [0, 1] && is_string($callable[1])) {
117  if (is_string($callable[0])) {
118  return "{$callable[0]}::{$callable[1]}";
119  }
120  return "(" . get_class($callable[0]) . ")->{$callable[1]}";
121  }
122  if ($callable instanceof \Closure) {
123  $ref = new \ReflectionFunction($callable);
124  $file = $ref->getFileName();
125  $line = $ref->getStartLine();
126 
127  if ($file_root && 0 === strpos($file, $file_root)) {
128  $file = substr($file, strlen($file_root));
129  }
130 
131  return "(Closure {$file}:{$line})";
132  }
133  if (is_object($callable)) {
134  return "(" . get_class($callable) . ")->__invoke()";
135  }
136  return print_r($callable, true);
137  }
138 }
isCallable($callback)
Test is callback is callable Unlike is_callable(), this function also tests invokable classes...
Helpers for providing callable-based APIs.
$type
Definition: delete.php:21
$args
Some servers don&#39;t allow PHP to check the rewrite, so try via AJAX.
trait Loggable
Enables adding a logger.
Definition: Loggable.php:14
The object passed to invokable class name handlers.
Definition: Event.php:12
The object passed to invokable class name handlers.
Definition: Hook.php:12
resolveCallable($callable)
Resolve a callable, possibly instantiating a class name.
$description
Definition: record.php:15
describeCallable($callable, $file_root= '')
Get a string description of a callback.
call($callable, $object, $args)
Call the handler with the hook/event object.
getLogger()
Returns logger.
Definition: Loggable.php:37
if($email instanceof\Elgg\Email) $object
Definition: body.php:24
Request container.
Definition: Request.php:12
var elgg
Definition: elgglib.js:4