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
 /**
  * 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());
             });
         }
     }
 }
 public function htmlRespImg($html, $name, array $options = array())
 {
     $dom = $this->createDOMDocument($html);
     $elements = $dom->getElementsByTagName('img');
     if (count($elements) === 0) {
         return $html;
     }
     // Get Extension boltresponsiveimages
     $extensionName = 'boltresponsiveimages';
     if (!$this->app['extensions']->isEnabled($extensionName)) {
         return $html;
     }
     $extension = $this->app['extensions.' . $extensionName];
     if ($extension == null) {
         return $html;
     }
     // Get Twig Function respImg
     $twigFunction = $this->twig->getFunction('respImg');
     if (!$twigFunction) {
         return $html;
     }
     $respImg = $twigFunction->getCallable();
     // Not override sizes, if defined
     if (!$options['sizes']) {
         $options['sizes'] = $extension->getSizesAttrib($name);
     }
     foreach ($elements as $element) {
         if (!$element->hasAttribute('src')) {
             continue;
         }
         $file = $element->getAttribute('src');
         $filename = Lib::safeFilename($file);
         // Set options for specific image
         $optionsImg = $options;
         // Add width fallback to sizes because layout reasons
         // Example: An editor choose a specific width
         if ($element->hasAttribute('width')) {
             $width = $element->getAttribute('width');
             $optionsImg['sizes'][] = $width . 'px';
         }
         if ($element->hasAttribute('class')) {
             $attrClass = $element->getAttribute('class');
             $optionsImg['class'][] = $attrClass;
         }
         $htmlImg = (string) $respImg($filename, $name, $optionsImg);
         $domImg = $this->createDOMDocument($htmlImg);
         // Load the $domImg document fragment node into the current document
         $newnode = $dom->importNode($domImg->documentElement, true);
         // Replace current img node
         $element->parentNode->replaceChild($newnode, $element);
     }
     $result = $dom->saveHTML();
     return new \Twig_Markup($result, 'UTF-8');
 }
 public function leaveNode(Twig_NodeInterface $node, Twig_Environment $env)
 {
     if ($node instanceof Twig_Node_Expression_Constant) {
         // constants are marked safe for all
         $this->setSafe($node, array('all'));
     } elseif ($node instanceof Twig_Node_Expression_Conditional) {
         // intersect safeness of both operands
         $safe = $this->intersectSafe($this->getSafe($node->getNode('expr2')), $this->getSafe($node->getNode('expr3')));
         $this->setSafe($node, $safe);
     } elseif ($node instanceof Twig_Node_Expression_Filter) {
         // filter expression is safe when the filter is safe
         $name = $node->getNode('filter')->getAttribute('value');
         $args = $node->getNode('arguments');
         if (false !== ($filter = $env->getFilter($name))) {
             $this->setSafe($node, $filter->getSafe($args));
         } else {
             $this->setSafe($node, array());
         }
     } elseif ($node instanceof Twig_Node_Expression_Function) {
         // function expression is safe when the function is safe
         $name = $node->getNode('name')->getAttribute('name');
         $args = $node->getNode('arguments');
         $function = $env->getFunction($name);
         if (false !== $function) {
             $this->setSafe($node, $function->getSafe($args));
         } else {
             $this->setSafe($node, array());
         }
     } else {
         $this->setSafe($node, array());
     }
     return $node;
 }
Beispiel #4
0
    /**
     * Extracts formulae from filter function nodes.
     *
     * @return array|null The formula
     */
    private function checkNode(\Twig_NodeInterface $node, \Twig_Environment $env)
    {
        if ($node instanceof \Twig_Node_Expression_Function) {
            $name = $node->getNode('name')->getAttribute('name');
            if ($env->getFunction($name) instanceof AsseticFilterFunction) {
                $arguments = array();
                foreach ($node->getNode('arguments') as $argument) {
                    $arguments[] = eval('return '.$env->compile($argument).';');
                }

                $invoker = $env->getExtension('assetic')->getFilterInvoker($name);
                $factory = $invoker->getFactory();

                $inputs = isset($arguments[0]) ? (array) $arguments[0] : array();
                $filters = $invoker->getFilters();
                $options = array_replace($invoker->getOptions(), isset($arguments[1]) ? $arguments[1] : array());

                if (!isset($options['name'])) {
                    $options['name'] = $factory->generateAssetName($inputs, $filters);
                }

                return array($inputs, $filters, $options);
            }
        }
    }
Beispiel #5
0
 public function leaveNode(Twig_NodeInterface $node, Twig_Environment $env)
 {
     if ($node instanceof Twig_Node_Expression_Constant) {
         // constants are marked safe for all
         $this->setSafe($node, array('all'));
     } elseif ($node instanceof Twig_Node_Expression_BlockReference) {
         // blocks are safe by definition
         $this->setSafe($node, array('all'));
     } elseif ($node instanceof Twig_Node_Expression_Parent) {
         // parent block is safe by definition
         $this->setSafe($node, array('all'));
     } elseif ($node instanceof Twig_Node_Expression_Conditional) {
         // intersect safeness of both operands
         $safe = $this->intersectSafe($this->getSafe($node->getNode('expr2')), $this->getSafe($node->getNode('expr3')));
         $this->setSafe($node, $safe);
     } elseif ($node instanceof Twig_Node_Expression_Filter) {
         // filter expression is safe when the filter is safe
         $name = $node->getNode('filter')->getAttribute('value');
         $args = $node->getNode('arguments');
         if (false !== ($filter = $env->getFilter($name))) {
             $safe = $filter->getSafe($args);
             if (null === $safe) {
                 $safe = $this->intersectSafe($this->getSafe($node->getNode('node')), $filter->getPreservesSafety());
             }
             $this->setSafe($node, $safe);
         } else {
             $this->setSafe($node, array());
         }
     } elseif ($node instanceof Twig_Node_Expression_Function) {
         // function expression is safe when the function is safe
         $name = $node->getAttribute('name');
         $args = $node->getNode('arguments');
         $function = $env->getFunction($name);
         if (false !== $function) {
             $this->setSafe($node, $function->getSafe($args));
         } else {
             $this->setSafe($node, array());
         }
     } elseif ($node instanceof Twig_Node_Expression_MethodCall) {
         if ($node->getAttribute('safe')) {
             $this->setSafe($node, array('all'));
         } else {
             $this->setSafe($node, array());
         }
     } elseif ($node instanceof Twig_Node_Expression_MacroCall) {
         $this->setSafe($node, array('all'));
     } elseif ($node instanceof Twig_Node_Expression_GetAttr && $node->getNode('node') instanceof Twig_Node_Expression_Name) {
         $name = $node->getNode('node')->getAttribute('name');
         // attributes on template instances are safe
         if ('_self' == $name || in_array($name, $this->safeVars)) {
             $this->setSafe($node, array('all'));
         } else {
             $this->setSafe($node, array());
         }
     } else {
         $this->setSafe($node, array());
     }
     return $node;
 }
 public function getFunction($name)
 {
     if (false !== ($function = parent::getFunction($name))) {
         return $function;
     }
     $helper = $this->view->getHelper($name);
     if (null === $helper) {
         return false;
     }
     $function = new Zwig_Function_ViewHelper($name, $helper);
     $this->addFunction($name, $function);
     return $function;
 }
 /**
  * @param $asset
  *
  * @return mixed
  */
 protected function asset($asset)
 {
     return call_user_func($this->twig->getFunction('asset')->getCallable(), $asset);
 }