add support for the class constant on objects

This commit is contained in:
Kévin Dunglas
2021-06-01 23:50:36 +02:00
committed by Fabien Potencier
parent 59d5260c33
commit 0d6bd45844
4 changed files with 17 additions and 0 deletions
+1
View File
@@ -1,6 +1,7 @@
# 3.3.9 (2022-XX-XX)
* Fix custom escapers when using multiple Twig environments
* Add support for "constant('class', object)"
# 3.3.8 (2022-02-04)
+6
View File
@@ -14,6 +14,12 @@ You can read constants from object instances as well:
{{ constant('RSS', date) }}
Retrieve the fully qualified class name of an object:
.. code-block:: twig
{{ constant('class', date) }}
Use the ``defined`` test to check if a constant is defined:
.. code-block:: twig
+8
View File
@@ -1359,6 +1359,10 @@ function twig_source(Environment $env, $name, $ignoreMissing = false)
function twig_constant($constant, $object = null)
{
if (null !== $object) {
if ('class' === $constant) {
return \get_class($object);
}
$constant = \get_class($object).'::'.$constant;
}
@@ -1376,6 +1380,10 @@ function twig_constant($constant, $object = null)
function twig_constant_is_defined($constant, $object = null)
{
if (null !== $object) {
if ('class' === $constant) {
return true;
}
$constant = \get_class($object).'::'.$constant;
}
+2
View File
@@ -3,8 +3,10 @@
--TEMPLATE--
{{ constant('DATE_W3C') == expect ? 'true' : 'false' }}
{{ constant('ARRAY_AS_PROPS', object) }}
{{ constant('class', object) }}
--DATA--
return ['expect' => DATE_W3C, 'object' => new \ArrayObject(['hi'])]
--EXPECT--
true
2
ArrayObject