4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\VarDumper\Caster;
15 * Represents a PHP class identifier.
17 * @author Nicolas Grekas <p@tchwork.com>
19 class ClassStub extends ConstStub
22 * @param string A PHP identifier, e.g. a class, method, interface, etc. name
23 * @param callable The callable targeted by the identifier when it is ambiguous or not a real PHP identifier
25 public function __construct($identifier, $callable = null)
27 $this->value = $identifier;
29 if (0 < $i = strrpos($identifier, '\\')) {
30 $this->attr['ellipsis'] = \strlen($identifier) - $i;
31 $this->attr['ellipsis-type'] = 'class';
32 $this->attr['ellipsis-tail'] = 1;
36 if (null !== $callable) {
37 if ($callable instanceof \Closure) {
38 $r = new \ReflectionFunction($callable);
39 } elseif (\is_object($callable)) {
40 $r = array($callable, '__invoke');
41 } elseif (\is_array($callable)) {
43 } elseif (false !== $i = strpos($callable, '::')) {
44 $r = array(substr($callable, 0, $i), substr($callable, 2 + $i));
46 $r = new \ReflectionFunction($callable);
48 } elseif (0 < $i = strpos($identifier, '::') ?: strpos($identifier, '->')) {
49 $r = array(substr($identifier, 0, $i), substr($identifier, 2 + $i));
51 $r = new \ReflectionClass($identifier);
56 $r = new \ReflectionMethod($r[0], $r[1]);
57 } catch (\ReflectionException $e) {
58 $r = new \ReflectionClass($r[0]);
61 } catch (\ReflectionException $e) {
65 if ($f = $r->getFileName()) {
66 $this->attr['file'] = $f;
67 $this->attr['line'] = $r->getStartLine();
71 public static function wrapCallable($callable)
73 if (\is_object($callable) || !\is_callable($callable)) {
77 if (!\is_array($callable)) {
78 $callable = new static($callable);
79 } elseif (\is_string($callable[0])) {
80 $callable[0] = new static($callable[0]);
82 $callable[1] = new static($callable[1], $callable);