| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of Twig. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
namespace ElementorDeps\Twig\Util; |
| 12 |
|
| 13 |
/** |
| 14 |
* @author Fabien Potencier <fabien@symfony.com> |
| 15 |
* |
| 16 |
* @internal |
| 17 |
*/ |
| 18 |
final class ReflectionCallable |
| 19 |
{ |
| 20 |
private $reflector; |
| 21 |
private $callable = null; |
| 22 |
private $name; |
| 23 |
public function __construct($callable, string $debugType = 'unknown', string $debugName = 'unknown') |
| 24 |
{ |
| 25 |
if (\is_string($callable) && \false !== ($pos = \strpos($callable, '::'))) { |
| 26 |
$callable = [\substr($callable, 0, $pos), \substr($callable, 2 + $pos)]; |
| 27 |
} |
| 28 |
if (\is_array($callable) && \method_exists($callable[0], $callable[1])) { |
| 29 |
$this->reflector = $r = new \ReflectionMethod($callable[0], $callable[1]); |
| 30 |
$this->callable = $callable; |
| 31 |
$this->name = $r->class . '::' . $r->name; |
| 32 |
return; |
| 33 |
} |
| 34 |
$checkVisibility = $callable instanceof \Closure; |
| 35 |
try { |
| 36 |
$closure = \Closure::fromCallable($callable); |
| 37 |
} catch (\TypeError $e) { |
| 38 |
throw new \LogicException(\sprintf('Callback for %s "%s" is not callable in the current scope.', $debugType, $debugName), 0, $e); |
| 39 |
} |
| 40 |
$this->reflector = $r = new \ReflectionFunction($closure); |
| 41 |
if (\str_contains($r->name, '{closure')) { |
| 42 |
$this->callable = $callable; |
| 43 |
$this->name = 'Closure'; |
| 44 |
return; |
| 45 |
} |
| 46 |
if ($object = $r->getClosureThis()) { |
| 47 |
$callable = [$object, $r->name]; |
| 48 |
$this->name = \get_debug_type($object) . '::' . $r->name; |
| 49 |
} elseif (\PHP_VERSION_ID >= 80111 && ($class = $r->getClosureCalledClass())) { |
| 50 |
$callable = [$class->name, $r->name]; |
| 51 |
$this->name = $class->name . '::' . $r->name; |
| 52 |
} elseif (\PHP_VERSION_ID < 80111 && ($class = $r->getClosureScopeClass())) { |
| 53 |
$callable = [\is_array($callable) ? $callable[0] : $class->name, $r->name]; |
| 54 |
$this->name = (\is_array($callable) ? $callable[0] : $class->name) . '::' . $r->name; |
| 55 |
} else { |
| 56 |
$callable = $this->name = $r->name; |
| 57 |
} |
| 58 |
if ($checkVisibility && \is_array($callable) && \method_exists(...$callable) && !(new \ReflectionMethod(...$callable))->isPublic()) { |
| 59 |
$callable = $r->getClosure(); |
| 60 |
} |
| 61 |
$this->callable = $callable; |
| 62 |
} |
| 63 |
public function getReflector() : \ReflectionFunctionAbstract |
| 64 |
{ |
| 65 |
return $this->reflector; |
| 66 |
} |
| 67 |
public function getCallable() |
| 68 |
{ |
| 69 |
return $this->callable; |
| 70 |
} |
| 71 |
public function getName() : string |
| 72 |
{ |
| 73 |
return $this->name; |
| 74 |
} |
| 75 |
} |
| 76 |
|