|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 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 | +if (\PHP_VERSION_ID <80400) { |
| 13 | +/** |
| 14 | + * @author Daniel Scherzer <daniel.e.scherzer@gmail.com> |
| 15 | + */ |
| 16 | +finalclass ReflectionConstant |
| 17 | + { |
| 18 | +/** |
| 19 | + * @var string |
| 20 | + * |
| 21 | + * @readonly |
| 22 | + */ |
| 23 | +public$name; |
| 24 | + |
| 25 | +private$value; |
| 26 | +private$deprecated; |
| 27 | + |
| 28 | +privatestatic$persistentConstants = []; |
| 29 | + |
| 30 | +publicfunction__construct(string$name) |
| 31 | + { |
| 32 | +if (!defined($name) ||false !==strpos($name,'::')) { |
| 33 | +thrownewReflectionException("Constant\"$name\" does not exist"); |
| 34 | + } |
| 35 | + |
| 36 | +$this->name =ltrim($name,'\\'); |
| 37 | +$deprecated =false; |
| 38 | +$eh =set_error_handler(staticfunction ($type,$msg,$file,$line)use ($name, &$deprecated, &$eh) { |
| 39 | +if (\E_DEPRECATED ===$type &&"Constant$name is deprecated" ===$msg) { |
| 40 | +return$deprecated =true; |
| 41 | + } |
| 42 | + |
| 43 | +return$eh &&$eh($type,$msg,$file,$line); |
| 44 | + }); |
| 45 | + |
| 46 | +try { |
| 47 | +$this->value =constant($name); |
| 48 | +$this->deprecated =$deprecated; |
| 49 | + }finally { |
| 50 | +restore_error_handler(); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | +publicfunctiongetName():string |
| 55 | + { |
| 56 | +return$this->name; |
| 57 | + } |
| 58 | + |
| 59 | +publicfunctiongetValue() |
| 60 | + { |
| 61 | +return$this->value; |
| 62 | + } |
| 63 | + |
| 64 | +publicfunctiongetNamespaceName():string |
| 65 | + { |
| 66 | +if (false ===$slashPos =strrpos($this->name,'\\')) { |
| 67 | +return''; |
| 68 | + } |
| 69 | + |
| 70 | +returnsubstr($this->name,0,$slashPos); |
| 71 | + } |
| 72 | + |
| 73 | +publicfunctiongetShortName():string |
| 74 | + { |
| 75 | +if (false ===$slashPos =strrpos($this->name,'\\')) { |
| 76 | +return$this->name; |
| 77 | + } |
| 78 | + |
| 79 | +returnsubstr($this->name,$slashPos +1); |
| 80 | + } |
| 81 | + |
| 82 | +publicfunctionisDeprecated():bool |
| 83 | + { |
| 84 | +return$this->deprecated; |
| 85 | + } |
| 86 | + |
| 87 | +publicfunction__toString():string |
| 88 | + { |
| 89 | +// A constant is persistent if provided by PHP itself rather than |
| 90 | +// being defined by users. If we got here, we know that it *is* |
| 91 | +// defined, so we just need to figure out if it is defined by the |
| 92 | +// user or not |
| 93 | +if (!self::$persistentConstants) { |
| 94 | +$persistentConstants =get_defined_constants(true); |
| 95 | + unset($persistentConstants['user']); |
| 96 | +foreach ($persistentConstantsas$constants) { |
| 97 | +self::$persistentConstants +=$constants; |
| 98 | + } |
| 99 | + } |
| 100 | +$persistent =array_key_exists($this->name,self::$persistentConstants); |
| 101 | + |
| 102 | +// Can't match the inclusion of `no_file_cache` but the rest is |
| 103 | +// possible to match |
| 104 | +$result ='Constant ['; |
| 105 | +if ($persistent ||$this->deprecated) { |
| 106 | +$result .='<'; |
| 107 | +if ($persistent) { |
| 108 | +$result .='persistent'; |
| 109 | +if ($this->deprecated) { |
| 110 | +$result .=','; |
| 111 | + } |
| 112 | + } |
| 113 | +if ($this->deprecated) { |
| 114 | +$result .='deprecated'; |
| 115 | + } |
| 116 | +$result .='>'; |
| 117 | + } |
| 118 | +// Cannot just use gettype() to match zend_zval_type_name() |
| 119 | +if (is_object($this->value)) { |
| 120 | +$result .= \PHP_VERSION_ID >=80000 ?get_debug_type($this->value) :gettype($this->value); |
| 121 | + }elseif (is_bool($this->value)) { |
| 122 | +$result .='bool'; |
| 123 | + }elseif (is_int($this->value)) { |
| 124 | +$result .='int'; |
| 125 | + }elseif (is_float($this->value)) { |
| 126 | +$result .='float'; |
| 127 | + }elseif (null ===$this->value) { |
| 128 | +$result .='null'; |
| 129 | + }else { |
| 130 | +$result .=gettype($this->value); |
| 131 | + } |
| 132 | +$result .=''; |
| 133 | +$result .=$this->name; |
| 134 | +$result .=' ] {'; |
| 135 | +if (is_array($this->value)) { |
| 136 | +$result .='Array'; |
| 137 | + }else { |
| 138 | +// This will throw an exception if the value is an object that |
| 139 | +// cannot be converted to string; that is expected and matches |
| 140 | +// the behavior of zval_get_string_func() |
| 141 | +$result .= (string)$this->value; |
| 142 | + } |
| 143 | +$result .=" }\n"; |
| 144 | + |
| 145 | +return$result; |
| 146 | + } |
| 147 | + |
| 148 | +publicfunction__sleep():array |
| 149 | + { |
| 150 | +thrownewException("Serialization of 'ReflectionConstant' is not allowed"); |
| 151 | + } |
| 152 | + |
| 153 | +publicfunction__wakeup():void |
| 154 | + { |
| 155 | +thrownewException("Unserialization of 'ReflectionConstant' is not allowed"); |
| 156 | + } |
| 157 | + } |
| 158 | +} |