|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[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 Symfony\Component\Translation\Extractor\Visitor; |
| 13 | + |
| 14 | +use PhpParser\Node; |
| 15 | +use Symfony\Component\Translation\MessageCatalogue; |
| 16 | + |
| 17 | +/** |
| 18 | + * @author Mathieu Santostefano <[email protected]> |
| 19 | + */ |
| 20 | +abstract class AbstractVisitor |
| 21 | +{ |
| 22 | + protected MessageCatalogue $catalogue; |
| 23 | + protected \SplFileInfo $file; |
| 24 | + protected string $messagePrefix; |
| 25 | + |
| 26 | + public function initialize(MessageCatalogue $catalogue, \SplFileInfo $file, string $messagePrefix): void |
| 27 | + { |
| 28 | + $this->catalogue = $catalogue; |
| 29 | + $this->file = $file; |
| 30 | + $this->messagePrefix = $messagePrefix; |
| 31 | + } |
| 32 | + |
| 33 | + protected function addMessageToCatalogue(string $message, ?string $domain, int $line): void |
| 34 | + { |
| 35 | + if (null === $domain) { |
| 36 | + $domain = 'messages'; |
| 37 | + } |
| 38 | + |
| 39 | + $this->catalogue->set($message, $this->messagePrefix.$message, $domain); |
| 40 | + $metadata = $this->catalogue->getMetadata($message, $domain) ?? []; |
| 41 | + $normalizedFilename = preg_replace('{[\\\\/]+}', '/', $this->file); |
| 42 | + $metadata['sources'][] = $normalizedFilename.':'.$line; |
| 43 | + $this->catalogue->setMetadata($message, $metadata, $domain); |
| 44 | + } |
| 45 | + |
| 46 | + protected function getStringArgument(Node\Expr\CallLike|Node\Attribute $node, int|string $index): ?string |
| 47 | + { |
| 48 | + if (\is_string($index)) { |
| 49 | + return $this->getStringNamedArgument($node, $index); |
| 50 | + } |
| 51 | + |
| 52 | + if (!\array_key_exists($index, $node->args)) { |
| 53 | + return null; |
| 54 | + } |
| 55 | + |
| 56 | + $result = $this->getStringValue($node->args[$index]->value); |
| 57 | + |
| 58 | + if ('' === $result) { |
| 59 | + return null; |
| 60 | + } |
| 61 | + |
| 62 | + return $result; |
| 63 | + } |
| 64 | + |
| 65 | + protected function hasNodeNamedArguments(Node\Expr\CallLike|Node\Attribute $node): bool |
| 66 | + { |
| 67 | + foreach ($node->args as $arg) { |
| 68 | + if (null !== $arg->name) { |
| 69 | + return true; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + return false; |
| 74 | + } |
| 75 | + |
| 76 | + private function getStringNamedArgument(Node\Expr\CallLike|Node\Attribute $node, string $argumentName): ?string |
| 77 | + { |
| 78 | + foreach ($node->args as $arg) { |
| 79 | + if ($arg->name?->toString() === $argumentName) { |
| 80 | + return $this->getStringValue($arg->value); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + return null; |
| 85 | + } |
| 86 | + |
| 87 | + private function getStringValue(Node $node): ?string |
| 88 | + { |
| 89 | + if ($node instanceof Node\Scalar\String_) { |
| 90 | + return $node->value; |
| 91 | + } |
| 92 | + |
| 93 | + if ($node instanceof Node\Expr\BinaryOp\Concat) { |
| 94 | + $left = $this->getStringValue($node->left); |
| 95 | + if (null === $left) { |
| 96 | + return null; |
| 97 | + } |
| 98 | + |
| 99 | + $right = $this->getStringValue($node->right); |
| 100 | + if (null === $right) { |
| 101 | + return null; |
| 102 | + } |
| 103 | + |
| 104 | + return $left.$right; |
| 105 | + } |
| 106 | + |
| 107 | + if ($node instanceof Node\Expr\Assign) { |
| 108 | + if ($node->expr instanceof Node\Scalar\String_) { |
| 109 | + return $node->expr->value; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + return null; |
| 114 | + } |
| 115 | +} |
0 commit comments