From: Mark Story Date: Fri, 11 Jan 2013 04:38:01 +0000 (-0500) Subject: Implement getting constants from objects. X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=cb921ce602b984e1a1dee3bd11bdb92486177dd5;p=web%2Fkonrad%2Ftwig.git Implement getting constants from objects. --- diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index 01eaee0..18deb81 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -184,7 +184,7 @@ class Twig_Extension_Core extends Twig_Extension { return array( new Twig_SimpleFunction('range', 'range'), - new Twig_SimpleFunction('constant', 'constant'), + new Twig_SimpleFunction('constant', 'twig_constant'), new Twig_SimpleFunction('cycle', 'twig_cycle'), new Twig_SimpleFunction('random', 'twig_random', array('needs_environment' => true)), new Twig_SimpleFunction('date', 'twig_date_converter', array('needs_environment' => true)), @@ -1256,3 +1256,22 @@ function twig_include(Twig_Environment $env, $context, $template, $variables = a $sandbox->disableSandbox(); } } + +/** + * Wrapper around constant() provides the ability to get constants + * from instances as well as class/global constants. + * + * @param string $constant The name of the constant. + * @param null|mixed $object The object to get the constant from. + * + * @return mixed + */ +function twig_constant($constant, $object = null) +{ + if (!$object) { + return constant($constant); + } + $class = get_class($object); + + return constant($class.'::'.$constant); +}