You are here

protected function Twig_Environment::initExtension in Translation template extractor 6.3

Same name and namespace in other branches
  1. 7.3 vendor/Twig/Environment.php \Twig_Environment::initExtension()
1 call to Twig_Environment::initExtension()
Twig_Environment::initExtensions in vendor/Twig/Environment.php

File

vendor/Twig/Environment.php, line 1166

Class

Twig_Environment
Stores the Twig configuration.

Code

protected function initExtension(Twig_ExtensionInterface $extension) {

  // filters
  foreach ($extension
    ->getFilters() as $name => $filter) {
    if ($name instanceof Twig_SimpleFilter) {
      $filter = $name;
      $name = $filter
        ->getName();
    }
    elseif ($filter instanceof Twig_SimpleFilter) {
      $name = $filter
        ->getName();
    }
    $this->filters[$name] = $filter;
  }

  // functions
  foreach ($extension
    ->getFunctions() as $name => $function) {
    if ($name instanceof Twig_SimpleFunction) {
      $function = $name;
      $name = $function
        ->getName();
    }
    elseif ($function instanceof Twig_SimpleFunction) {
      $name = $function
        ->getName();
    }
    $this->functions[$name] = $function;
  }

  // tests
  foreach ($extension
    ->getTests() as $name => $test) {
    if ($name instanceof Twig_SimpleTest) {
      $test = $name;
      $name = $test
        ->getName();
    }
    elseif ($test instanceof Twig_SimpleTest) {
      $name = $test
        ->getName();
    }
    $this->tests[$name] = $test;
  }

  // token parsers
  foreach ($extension
    ->getTokenParsers() as $parser) {
    if ($parser instanceof Twig_TokenParserInterface) {
      $this->parsers
        ->addTokenParser($parser);
    }
    elseif ($parser instanceof Twig_TokenParserBrokerInterface) {
      $this->parsers
        ->addTokenParserBroker($parser);
    }
    else {
      throw new LogicException('getTokenParsers() must return an array of Twig_TokenParserInterface or Twig_TokenParserBrokerInterface instances');
    }
  }

  // node visitors
  foreach ($extension
    ->getNodeVisitors() as $visitor) {
    $this->visitors[] = $visitor;
  }

  // operators
  if ($operators = $extension
    ->getOperators()) {
    if (2 !== count($operators)) {
      throw new InvalidArgumentException(sprintf('"%s::getOperators()" does not return a valid operators array.', get_class($extension)));
    }
    $this->unaryOperators = array_merge($this->unaryOperators, $operators[0]);
    $this->binaryOperators = array_merge($this->binaryOperators, $operators[1]);
  }
}