Chris@13
|
1 <?php
|
Chris@13
|
2
|
Chris@13
|
3 /*
|
Chris@13
|
4 * This file is part of Psy Shell.
|
Chris@13
|
5 *
|
Chris@13
|
6 * (c) 2012-2018 Justin Hileman
|
Chris@13
|
7 *
|
Chris@13
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@13
|
9 * file that was distributed with this source code.
|
Chris@13
|
10 */
|
Chris@13
|
11
|
Chris@13
|
12 namespace Psy\Util;
|
Chris@13
|
13
|
Chris@13
|
14 use Psy\Exception\RuntimeException;
|
Chris@16
|
15 use Psy\Reflection\ReflectionClassConstant;
|
Chris@16
|
16 use Psy\Reflection\ReflectionConstant_;
|
Chris@13
|
17
|
Chris@13
|
18 /**
|
Chris@13
|
19 * A utility class for getting Reflectors.
|
Chris@13
|
20 */
|
Chris@13
|
21 class Mirror
|
Chris@13
|
22 {
|
Chris@13
|
23 const CONSTANT = 1;
|
Chris@13
|
24 const METHOD = 2;
|
Chris@13
|
25 const STATIC_PROPERTY = 4;
|
Chris@13
|
26 const PROPERTY = 8;
|
Chris@13
|
27
|
Chris@13
|
28 /**
|
Chris@13
|
29 * Get a Reflector for a function, class or instance, constant, method or property.
|
Chris@13
|
30 *
|
Chris@13
|
31 * Optionally, pass a $filter param to restrict the types of members checked. For example, to only Reflectors for
|
Chris@13
|
32 * static properties and constants, pass:
|
Chris@13
|
33 *
|
Chris@13
|
34 * $filter = Mirror::CONSTANT | Mirror::STATIC_PROPERTY
|
Chris@13
|
35 *
|
Chris@13
|
36 * @throws \Psy\Exception\RuntimeException when a $member specified but not present on $value
|
Chris@13
|
37 * @throws \InvalidArgumentException if $value is something other than an object or class/function name
|
Chris@13
|
38 *
|
Chris@13
|
39 * @param mixed $value Class or function name, or variable instance
|
Chris@13
|
40 * @param string $member Optional: property, constant or method name (default: null)
|
Chris@13
|
41 * @param int $filter (default: CONSTANT | METHOD | PROPERTY | STATIC_PROPERTY)
|
Chris@13
|
42 *
|
Chris@13
|
43 * @return \Reflector
|
Chris@13
|
44 */
|
Chris@13
|
45 public static function get($value, $member = null, $filter = 15)
|
Chris@13
|
46 {
|
Chris@17
|
47 if ($member === null && \is_string($value)) {
|
Chris@17
|
48 if (\function_exists($value)) {
|
Chris@16
|
49 return new \ReflectionFunction($value);
|
Chris@17
|
50 } elseif (\defined($value) || ReflectionConstant_::isMagicConstant($value)) {
|
Chris@16
|
51 return new ReflectionConstant_($value);
|
Chris@16
|
52 }
|
Chris@13
|
53 }
|
Chris@13
|
54
|
Chris@13
|
55 $class = self::getClass($value);
|
Chris@13
|
56
|
Chris@13
|
57 if ($member === null) {
|
Chris@13
|
58 return $class;
|
Chris@13
|
59 } elseif ($filter & self::CONSTANT && $class->hasConstant($member)) {
|
Chris@16
|
60 return ReflectionClassConstant::create($value, $member);
|
Chris@13
|
61 } elseif ($filter & self::METHOD && $class->hasMethod($member)) {
|
Chris@13
|
62 return $class->getMethod($member);
|
Chris@13
|
63 } elseif ($filter & self::PROPERTY && $class->hasProperty($member)) {
|
Chris@13
|
64 return $class->getProperty($member);
|
Chris@13
|
65 } elseif ($filter & self::STATIC_PROPERTY && $class->hasProperty($member) && $class->getProperty($member)->isStatic()) {
|
Chris@13
|
66 return $class->getProperty($member);
|
Chris@13
|
67 } else {
|
Chris@17
|
68 throw new RuntimeException(\sprintf(
|
Chris@13
|
69 'Unknown member %s on class %s',
|
Chris@13
|
70 $member,
|
Chris@17
|
71 \is_object($value) ? \get_class($value) : $value
|
Chris@13
|
72 ));
|
Chris@13
|
73 }
|
Chris@13
|
74 }
|
Chris@13
|
75
|
Chris@13
|
76 /**
|
Chris@13
|
77 * Get a ReflectionClass (or ReflectionObject) if possible.
|
Chris@13
|
78 *
|
Chris@13
|
79 * @throws \InvalidArgumentException if $value is not a class name or instance
|
Chris@13
|
80 *
|
Chris@13
|
81 * @param mixed $value
|
Chris@13
|
82 *
|
Chris@13
|
83 * @return \ReflectionClass
|
Chris@13
|
84 */
|
Chris@13
|
85 private static function getClass($value)
|
Chris@13
|
86 {
|
Chris@17
|
87 if (\is_object($value)) {
|
Chris@13
|
88 return new \ReflectionObject($value);
|
Chris@13
|
89 }
|
Chris@13
|
90
|
Chris@17
|
91 if (!\is_string($value)) {
|
Chris@13
|
92 throw new \InvalidArgumentException('Mirror expects an object or class');
|
Chris@17
|
93 } elseif (!\class_exists($value) && !\interface_exists($value) && !\trait_exists($value)) {
|
Chris@13
|
94 throw new \InvalidArgumentException('Unknown class or function: ' . $value);
|
Chris@13
|
95 }
|
Chris@13
|
96
|
Chris@13
|
97 return new \ReflectionClass($value);
|
Chris@13
|
98 }
|
Chris@13
|
99 }
|