public function __construct(Environment $environment = null) { $this->sections = [new Parser\Section\IfParser(), new Parser\Section\BlockParser(), new Parser\Section\LoopParser(), new Parser\Section\SetParser(), new Parser\Section\MacroParser(), new Parser\Section\ExtendsParser()]; if ($environment) { $this->sections = array_merge($this->sections, $environment->getSectionParsers()); } $this->environment = $environment; }
public function filter($name, $arguments, $content) { if (!$this->environment->hasFilter($name)) { throw new LogicException(sprintf('Filter "%s" not defined', $name)); } $filter = $this->environment->getFilter($name); return call_user_func_array($filter->getCallable(), array_merge([$content()], $arguments)); }
/** * @param Reader $reader * @param Environment $environment */ function __construct(Reader $reader, Environment $environment) { # Parsed tokens collection $this->tokens = new TokenCollection($reader->getFileName()); # Input reader $this->reader = $reader; # Extensions section parsers if ($environment && is_array($_sectionParsers = $environment->getSectionParsers())) { foreach ($_sectionParsers as $sectionParser) { $this->sectionKeywords[] = $sectionParser->getSectionName(); $this->sectionKeywords[] = $sectionParser->getSectionEnd(); } } # Filter empty keywords $this->sectionKeywords = array_filter($this->sectionKeywords); # Section types $this->regex[TokenTypes::T_SECTION_TYPE] = '/' . implode('|', $this->sectionKeywords) . '/iA'; # Print block tokens $this->printBlockTokens = array_intersect_key($this->regex, array_flip($this->printBlockTokens)); # Expression tokens $_temp = []; foreach ($this->expressionBlockTokens as $token) { if (is_array($token)) { $_temp[$token[0]] = [$this->regex[$token[0]], $token[1]]; } else { $_temp[$token] = $this->regex[$token]; } } $this->expressionBlockTokens = $_temp; # Initialize Section block start token $this->codeBlocksRegex[BlockType::BLOCK_SECTION][0] = '/(?=' . implode('(\\s|$)|', $this->sectionKeywords) . '(\\s|$))/Ai'; $this->environment = $environment; }