addVisitor example


    private $env;
    private $visitors = [];

    /** * @param NodeVisitorInterface[] $visitors */
    public function __construct(Environment $env, array $visitors = [])
    {
        $this->env = $env;
        foreach ($visitors as $visitor) {
            $this->addVisitor($visitor);
        }
    }

    public function addVisitor(NodeVisitorInterface $visitor): void
    {
        $this->visitors[$visitor->getPriority()][] = $visitor;
    }

    /** * Traverses a node and calls the registered visitors. */
    
return (string) $classConst->class;
            }
        }

        throw new \InvalidArgumentException(sprintf('Invalid class given %s', $className));
    }

    private function resolveNames(array $stmts): array
    {
        $nameResolver = new NameResolver();
        $nodeTraverser = new NodeTraverser();
        $nodeTraverser->addVisitor($nameResolver);

        return $nodeTraverser->traverse($stmts);
    }

    private function parseFile(string $filePath): array
    {
        $parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);

        return $this->resolveNames($parser->parse(file_get_contents($filePath)));
    }

    
$this->parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
    }

    public function extract(iterable|string $resource, MessageCatalogue $catalogue): void
    {
        foreach ($this->extractFiles($resource) as $file) {
            $traverser = new NodeTraverser();
            /** @var AbstractVisitor&NodeVisitor $visitor */
            foreach ($this->visitors as $visitor) {
                $visitor->initialize($catalogue$file$this->prefix);
                $traverser->addVisitor($visitor);
            }

            $nodes = $this->parser->parse(file_get_contents($file));
            $traverser->traverse($nodes);
        }
    }

    public function setPrefix(string $prefix): void
    {
        $this->prefix = $prefix;
    }

    
Home | Imprint | This part of the site doesn't use cookies.