-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathImportCaseConverter.php
More file actions
176 lines (144 loc) · 4.87 KB
/
ImportCaseConverter.php
File metadata and controls
176 lines (144 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
declare(strict_types=1);
namespace Symplify\PhpConfigPrinter\CaseConverter;
use Nette\Utils\Strings;
use PhpParser\BuilderHelpers;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Expression;
use Symplify\PhpConfigPrinter\Contract\CaseConverterInterface;
use Symplify\PhpConfigPrinter\Exception\NotImplementedYetException;
use Symplify\PhpConfigPrinter\NodeFactory\CommonNodeFactory;
use Symplify\PhpConfigPrinter\Sorter\YamlArgumentSorter;
use Symplify\PhpConfigPrinter\ValueObject\VariableName;
use Symplify\PhpConfigPrinter\ValueObject\YamlKey;
/**
* Handles this part:
*
* imports: <---
*/
final readonly class ImportCaseConverter implements CaseConverterInterface
{
/**
* @see https://regex101.com/r/hOTdIE/1
* @var string
*/
private const INPUT_SUFFIX_REGEX = '#\.(yml|yaml|xml)$#';
public function __construct(
private YamlArgumentSorter $yamlArgumentSorter,
private CommonNodeFactory $commonNodeFactory
) {
}
public function match(string $rootKey, mixed $key, mixed $values): bool
{
return $rootKey === YamlKey::IMPORTS;
}
public function convertToMethodCallStmt(mixed $key, mixed $values): Stmt
{
if (is_array($values)) {
$arguments = $this->yamlArgumentSorter->sortArgumentsByKeyIfExists($values, [
YamlKey::RESOURCE => '',
'type' => null,
YamlKey::IGNORE_ERRORS => false,
]);
return $this->createImportMethodCall($arguments);
}
if (is_string($values)) {
return $this->createImportMethodCall([$values]);
}
throw new NotImplementedYetException();
}
/**
* @param mixed[] $arguments
*/
private function createImportMethodCall(array $arguments): Expression
{
$containerConfiguratorVariable = new Variable(VariableName::CONTAINER_CONFIGURATOR);
$args = $this->createArgs($arguments);
$methodCall = new MethodCall($containerConfiguratorVariable, 'import', $args);
return new Expression($methodCall);
}
/**
* @param array<int|string, mixed> $arguments
* @return Arg[]
*/
private function createArgs(array $arguments): array
{
$args = [];
foreach ($arguments as $name => $value) {
if (is_string($name) && $this->shouldSkipDefaultValue($name, $value, $arguments)) {
continue;
}
$expr = $this->resolveExpr($value);
$args[] = new Arg($expr);
}
return $args;
}
/**
* @param mixed[] $arguments
*/
private function shouldSkipDefaultValue(string $name, mixed $value, array $arguments): bool
{
// skip default value for "ignore_errors"
if ($name === YamlKey::IGNORE_ERRORS && $value === false) {
return true;
}
// check if default value for "type"
if ($name !== 'type') {
return false;
}
if ($value !== null) {
return false;
}
// follow by default value for "ignore_errors"
if (! isset($arguments[YamlKey::IGNORE_ERRORS])) {
return false;
}
return $arguments[YamlKey::IGNORE_ERRORS] === false;
}
private function replaceImportedFileSuffix(mixed $value): mixed
{
if (! is_string($value)) {
return $value;
}
return Strings::replace($value, self::INPUT_SUFFIX_REGEX, '.php');
}
private function resolveExpr(mixed $value): Expr
{
$expr = $this->processNormalizedValue($value);
if ($expr instanceof Expr) {
return $expr;
}
if ($value === 'not_found') {
return new String_('not_found');
}
if (is_string($value) && \str_contains($value, '::')) {
[$className, $constantName] = explode('::', $value);
return new ClassConstFetch(new FullyQualified($className), $constantName);
}
if (is_string($value) && \str_starts_with($value, '@')) {
return new String_($value);
}
$value = $this->replaceImportedFileSuffix($value);
if (is_string($value) && \str_starts_with($value, '%')) {
return new String_($value);
}
return $this->commonNodeFactory->createAbsoluteDirExpr($value);
}
private function processNormalizedValue(mixed $value): ?Expr
{
if (is_bool($value)) {
return BuilderHelpers::normalizeValue($value);
}
if (in_array($value, ['annotations', 'directory', 'glob'], true)) {
return BuilderHelpers::normalizeValue($value);
}
return null;
}
}