Heimdall/vendor/symfony/var-dumper/Caster/ClassStub.php

107 lines
3.6 KiB
PHP
Raw Normal View History

2018-02-01 20:01:12 +00:00
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\VarDumper\Caster;
2019-01-18 18:33:28 +00:00
use Symfony\Component\VarDumper\Cloner\Stub;
2018-02-01 20:01:12 +00:00
/**
* Represents a PHP class identifier.
*
* @author Nicolas Grekas <p@tchwork.com>
*/
class ClassStub extends ConstStub
{
/**
2019-01-18 18:33:28 +00:00
* @param string $identifier A PHP identifier, e.g. a class, method, interface, etc. name
* @param callable $callable The callable targeted by the identifier when it is ambiguous or not a real PHP identifier
2018-02-01 20:01:12 +00:00
*/
public function __construct(string $identifier, $callable = null)
2018-02-01 20:01:12 +00:00
{
$this->value = $identifier;
try {
if (null !== $callable) {
if ($callable instanceof \Closure) {
$r = new \ReflectionFunction($callable);
} elseif (\is_object($callable)) {
2019-06-11 11:29:32 +00:00
$r = [$callable, '__invoke'];
} elseif (\is_array($callable)) {
2018-02-01 20:01:12 +00:00
$r = $callable;
} elseif (false !== $i = strpos($callable, '::')) {
2019-06-11 11:29:32 +00:00
$r = [substr($callable, 0, $i), substr($callable, 2 + $i)];
2018-02-01 20:01:12 +00:00
} else {
$r = new \ReflectionFunction($callable);
}
} elseif (0 < $i = strpos($identifier, '::') ?: strpos($identifier, '->')) {
2019-06-11 11:29:32 +00:00
$r = [substr($identifier, 0, $i), substr($identifier, 2 + $i)];
2018-02-01 20:01:12 +00:00
} else {
$r = new \ReflectionClass($identifier);
}
if (\is_array($r)) {
2018-02-01 20:01:12 +00:00
try {
$r = new \ReflectionMethod($r[0], $r[1]);
} catch (\ReflectionException $e) {
$r = new \ReflectionClass($r[0]);
}
}
2019-01-18 18:33:28 +00:00
if (false !== strpos($identifier, "class@anonymous\0")) {
$this->value = $identifier = preg_replace_callback('/class@anonymous\x00.*?\.php0x?[0-9a-fA-F]++/', function ($m) {
return \class_exists($m[0], false) ? get_parent_class($m[0]).'@anonymous' : $m[0];
}, $identifier);
}
if (null !== $callable && $r instanceof \ReflectionFunctionAbstract) {
2019-06-11 11:29:32 +00:00
$s = ReflectionCaster::castFunctionAbstract($r, [], new Stub(), true);
2019-01-18 18:33:28 +00:00
$s = ReflectionCaster::getSignature($s);
if ('()' === substr($identifier, -2)) {
$this->value = substr_replace($identifier, $s, -2);
} else {
$this->value .= $s;
}
}
2018-02-01 20:01:12 +00:00
} catch (\ReflectionException $e) {
return;
2019-01-18 18:33:28 +00:00
} finally {
if (0 < $i = strrpos($this->value, '\\')) {
$this->attr['ellipsis'] = \strlen($this->value) - $i;
$this->attr['ellipsis-type'] = 'class';
$this->attr['ellipsis-tail'] = 1;
}
2018-02-01 20:01:12 +00:00
}
if ($f = $r->getFileName()) {
$this->attr['file'] = $f;
$this->attr['line'] = $r->getStartLine();
}
}
public static function wrapCallable($callable)
{
if (\is_object($callable) || !\is_callable($callable)) {
2018-02-01 20:01:12 +00:00
return $callable;
}
if (!\is_array($callable)) {
2019-01-18 18:33:28 +00:00
$callable = new static($callable, $callable);
} elseif (\is_string($callable[0])) {
2019-01-18 18:33:28 +00:00
$callable[0] = new static($callable[0], $callable);
2018-02-01 20:01:12 +00:00
} else {
$callable[1] = new static($callable[1], $callable);
}
return $callable;
}
}