|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of the humbug/php-scoper package. |
| 7 | + * |
| 8 | + * Copyright (c) 2017 Théo FIDRY <[email protected]>, |
| 9 | + * Pádraic Brady <[email protected]> |
| 10 | + * |
| 11 | + * For the full copyright and license information, please view the LICENSE |
| 12 | + * file that was distributed with this source code. |
| 13 | + */ |
| 14 | + |
| 15 | +namespace Humbug\PhpScoper\Scoper; |
| 16 | + |
| 17 | +use Humbug\PhpScoper\NodeVisitor\FullyQualifiedNamespaceUseScoperNodeVisitor; |
| 18 | +use Humbug\PhpScoper\NodeVisitor\GroupUseNamespaceScoperNodeVisitor; |
| 19 | +use Humbug\PhpScoper\NodeVisitor\IgnoreNamespaceScoperNodeVisitor; |
| 20 | +use Humbug\PhpScoper\NodeVisitor\NamespaceScoperNodeVisitor; |
| 21 | +use Humbug\PhpScoper\NodeVisitor\ParentNodeVisitor; |
| 22 | +use Humbug\PhpScoper\NodeVisitor\UseNamespaceScoperNodeVisitor; |
| 23 | +use Humbug\PhpScoper\Scoper; |
| 24 | +use PhpParser\Error as PhpParserError; |
| 25 | +use PhpParser\NodeTraverser; |
| 26 | +use PhpParser\NodeTraverserInterface; |
| 27 | +use PhpParser\Parser; |
| 28 | +use PhpParser\PrettyPrinter\Standard; |
| 29 | + |
| 30 | +final class PhpScoper implements Scoper |
| 31 | +{ |
| 32 | + /** @internal */ |
| 33 | + const FILE_PATTERN = '/.*\.php$/'; |
| 34 | + |
| 35 | + private $parser; |
| 36 | + private $decoratedScoper; |
| 37 | + |
| 38 | + public function __construct(Parser $parser, Scoper $decoratedScoper) |
| 39 | + { |
| 40 | + $this->parser = $parser; |
| 41 | + $this->decoratedScoper = $decoratedScoper; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Scopes PHP files. |
| 46 | + * |
| 47 | + * {@inheritdoc} |
| 48 | + * |
| 49 | + * @throws PhpParserError |
| 50 | + */ |
| 51 | + public function scope(string $filePath, string $prefix): string |
| 52 | + { |
| 53 | + if (1 !== preg_match(self::FILE_PATTERN, $filePath)) { |
| 54 | + return $this->decoratedScoper->scope($filePath, $prefix); |
| 55 | + } |
| 56 | + |
| 57 | + $content = file_get_contents($filePath); |
| 58 | + |
| 59 | + $traverser = $this->createTraverser($prefix); |
| 60 | + |
| 61 | + $statements = $this->parser->parse($content); |
| 62 | + $statements = $traverser->traverse($statements); |
| 63 | + |
| 64 | + $prettyPrinter = new Standard(); |
| 65 | + |
| 66 | + return $prettyPrinter->prettyPrintFile($statements)."\n"; |
| 67 | + } |
| 68 | + |
| 69 | + private function createTraverser(string $prefix): NodeTraverserInterface |
| 70 | + { |
| 71 | + $traverser = new NodeTraverser(); |
| 72 | + |
| 73 | + $traverser->addVisitor(new ParentNodeVisitor()); |
| 74 | + $traverser->addVisitor(new IgnoreNamespaceScoperNodeVisitor()); |
| 75 | + $traverser->addVisitor(new GroupUseNamespaceScoperNodeVisitor($prefix)); |
| 76 | + $traverser->addVisitor(new NamespaceScoperNodeVisitor($prefix)); |
| 77 | + $traverser->addVisitor(new UseNamespaceScoperNodeVisitor($prefix)); |
| 78 | + $traverser->addVisitor(new FullyQualifiedNamespaceUseScoperNodeVisitor($prefix)); |
| 79 | + |
| 80 | + return $traverser; |
| 81 | + } |
| 82 | +} |
0 commit comments