-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAliasCaseConverter.php
More file actions
170 lines (136 loc) · 5.53 KB
/
AliasCaseConverter.php
File metadata and controls
170 lines (136 loc) · 5.53 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
<?php
declare(strict_types=1);
namespace Symplify\PhpConfigPrinter\CaseConverter;
use Nette\Utils\Strings;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\BinaryOp\Concat;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Expression;
use Symplify\PhpConfigPrinter\Contract\CaseConverterInterface;
use Symplify\PhpConfigPrinter\Exception\ShouldNotHappenException;
use Symplify\PhpConfigPrinter\NodeFactory\ArgsNodeFactory;
use Symplify\PhpConfigPrinter\NodeFactory\CommonNodeFactory;
use Symplify\PhpConfigPrinter\NodeFactory\Service\ServiceOptionNodeFactory;
use Symplify\PhpConfigPrinter\ValueObject\MethodName;
use Symplify\PhpConfigPrinter\ValueObject\VariableName;
use Symplify\PhpConfigPrinter\ValueObject\YamlKey;
final readonly class AliasCaseConverter implements CaseConverterInterface
{
/**
* @see https://regex101.com/r/BwXkfO/2/
* @var string
*/
private const ARGUMENT_NAME_REGEX = '#\$(?<argument_name>\w+)#';
/**
* @see https://regex101.com/r/DDuuVM/1
* @var string
*/
private const NAMED_ALIAS_REGEX = '#\w+\s+\$\w+#';
public function __construct(
private CommonNodeFactory $commonNodeFactory,
private ArgsNodeFactory $argsNodeFactory,
private ServiceOptionNodeFactory $serviceOptionNodeFactory,
) {
}
public function convertToMethodCallStmt(mixed $key, mixed $values): Stmt
{
if (! is_string($key)) {
throw new ShouldNotHappenException();
}
$servicesVariable = new Variable(VariableName::SERVICES);
if ($this->doesClassLikeExist($key)) {
return $this->createFromClassLike($key, $values, $servicesVariable);
}
// handles: "SomeClass $someVariable: ..."
$fullClassName = Strings::before($key, ' $');
if ($fullClassName !== null) {
$methodCall = $this->createAliasNode($key, $fullClassName, $values);
return new Expression($methodCall);
}
if (is_string($values) && $values[0] === '@') {
$args = $this->argsNodeFactory->createFromValues([$key, $values], true);
$methodCall = new MethodCall($servicesVariable, MethodName::ALIAS, $args);
return new Expression($methodCall);
}
if (is_array($values)) {
return $this->createFromArrayValues($values, $key, $servicesVariable);
}
throw new ShouldNotHappenException();
}
public function match(string $rootKey, mixed $key, mixed $values): bool
{
if ($rootKey !== YamlKey::SERVICES) {
return false;
}
if (isset($values[YamlKey::ALIAS])) {
return true;
}
if (Strings::match($key, self::NAMED_ALIAS_REGEX)) {
return true;
}
if (! is_string($values)) {
return false;
}
return $values[0] === '@';
}
private function createAliasNode(string $key, string $fullClassName, mixed $serviceValues): MethodCall
{
$args = [];
$classConstFetch = $this->commonNodeFactory->createClassReference($fullClassName);
Strings::match($key, self::ARGUMENT_NAME_REGEX);
$argumentName = '$' . Strings::after($key, '$');
$concat = new Concat($classConstFetch, new String_(' ' . $argumentName));
$args[] = new Arg($concat);
$serviceName = ltrim((string) $serviceValues, '@');
$args[] = new Arg(new String_($serviceName));
return new MethodCall(new Variable(VariableName::SERVICES), MethodName::ALIAS, $args);
}
private function createFromClassLike(string $key, mixed $values, Variable $servicesVariable): Expression
{
$classConstFetch = $this->commonNodeFactory->createClassReference($key);
$argValues = [];
$argValues[] = $classConstFetch;
$argValues[] = $values[MethodName::ALIAS] ?? $values;
$args = $this->argsNodeFactory->createFromValues($argValues, true);
$methodCall = new MethodCall($servicesVariable, MethodName::ALIAS, $args);
return new Expression($methodCall);
}
private function createFromAlias(string $serviceName, string $key, Variable $servicesVariable): MethodCall
{
if ($this->doesClassLikeExist($serviceName)) {
$classReference = $this->commonNodeFactory->createClassReference($serviceName);
$args = $this->argsNodeFactory->createFromValues([$key, $classReference]);
} else {
$args = $this->argsNodeFactory->createFromValues([$key, $serviceName]);
}
return new MethodCall($servicesVariable, MethodName::ALIAS, $args);
}
/**
* @param mixed[] $values
*/
private function createFromArrayValues(array $values, string $key, Variable $servicesVariable): Expression
{
if (isset($values[MethodName::ALIAS])) {
$methodCall = $this->createFromAlias($values[MethodName::ALIAS], $key, $servicesVariable);
unset($values[MethodName::ALIAS]);
} else {
throw new ShouldNotHappenException();
}
/** @var MethodCall $methodCall */
$methodCall = $this->serviceOptionNodeFactory->convertServiceOptionsToNodes($values, $methodCall);
return new Expression($methodCall);
}
private function doesClassLikeExist(string $class): bool
{
if (class_exists($class)) {
return true;
}
if (interface_exists($class)) {
return true;
}
return trait_exists($class);
}
}