Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | 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
 *
200 lars 6
 * (c) 2012-2023 Justin Hileman
148 lars 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\Reflection;
13
 
14
/**
15
 * Somehow the standard reflection library didn't include class constants until 7.1.
16
 *
17
 * ReflectionClassConstant corrects that omission.
18
 */
19
class ReflectionClassConstant implements \Reflector
20
{
21
    public $class;
22
    public $name;
23
    private $value;
24
 
25
    /**
26
     * Construct a ReflectionClassConstant object.
27
     *
28
     * @param string|object $class
29
     * @param string        $name
30
     */
31
    public function __construct($class, string $name)
32
    {
33
        if (!$class instanceof \ReflectionClass) {
34
            $class = new \ReflectionClass($class);
35
        }
36
 
37
        $this->class = $class;
38
        $this->name = $name;
39
 
40
        $constants = $class->getConstants();
41
        if (!\array_key_exists($name, $constants)) {
42
            throw new \InvalidArgumentException('Unknown constant: '.$name);
43
        }
44
 
45
        $this->value = $constants[$name];
46
    }
47
 
48
    /**
49
     * Exports a reflection.
50
     *
51
     * @param string|object $class
52
     * @param string        $name
53
     * @param bool          $return pass true to return the export, as opposed to emitting it
54
     *
55
     * @return string|null
56
     */
57
    public static function export($class, string $name, bool $return = false)
58
    {
59
        $refl = new self($class, $name);
60
        $value = $refl->getValue();
61
 
62
        $str = \sprintf('Constant [ public %s %s ] { %s }', \gettype($value), $refl->getName(), $value);
63
 
64
        if ($return) {
65
            return $str;
66
        }
67
 
68
        echo $str."\n";
69
    }
70
 
71
    /**
72
     * Gets the declaring class.
73
     */
74
    public function getDeclaringClass(): \ReflectionClass
75
    {
76
        $parent = $this->class;
77
 
78
        // Since we don't have real reflection constants, we can't see where
79
        // it's actually defined. Let's check for a constant that is also
80
        // available on the parent class which has exactly the same value.
81
        //
82
        // While this isn't _technically_ correct, it's prolly close enough.
83
        do {
84
            $class = $parent;
85
            $parent = $class->getParentClass();
86
        } while ($parent && $parent->hasConstant($this->name) && $parent->getConstant($this->name) === $this->value);
87
 
88
        return $class;
89
    }
90
 
91
    /**
92
     * Get the constant's docblock.
93
     *
94
     * @return false
95
     */
96
    public function getDocComment(): bool
97
    {
98
        return false;
99
    }
100
 
101
    /**
102
     * Gets the class constant modifiers.
103
     *
104
     * Since this is only used for PHP < 7.1, we can just return "public". All
105
     * the fancier modifiers are only available on PHP versions which have their
106
     * own ReflectionClassConstant class :)
107
     */
108
    public function getModifiers(): int
109
    {
110
        return \ReflectionMethod::IS_PUBLIC;
111
    }
112
 
113
    /**
114
     * Gets the constant name.
115
     */
116
    public function getName(): string
117
    {
118
        return $this->name;
119
    }
120
 
121
    /**
122
     * Gets the value of the constant.
123
     *
124
     * @return mixed
125
     */
126
    public function getValue()
127
    {
128
        return $this->value;
129
    }
130
 
131
    /**
132
     * Checks if class constant is private.
133
     *
134
     * @return bool false
135
     */
136
    public function isPrivate(): bool
137
    {
138
        return false;
139
    }
140
 
141
    /**
142
     * Checks if class constant is protected.
143
     *
144
     * @return bool false
145
     */
146
    public function isProtected(): bool
147
    {
148
        return false;
149
    }
150
 
151
    /**
152
     * Checks if class constant is public.
153
     *
154
     * @return bool true
155
     */
156
    public function isPublic(): bool
157
    {
158
        return true;
159
    }
160
 
161
    /**
162
     * To string.
163
     */
164
    public function __toString(): string
165
    {
166
        return $this->getName();
167
    }
168
 
169
    /**
170
     * Gets the constant's file name.
171
     *
172
     * Currently returns null, because if it returns a file name the signature
173
     * formatter will barf.
174
     */
175
    public function getFileName()
176
    {
177
        return;
178
        // return $this->class->getFileName();
179
    }
180
 
181
    /**
182
     * Get the code start line.
183
     *
184
     * @throws \RuntimeException
185
     */
186
    public function getStartLine()
187
    {
188
        throw new \RuntimeException('Not yet implemented because it\'s unclear what I should do here :)');
189
    }
190
 
191
    /**
192
     * Get the code end line.
193
     *
194
     * @throws \RuntimeException
195
     */
196
    public function getEndLine()
197
    {
198
        return $this->getStartLine();
199
    }
200
 
201
    /**
202
     * Get a ReflectionClassConstant instance.
203
     *
204
     * In PHP >= 7.1, this will return a \ReflectionClassConstant from the
205
     * standard reflection library. For older PHP, it will return this polyfill.
206
     *
207
     * @param string|object $class
208
     * @param string        $name
209
     *
210
     * @return ReflectionClassConstant|\ReflectionClassConstant
211
     */
212
    public static function create($class, string $name)
213
    {
214
        if (\class_exists(\ReflectionClassConstant::class)) {
215
            return new \ReflectionClassConstant($class, $name);
216
        }
217
 
218
        return new self($class, $name);
219
    }
220
}