Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\VarDumper\Caster; Chris@0: Chris@0: /** Chris@0: * Represents a PHP class identifier. Chris@0: * Chris@0: * @author Nicolas Grekas Chris@0: */ Chris@0: class ClassStub extends ConstStub Chris@0: { Chris@0: /** Chris@17: * @param string $identifier A PHP identifier, e.g. a class, method, interface, etc. name Chris@17: * @param callable $callable The callable targeted by the identifier when it is ambiguous or not a real PHP identifier Chris@0: */ Chris@0: public function __construct($identifier, $callable = null) Chris@0: { Chris@0: $this->value = $identifier; Chris@0: Chris@0: if (0 < $i = strrpos($identifier, '\\')) { Chris@17: $this->attr['ellipsis'] = \strlen($identifier) - $i; Chris@0: $this->attr['ellipsis-type'] = 'class'; Chris@0: $this->attr['ellipsis-tail'] = 1; Chris@0: } Chris@0: Chris@0: try { Chris@0: if (null !== $callable) { Chris@0: if ($callable instanceof \Closure) { Chris@0: $r = new \ReflectionFunction($callable); Chris@17: } elseif (\is_object($callable)) { Chris@17: $r = [$callable, '__invoke']; Chris@17: } elseif (\is_array($callable)) { Chris@0: $r = $callable; Chris@0: } elseif (false !== $i = strpos($callable, '::')) { Chris@17: $r = [substr($callable, 0, $i), substr($callable, 2 + $i)]; Chris@0: } else { Chris@0: $r = new \ReflectionFunction($callable); Chris@0: } Chris@0: } elseif (0 < $i = strpos($identifier, '::') ?: strpos($identifier, '->')) { Chris@17: $r = [substr($identifier, 0, $i), substr($identifier, 2 + $i)]; Chris@0: } else { Chris@0: $r = new \ReflectionClass($identifier); Chris@0: } Chris@0: Chris@17: if (\is_array($r)) { Chris@0: try { Chris@0: $r = new \ReflectionMethod($r[0], $r[1]); Chris@0: } catch (\ReflectionException $e) { Chris@0: $r = new \ReflectionClass($r[0]); Chris@0: } Chris@0: } Chris@0: } catch (\ReflectionException $e) { Chris@0: return; Chris@0: } Chris@0: Chris@0: if ($f = $r->getFileName()) { Chris@0: $this->attr['file'] = $f; Chris@0: $this->attr['line'] = $r->getStartLine(); Chris@0: } Chris@0: } Chris@0: Chris@0: public static function wrapCallable($callable) Chris@0: { Chris@17: if (\is_object($callable) || !\is_callable($callable)) { Chris@0: return $callable; Chris@0: } Chris@0: Chris@17: if (!\is_array($callable)) { Chris@0: $callable = new static($callable); Chris@17: } elseif (\is_string($callable[0])) { Chris@0: $callable[0] = new static($callable[0]); Chris@0: } else { Chris@0: $callable[1] = new static($callable[1], $callable); Chris@0: } Chris@0: Chris@0: return $callable; Chris@0: } Chris@0: }