|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the PHP Translation package. |
| 5 | + * |
| 6 | + * (c) PHP Translation team <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Translation\Extractor\Visitor\Php\Knp\Menu; |
| 13 | + |
| 14 | +use PhpParser\Node; |
| 15 | +use PhpParser\NodeVisitor; |
| 16 | +use Translation\Extractor\Model\SourceLocation; |
| 17 | +use Translation\Extractor\Visitor\Php\BasePHPVisitor; |
| 18 | + |
| 19 | +/** |
| 20 | + * This class provides common functionality for KnpMenu extractors. |
| 21 | + */ |
| 22 | +abstract class AbstractKnpMenuVisitor extends BasePHPVisitor implements NodeVisitor |
| 23 | +{ |
| 24 | + /** |
| 25 | + * @var bool |
| 26 | + */ |
| 27 | + private $isKnpMenuBuildingMethod = false; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var string|bool |
| 31 | + */ |
| 32 | + private $domain; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var SourceLocation[] |
| 36 | + */ |
| 37 | + private $sourceLocations = []; |
| 38 | + |
| 39 | + public function beforeTraverse(array $nodes): ?Node |
| 40 | + { |
| 41 | + $this->sourceLocations = []; |
| 42 | + |
| 43 | + return null; |
| 44 | + } |
| 45 | + |
| 46 | + public function enterNode(Node $node): ?Node |
| 47 | + { |
| 48 | + if (!$this->isKnpMenuBuildingMethod($node)) { |
| 49 | + return null; |
| 50 | + } |
| 51 | + |
| 52 | + if (!$node instanceof Node\Expr\MethodCall) { |
| 53 | + return null; |
| 54 | + } |
| 55 | + |
| 56 | + if (!\is_string($node->name) && !$node->name instanceof Node\Identifier) { |
| 57 | + return null; |
| 58 | + } |
| 59 | + |
| 60 | + $methodName = (string) $node->name; |
| 61 | + if ('setExtra' !== $methodName) { |
| 62 | + return null; |
| 63 | + } |
| 64 | + |
| 65 | + $extraKey = $this->getStringArgument($node, 0); |
| 66 | + if ('translation_domain' === $extraKey) { |
| 67 | + if ( |
| 68 | + $node->args[1]->value instanceof Node\Expr\ConstFetch |
| 69 | + && 'false' === $node->args[1]->value->name->toString() |
| 70 | + ) { |
| 71 | + // translation disabled |
| 72 | + $this->domain = false; |
| 73 | + } else { |
| 74 | + $extraValue = $this->getStringArgument($node, 1); |
| 75 | + if (null !== $extraValue) { |
| 76 | + $this->domain = $extraValue; |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + return null; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Checks if the given node is a class method returning a knp menu. |
| 86 | + */ |
| 87 | + protected function isKnpMenuBuildingMethod(Node $node): bool |
| 88 | + { |
| 89 | + if ($node instanceof Node\Stmt\ClassMethod) { |
| 90 | + if (null === $node->returnType) { |
| 91 | + $this->isKnpMenuBuildingMethod = false; |
| 92 | + } |
| 93 | + if ($node->returnType instanceof Node\Identifier) { |
| 94 | + $this->isKnpMenuBuildingMethod = false; |
| 95 | + } |
| 96 | + |
| 97 | + $returnType = $node->returnType; |
| 98 | + if ($returnType instanceof Node\NullableType) { |
| 99 | + $returnType = $returnType->type; |
| 100 | + } |
| 101 | + |
| 102 | + if (!$returnType instanceof Node\Name) { |
| 103 | + $this->isKnpMenuBuildingMethod = false; |
| 104 | + } else { |
| 105 | + $this->isKnpMenuBuildingMethod = 'ItemInterface' === $returnType->toString(); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return $this->isKnpMenuBuildingMethod; |
| 110 | + } |
| 111 | + |
| 112 | + protected function lateCollect(SourceLocation $location): void |
| 113 | + { |
| 114 | + $this->sourceLocations[] = $location; |
| 115 | + } |
| 116 | + |
| 117 | + public function leaveNode(Node $node): ?Node |
| 118 | + { |
| 119 | + return null; |
| 120 | + } |
| 121 | + |
| 122 | + public function afterTraverse(array $nodes): ?Node |
| 123 | + { |
| 124 | + if (false === $this->domain) { |
| 125 | + // translation disabled |
| 126 | + return null; |
| 127 | + } |
| 128 | + |
| 129 | + /** @var SourceLocation $location */ |
| 130 | + foreach ($this->sourceLocations as $location) { |
| 131 | + if (null !== $this->domain) { |
| 132 | + $context = $location->getContext(); |
| 133 | + $context['domain'] = $this->domain; |
| 134 | + $location = new SourceLocation($location->getMessage(), $location->getPath(), $location->getLine(), $context); |
| 135 | + } |
| 136 | + $this->collection->addLocation($location); |
| 137 | + } |
| 138 | + $this->sourceLocations = []; |
| 139 | + |
| 140 | + return null; |
| 141 | + } |
| 142 | +} |
0 commit comments