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