| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the Symfony package.
|
|
|
5 |
*
|
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
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 <msantostefano@protonmail.com>
|
|
|
19 |
*/
|
|
|
20 |
abstract class AbstractVisitor
|
|
|
21 |
{
|
|
|
22 |
private MessageCatalogue $catalogue;
|
|
|
23 |
private \SplFileInfo $file;
|
|
|
24 |
private 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 |
$domain ??= 'messages';
|
|
|
36 |
$this->catalogue->set($message, $this->messagePrefix.$message, $domain);
|
|
|
37 |
$metadata = $this->catalogue->getMetadata($message, $domain) ?? [];
|
|
|
38 |
$normalizedFilename = preg_replace('{[\\\\/]+}', '/', $this->file);
|
|
|
39 |
$metadata['sources'][] = $normalizedFilename.':'.$line;
|
|
|
40 |
$this->catalogue->setMetadata($message, $metadata, $domain);
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
protected function getStringArguments(Node\Expr\CallLike|Node\Attribute|Node\Expr\New_ $node, int|string $index, bool $indexIsRegex = false): array
|
|
|
44 |
{
|
|
|
45 |
if (\is_string($index)) {
|
|
|
46 |
return $this->getStringNamedArguments($node, $index, $indexIsRegex);
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
$args = $node instanceof Node\Expr\CallLike ? $node->getRawArgs() : $node->args;
|
|
|
50 |
|
|
|
51 |
if (!($arg = $args[$index] ?? null) instanceof Node\Arg) {
|
|
|
52 |
return [];
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
return (array) $this->getStringValue($arg->value);
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
protected function hasNodeNamedArguments(Node\Expr\CallLike|Node\Attribute|Node\Expr\New_ $node): bool
|
|
|
59 |
{
|
|
|
60 |
$args = $node instanceof Node\Expr\CallLike ? $node->getRawArgs() : $node->args;
|
|
|
61 |
|
|
|
62 |
foreach ($args as $arg) {
|
|
|
63 |
if ($arg instanceof Node\Arg && null !== $arg->name) {
|
|
|
64 |
return true;
|
|
|
65 |
}
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
return false;
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
private function getStringNamedArguments(Node\Expr\CallLike|Node\Attribute $node, string $argumentName = null, bool $isArgumentNamePattern = false): array
|
|
|
72 |
{
|
|
|
73 |
$args = $node instanceof Node\Expr\CallLike ? $node->getArgs() : $node->args;
|
|
|
74 |
$argumentValues = [];
|
|
|
75 |
|
|
|
76 |
foreach ($args as $arg) {
|
|
|
77 |
if (!$isArgumentNamePattern && $arg->name?->toString() === $argumentName) {
|
|
|
78 |
$argumentValues[] = $this->getStringValue($arg->value);
|
|
|
79 |
} elseif ($isArgumentNamePattern && preg_match($argumentName, $arg->name?->toString() ?? '') > 0) {
|
|
|
80 |
$argumentValues[] = $this->getStringValue($arg->value);
|
|
|
81 |
}
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
return array_filter($argumentValues);
|
|
|
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 |
if (null === $left = $this->getStringValue($node->left)) {
|
|
|
95 |
return null;
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
if (null === $right = $this->getStringValue($node->right)) {
|
|
|
99 |
return null;
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
return $left.$right;
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
if ($node instanceof Node\Expr\Assign && $node->expr instanceof Node\Scalar\String_) {
|
|
|
106 |
return $node->expr->value;
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
return null;
|
|
|
110 |
}
|
|
|
111 |
}
|