/** * Adds a visitor. * * @param Twig_NodeVisitorInterface $visitor A Twig_NodeVisitorInterface instance */ public function addVisitor(Twig_NodeVisitorInterface $visitor) { if (!isset($this->visitors[$visitor->getPriority()])) { $this->visitors[$visitor->getPriority()] = array(); } $this->visitors[$visitor->getPriority()][] = $visitor; }
private function traverseForVisitor(Twig_NodeVisitorInterface $visitor, Twig_Node $node) { $node = $visitor->enterNode($node, $this->env); foreach ($node as $k => $n) { if (false !== ($n = $this->traverseForVisitor($visitor, $n))) { $node->setNode($k, $n); } else { $node->removeNode($k); } } return $visitor->leaveNode($node, $this->env); }
public function traverse(Twig_Node $node, Twig_NodeVisitorInterface $visitor) { $node = $visitor->enterNode($node, $this->env); if ($node instanceof Twig_NodeListInterface) { $newNodes = array(); foreach ($nodes = $node->getNodes() as $k => $n) { if (null !== ($n = $this->traverse($n, $visitor))) { $newNodes[$k] = $n; } } $node->setNodes($newNodes); } return $visitor->leaveNode($node, $this->env); }
protected function traverseForVisitor(Twig_NodeVisitorInterface $visitor, Twig_NodeInterface $node = null) { if (null === $node) { return null; } $node = $visitor->enterNode($node, $this->env); foreach ($node as $k => $n) { if (false !== $n = $this->traverseForVisitor($visitor, $n)) { $node->setNode($k, $n); } else { $node->removeNode($k); } } return $visitor->leaveNode($node, $this->env); }
/** * @return MessageCatalogue * @throws \Exception */ public function extract() { if (!empty($this->removingTwigVisitor)) { $this->removingTwigVisitor->setEnabled(false); } if (!empty($this->defaultApplyingTwigVisitor)) { $this->defaultApplyingTwigVisitor->setEnabled(false); } $finder = Finder::create()->in($this->directory); foreach ($this->excludedDirs as $dir) { $finder->exclude($dir); } foreach ($this->excludedNames as $name) { $finder->notName($name); } if ($this->pattern) { $finder->name($this->pattern); } $curTwigLoader = $this->twig->getLoader(); $this->twig->setLoader(new \Twig_Loader_String()); try { $catalogue = new MessageCatalogue(); foreach ($finder as $file) { $visitingMethod = 'visitFile'; $visitingArgs = array($file, $catalogue); $this->logger->debug(sprintf('Parsing file "%s"', $file)); if (false !== ($pos = strrpos($file, '.'))) { $extension = substr($file, $pos + 1); if ('php' === $extension) { try { $ast = $this->phpParser->parse(file_get_contents($file)); } catch (Error $ex) { throw new \RuntimeException(sprintf('Could not parse "%s": %s', $file, $ex->getMessage()), $ex->getCode(), $ex); } $visitingMethod = 'visitPhpFile'; $visitingArgs[] = $ast; } elseif ('twig' === $extension) { $visitingMethod = 'visitTwigFile'; $visitingArgs[] = $this->twig->parse($this->twig->tokenize(file_get_contents($file), (string) $file)); } } foreach ($this->visitors as $visitor) { call_user_func_array(array($visitor, $visitingMethod), $visitingArgs); } } if (null !== $curTwigLoader) { $this->twig->setLoader($curTwigLoader); } if (!empty($this->removingTwigVisitor)) { $this->removingTwigVisitor->setEnabled(true); } if (!empty($this->defaultApplyingTwigVisitor)) { $this->defaultApplyingTwigVisitor->setEnabled(true); } return $catalogue; } catch (\Exception $ex) { if (null !== $curTwigLoader) { $this->twig->setLoader($curTwigLoader); } throw $ex; } }