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 doesn't include constants.
16
 *
17
 * ReflectionConstant_ corrects that omission.
18
 *
19
 * Note: For backwards compatibility reasons, this class is named
20
 * ReflectionConstant_ rather than ReflectionConstant. It will be renamed in
21
 * v0.10.0.
22
 */
23
class ReflectionConstant_ implements \Reflector
24
{
25
    public $name;
26
    private $value;
27
 
28
    private static $magicConstants = [
29
        '__LINE__',
30
        '__FILE__',
31
        '__DIR__',
32
        '__FUNCTION__',
33
        '__CLASS__',
34
        '__TRAIT__',
35
        '__METHOD__',
36
        '__NAMESPACE__',
37
        '__COMPILER_HALT_OFFSET__',
38
    ];
39
 
40
    /**
41
     * Construct a ReflectionConstant_ object.
42
     *
43
     * @param string $name
44
     */
45
    public function __construct(string $name)
46
    {
47
        $this->name = $name;
48
 
49
        if (!\defined($name) && !self::isMagicConstant($name)) {
50
            throw new \InvalidArgumentException('Unknown constant: '.$name);
51
        }
52
 
53
        if (!self::isMagicConstant($name)) {
54
            $this->value = @\constant($name);
55
        }
56
    }
57
 
58
    /**
59
     * Exports a reflection.
60
     *
61
     * @param string $name
62
     * @param bool   $return pass true to return the export, as opposed to emitting it
63
     *
64
     * @return string|null
65
     */
66
    public static function export(string $name, bool $return = false)
67
    {
68
        $refl = new self($name);
69
        $value = $refl->getValue();
70
 
71
        $str = \sprintf('Constant [ %s %s ] { %s }', \gettype($value), $refl->getName(), $value);
72
 
73
        if ($return) {
74
            return $str;
75
        }
76
 
77
        echo $str."\n";
78
    }
79
 
80
    public static function isMagicConstant($name)
81
    {
82
        return \in_array($name, self::$magicConstants);
83
    }
84
 
85
    /**
86
     * Get the constant's docblock.
87
     *
88
     * @return false
89
     */
90
    public function getDocComment(): bool
91
    {
92
        return false;
93
    }
94
 
95
    /**
96
     * Gets the constant name.
97
     */
98
    public function getName(): string
99
    {
100
        return $this->name;
101
    }
102
 
103
    /**
104
     * Gets the namespace name.
105
     *
106
     * Returns '' when the constant is not namespaced.
107
     */
108
    public function getNamespaceName(): string
109
    {
110
        if (!$this->inNamespace()) {
111
            return '';
112
        }
113
 
114
        return \preg_replace('/\\\\[^\\\\]+$/', '', $this->name);
115
    }
116
 
117
    /**
118
     * Gets the value of the constant.
119
     *
120
     * @return mixed
121
     */
122
    public function getValue()
123
    {
124
        return $this->value;
125
    }
126
 
127
    /**
128
     * Checks if this constant is defined in a namespace.
129
     */
130
    public function inNamespace(): bool
131
    {
132
        return \strpos($this->name, '\\') !== false;
133
    }
134
 
135
    /**
136
     * To string.
137
     */
138
    public function __toString(): string
139
    {
140
        return $this->getName();
141
    }
142
 
143
    /**
144
     * Gets the constant's file name.
145
     *
146
     * Currently returns null, because if it returns a file name the signature
147
     * formatter will barf.
148
     */
149
    public function getFileName()
150
    {
151
        return;
152
        // return $this->class->getFileName();
153
    }
154
 
155
    /**
156
     * Get the code start line.
157
     *
158
     * @throws \RuntimeException
159
     */
160
    public function getStartLine()
161
    {
162
        throw new \RuntimeException('Not yet implemented because it\'s unclear what I should do here :)');
163
    }
164
 
165
    /**
166
     * Get the code end line.
167
     *
168
     * @throws \RuntimeException
169
     */
170
    public function getEndLine()
171
    {
172
        return $this->getStartLine();
173
    }
174
}