annotate vendor/psy/psysh/src/Psy/Util/Mirror.php @ 0:4c8ae668cc8c

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