Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen Revision | Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/*
4
 * This file is part of Psy Shell.
5
 *
6
 * (c) 2012-2022 Justin Hileman
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
 
12
namespace Psy\Util;
13
 
14
use Psy\Exception\RuntimeException;
15
use Psy\Reflection\ReflectionClassConstant;
16
use Psy\Reflection\ReflectionConstant_;
17
use Psy\Reflection\ReflectionNamespace;
18
 
19
/**
20
 * A utility class for getting Reflectors.
21
 */
22
class Mirror
23
{
24
    const CONSTANT = 1;
25
    const METHOD = 2;
26
    const STATIC_PROPERTY = 4;
27
    const PROPERTY = 8;
28
 
29
    /**
30
     * Get a Reflector for a function, class or instance, constant, method or property.
31
     *
32
     * Optionally, pass a $filter param to restrict the types of members checked. For example, to only Reflectors for
33
     * static properties and constants, pass:
34
     *
35
     *    $filter = Mirror::CONSTANT | Mirror::STATIC_PROPERTY
36
     *
37
     * @throws \Psy\Exception\RuntimeException when a $member specified but not present on $value
38
     * @throws \InvalidArgumentException       if $value is something other than an object or class/function name
39
     *
40
     * @param mixed  $value  Class or function name, or variable instance
41
     * @param string $member Optional: property, constant or method name (default: null)
42
     * @param int    $filter (default: CONSTANT | METHOD | PROPERTY | STATIC_PROPERTY)
43
     *
44
     * @return \Reflector
45
     */
46
    public static function get($value, string $member = null, int $filter = 15): \Reflector
47
    {
48
        if ($member === null && \is_string($value)) {
49
            if (\function_exists($value)) {
50
                return new \ReflectionFunction($value);
51
            } elseif (\defined($value) || ReflectionConstant_::isMagicConstant($value)) {
52
                return new ReflectionConstant_($value);
53
            }
54
        }
55
 
56
        $class = self::getClass($value);
57
 
58
        if ($member === null) {
59
            return $class;
60
        } elseif ($filter & self::CONSTANT && $class->hasConstant($member)) {
61
            return ReflectionClassConstant::create($value, $member);
62
        } elseif ($filter & self::METHOD && $class->hasMethod($member)) {
63
            return $class->getMethod($member);
64
        } elseif ($filter & self::PROPERTY && $class->hasProperty($member)) {
65
            return $class->getProperty($member);
66
        } elseif ($filter & self::STATIC_PROPERTY && $class->hasProperty($member) && $class->getProperty($member)->isStatic()) {
67
            return $class->getProperty($member);
68
        } else {
69
            throw new RuntimeException(\sprintf('Unknown member %s on class %s', $member, \is_object($value) ? \get_class($value) : $value));
70
        }
71
    }
72
 
73
    /**
74
     * Get a ReflectionClass (or ReflectionObject, or ReflectionNamespace) if possible.
75
     *
76
     * @throws \InvalidArgumentException if $value is not a namespace or class name or instance
77
     *
78
     * @param mixed $value
79
     *
80
     * @return \ReflectionClass|ReflectionNamespace
81
     */
82
    private static function getClass($value)
83
    {
84
        if (\is_object($value)) {
85
            return new \ReflectionObject($value);
86
        }
87
 
88
        if (!\is_string($value)) {
89
            throw new \InvalidArgumentException('Mirror expects an object or class');
90
        }
91
 
92
        if (\class_exists($value) || \interface_exists($value) || \trait_exists($value)) {
93
            return new \ReflectionClass($value);
94
        }
95
 
96
        $namespace = \preg_replace('/(^\\\\|\\\\$)/', '', $value);
97
        if (self::namespaceExists($namespace)) {
98
            return new ReflectionNamespace($namespace);
99
        }
100
 
101
        throw new \InvalidArgumentException('Unknown namespace, class or function: '.$value);
102
    }
103
 
104
    /**
105
     * Check declared namespaces for a given namespace.
106
     */
107
    private static function namespaceExists(string $value): bool
108
    {
109
        return \in_array(\strtolower($value), self::getDeclaredNamespaces());
110
    }
111
 
112
    /**
113
     * Get an array of all currently declared namespaces.
114
     *
115
     * Note that this relies on at least one function, class, interface, trait
116
     * or constant to have been declared in that namespace.
117
     */
118
    private static function getDeclaredNamespaces(): array
119
    {
120
        $functions = \get_defined_functions();
121
 
122
        $allNames = \array_merge(
123
            $functions['internal'],
124
            $functions['user'],
125
            \get_declared_classes(),
126
            \get_declared_interfaces(),
127
            \get_declared_traits(),
128
            \array_keys(\get_defined_constants())
129
        );
130
 
131
        $namespaces = [];
132
        foreach ($allNames as $name) {
133
            $chunks = \explode('\\', \strtolower($name));
134
 
135
            // the last one is the function or class or whatever...
136
            \array_pop($chunks);
137
 
138
            while (!empty($chunks)) {
139
                $namespaces[\implode('\\', $chunks)] = true;
140
                \array_pop($chunks);
141
            }
142
        }
143
 
144
        $namespaceNames = \array_keys($namespaces);
145
 
146
        \sort($namespaceNames);
147
 
148
        return $namespaceNames;
149
    }
150
}