Permalink
Browse files

feature #2242 added 'is defined' support for constant (fabpot)

This PR was merged into the 1.x branch.

Discussion
----------

added 'is defined' support for constant

Commits
-------

c81bae4 added 'is defined' support for constant
  • Loading branch information...
fabpot committed Nov 11, 2016
2 parents 0d6581b + c81bae4 commit 1c2bec91126981e612d22bb4f5829fafdfbc0a49
View
@@ -1,6 +1,6 @@
* 1.28.0 (2016-XX-XX)
* added "is defined" support for block()
* added "is defined" support for block() and constant()
* optimized the way attributes are fetched
* 1.27.0 (2016-10-25)
@@ -16,3 +16,11 @@ As of 1.12.1 you can read constants from object instances as well:
.. code-block:: jinja
{{ constant('RSS', date) }}
Use the ``defined`` test to check if a constant is defined:
.. code-block:: jinja
{% if constant('SOME_CONST') is defined %}
...
{% endif %}
@@ -1452,6 +1452,23 @@ function twig_constant($constant, $object = null)
return constant($constant);
}
/**
* Checks if a constant exists.
*
* @param string $constant The name of the constant
* @param null|object $object The object to get the constant from
*
* @return bool
*/
function twig_constant_is_defined($constant, $object = null)
{
if (null !== $object) {
$constant = get_class($object).'::'.$constant;
}
return defined($constant);
}
/**
* Batches item.
*
@@ -12,7 +12,7 @@ class Twig_Node_Expression_Function extends Twig_Node_Expression_Call
{
public function __construct($name, Twig_NodeInterface $arguments, $lineno)
{
parent::__construct(array('arguments' => $arguments), array('name' => $name), $lineno);
parent::__construct(array('arguments' => $arguments), array('name' => $name, 'is_defined_test' => false), $lineno);
}
public function compile(Twig_Compiler $compiler)
@@ -27,7 +27,12 @@ public function compile(Twig_Compiler $compiler)
$this->setAttribute('needs_context', $function->needsContext());
$this->setAttribute('arguments', $function->getArguments());
if ($function instanceof Twig_FunctionCallableInterface || $function instanceof Twig_SimpleFunction) {
$this->setAttribute('callable', $function->getCallable());
$callable = $function->getCallable();
if ('constant' === $name && $this->getAttribute('is_defined_test')) {
$callable = 'twig_constant_is_defined';
}
$this->setAttribute('callable', $callable);
}
if ($function instanceof Twig_SimpleFunction) {
$this->setAttribute('is_variadic', $function->isVariadic());
@@ -32,6 +32,8 @@ public function __construct(Twig_NodeInterface $node, $name, Twig_NodeInterface
$this->changeIgnoreStrictCheck($node);
} elseif ($node instanceof Twig_Node_Expression_BlockReference) {
$node->setAttribute('is_defined_test', true);
} elseif ($node instanceof Twig_Node_Expression_Function && 'constant' === $node->getAttribute('name')) {
$node->setAttribute('is_defined_test', true);
} elseif ($node instanceof Twig_Node_Expression_Constant || $node instanceof Twig_Node_Expression_Array) {
$node = new Twig_Node_Expression_Constant(true, $node->getTemplateLine());
} else {
@@ -0,0 +1,14 @@
--TEST--
"defined" support for constants
--TEMPLATE--
{{ constant('DATE_W3C') is defined ? 'ok' : 'ko' }}
{{ constant('ARRAY_AS_PROPS', object) is defined ? 'ok' : 'ko' }}
{{ constant('FOOBAR') is not defined ? 'ok' : 'ko' }}
{{ constant('FOOBAR', object) is not defined ? 'ok' : 'ko' }}
--DATA--
return array('expect' => DATE_W3C, 'object' => new ArrayObject(array('hi')));
--EXPECT--
ok
ok
ok
ok

0 comments on commit 1c2bec9

Please sign in to comment.