-
-
Notifications
You must be signed in to change notification settings - Fork 144
[8.4] Add polyfill for ReflectionConstant
#524
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DanielEScherzer
wants to merge
1
commit into
symfony:1.x
Choose a base branch
from
DanielEScherzer:ReflectionConstant
base: 1.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+461
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
/** | ||
* @author Daniel Scherzer <[email protected]> | ||
*/ | ||
if (\PHP_VERSION_ID < 80400) { | ||
final class ReflectionConstant { | ||
|
||
/** | ||
* @var string | ||
* @readonly | ||
*/ | ||
public $name; | ||
|
||
private $value; | ||
|
||
/** @var bool */ | ||
private $deprecated; | ||
|
||
/** @var bool */ | ||
private $persistent; | ||
|
||
public function __construct(string $name) | ||
{ | ||
if (!defined($name)) { | ||
throw new ReflectionException("Constant \"$name\" does not exist"); | ||
} | ||
// ReflectionConstant cannot be used for class constants; constants | ||
// with 2 `:` or more in a row are prohibited via define() | ||
if (strpos($name, '::') !== false) { | ||
throw new ReflectionException("Constant \"$name\" does not exist"); | ||
} | ||
$this->name = $name; | ||
$deprecated = false; | ||
$old = set_error_handler( | ||
static function ( | ||
int $errno, | ||
string $errstr | ||
) use ($name, &$deprecated) { | ||
if ($errno === E_DEPRECATED | ||
&& $errstr === "Constant $name is deprecated" | ||
) { | ||
$deprecated = true; | ||
} | ||
return true; | ||
} | ||
); | ||
$this->value = constant($name); | ||
$this->deprecated = $deprecated; | ||
set_error_handler($old); | ||
|
||
// A constant is persistent if provided by PHP itself rather than | ||
// being defined by users. If we got here, we know that it *is* | ||
// defined, so we just need to figure out if it is defined by the | ||
// user or not | ||
$allConstants = get_defined_constants(true); | ||
$userConstants = $allConstants['user'] ?? []; | ||
$key = ltrim($this->name, '\\'); | ||
$this->persistent = !array_key_exists($key, $userConstants); | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return ltrim($this->name, '\\'); | ||
} | ||
|
||
public function getValue() | ||
{ | ||
return $this->value; | ||
} | ||
|
||
public function getNamespaceName(): string | ||
{ | ||
$slashPos = strrpos($this->name, '\\'); | ||
if ($slashPos === false) { | ||
return ''; | ||
} | ||
return substr($this->name, 1, $slashPos - 1); | ||
} | ||
|
||
public function getShortName(): string | ||
{ | ||
$slashPos = strrpos($this->name, '\\'); | ||
if ($slashPos === false) { | ||
return $this->name; | ||
} | ||
return substr($this->name, $slashPos + 1); | ||
} | ||
|
||
public function isDeprecated(): bool | ||
{ | ||
return $this->deprecated; | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
// Can't match the inclusion of `no_file_cache` but the rest is | ||
// possible to match | ||
$result = 'Constant [ '; | ||
if ($this->persistent || $this->deprecated) { | ||
$result .= '<'; | ||
if ($this->persistent) { | ||
$result .= 'persistent'; | ||
if ($this->deprecated) { | ||
$result .= ', '; | ||
} | ||
} | ||
if ($this->deprecated) { | ||
$result .= 'deprecated'; | ||
} | ||
$result .= '> '; | ||
} | ||
// Cannot just use gettype() to match zend_zval_type_name() | ||
if (is_object($this->value)) { | ||
$result .= get_class($this->value); | ||
} elseif (is_bool($this->value)) { | ||
$result .= 'bool'; | ||
} elseif (is_int($this->value)) { | ||
$result .= 'int'; | ||
} elseif (is_float($this->value)) { | ||
$result .= 'float'; | ||
} elseif (is_string($this->value)) { | ||
$result .= 'string'; | ||
} elseif (is_resource($this->value)) { | ||
$result .= 'resource'; | ||
} elseif ($this->value === null) { | ||
$result .= 'null'; | ||
} else { | ||
// Reasonable fallback | ||
$result .= gettype($this->value); | ||
} | ||
$result .= ' '; | ||
$result .= $this->getName(); | ||
$result .= ' ] { '; | ||
if (is_array($this->value)) { | ||
$result .= 'Array'; | ||
} else { | ||
// This will throw an exception if the value is an object that | ||
// cannot be converted to string; that is expected and matches | ||
// the behavior of zval_get_string_func() | ||
$result .= (string)$this->value; | ||
} | ||
$result .= " }\n"; | ||
return $result; | ||
} | ||
|
||
public function __sleep(): array | ||
{ | ||
throw new Exception("Serialization of 'ReflectionConstant' is not allowed"); | ||
} | ||
|
||
public function __wakeup(): void | ||
{ | ||
throw new Exception("Unserialization of 'ReflectionConstant' is not allowed"); | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.