Twig_Environment::getFunction PHP Method

getFunction() public method

Subclasses may override this method and load functions differently; so no list of functions is available.
public getFunction ( string $name ) : Twig_Function | false
$name string function name
return Twig_Function | false A Twig_Function instance or false if the function does not exists
    public function getFunction($name)
    {
        if (null === $this->functions) {
            $this->loadFunctions();
        }

        if (isset($this->functions[$name])) {
            return $this->functions[$name];
        }

        foreach ($this->functionCallbacks as $callback) {
            if (false !== $function = call_user_func($callback, $name)) {
                return $function;
            }
        }

        return false;
    }

Usage Example

 /**
  * Initializes arrays of filters and functions.
  */
 private function lazyInit()
 {
     $stringyClass = new \ReflectionClass('Stringy\\Stringy');
     $methods = $stringyClass->getMethods(\ReflectionMethod::IS_PUBLIC);
     $names = array_map(function ($value) {
         return $value->getName();
     }, $methods);
     foreach ($names as $name) {
         if (in_array($name, self::EXCLUDE_FUNCTIONS)) {
             continue;
         }
         $method = $stringyClass->getMethod($name);
         // Get the return type from the doc comment
         $doc = $method->getDocComment();
         if (strpos($doc, '@return bool')) {
             // Don't add functions which have the same name as any already in the environment
             if ($this->environment->getFunction($name)) {
                 continue;
             }
             $this->functions[$name] = new \Twig_SimpleFunction($name, function () use($name) {
                 return call_user_func_array(['Stringy\\StaticStringy', $name], func_get_args());
             });
         } else {
             // Don't add filters which have the same name as any already in the environment
             if ($this->environment->getFilter($name)) {
                 continue;
             }
             $this->filters[$name] = new \Twig_SimpleFilter($name, function () use($name) {
                 return call_user_func_array(['Stringy\\StaticStringy', $name], func_get_args());
             });
         }
     }
 }
All Usage Examples Of Twig_Environment::getFunction