1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Prometee\SwaggerClientGenerator\Base\Generator\Other; |
6
|
|
|
|
7
|
|
|
use Prometee\SwaggerClientGenerator\Base\Generator\AbstractGenerator; |
8
|
|
|
use Prometee\SwaggerClientGenerator\Base\View\Other\UsesViewInterface; |
9
|
|
|
|
10
|
|
|
class UsesGenerator extends AbstractGenerator implements UsesGeneratorInterface |
11
|
|
|
{ |
12
|
|
|
/** @var string[] */ |
13
|
|
|
protected $uses = []; |
14
|
|
|
/** @var string[] */ |
15
|
|
|
protected $internalUses = []; |
16
|
|
|
/** @var string */ |
17
|
|
|
protected $namespace = ''; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param UsesViewInterface $usesView |
21
|
|
|
*/ |
22
|
|
|
public function __construct(UsesViewInterface $usesView) |
23
|
|
|
{ |
24
|
|
|
$this->setView($usesView); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* {@inheritDoc} |
29
|
|
|
*/ |
30
|
|
|
public function configure(string $namespace, array $uses = [], array $internalUses = []): void |
31
|
|
|
{ |
32
|
|
|
$this->namespace = $namespace; |
33
|
|
|
$this->uses = $uses; |
34
|
|
|
$this->internalUses = $internalUses; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritDoc} |
39
|
|
|
*/ |
40
|
|
|
public function isUsable(string $str): bool |
41
|
|
|
{ |
42
|
|
|
return 1 === preg_match('#\\\\#', $str); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritDoc} |
47
|
|
|
*/ |
48
|
|
|
public function cleanUse(string $use): string |
49
|
|
|
{ |
50
|
|
|
$cleanedUse = rtrim($use, ']['); |
51
|
|
|
$cleanedUse = trim($cleanedUse, '\\'); |
52
|
|
|
return $cleanedUse; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritDoc} |
57
|
|
|
*/ |
58
|
|
|
public function guessUseOrReturnType(string $use): string |
59
|
|
|
{ |
60
|
|
|
if (false === $this->isUsable($use)) { |
61
|
|
|
return $use; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$isArray = 1 === preg_match('#\[\]$#', $use); |
65
|
|
|
$this->guessUse($use); |
66
|
|
|
return $this->getInternalUseName($use) . ($isArray ? '[]' : ''); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritDoc} |
71
|
|
|
*/ |
72
|
|
|
public function guessUse(string $use, string $alias = ''): void |
73
|
|
|
{ |
74
|
|
|
if (false === $this->isUsable($use)) { |
75
|
|
|
return; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$use = $this->cleanUse($use); |
79
|
|
|
|
80
|
|
|
if (true === $this->hasUse($use)) { |
81
|
|
|
return; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$useParts = explode('\\', $use); |
85
|
|
|
array_pop($useParts); |
86
|
|
|
$namespace = implode('\\', $useParts); |
87
|
|
|
|
88
|
|
|
if ($namespace === $this->namespace) { |
89
|
|
|
$this->processInternalUseName($use, $alias); |
90
|
|
|
return; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$this->addUse($use, $alias); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritDoc} |
98
|
|
|
*/ |
99
|
|
|
public function addUse(string $use, string $alias = '') |
100
|
|
|
{ |
101
|
|
|
if (!$this->hasUse($use)) { |
102
|
|
|
$this->setUse($use, $alias); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritDoc} |
108
|
|
|
*/ |
109
|
|
|
public function hasUse(string $use): bool |
110
|
|
|
{ |
111
|
|
|
return isset($this->uses[$use]); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* {@inheritDoc} |
116
|
|
|
*/ |
117
|
|
|
public function setUse(string $use, string $alias = '') |
118
|
|
|
{ |
119
|
|
|
$use = $this->cleanUse($use); |
120
|
|
|
$this->uses[$use] = $alias; |
121
|
|
|
$this->processInternalUseName($use, $alias); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* {@inheritDoc} |
126
|
|
|
*/ |
127
|
|
|
public function getUseAlias(string $use): ?string |
128
|
|
|
{ |
129
|
|
|
$use = $this->cleanUse($use); |
130
|
|
|
if ($this->hasUse($use)) { |
131
|
|
|
return $this->uses[$use]; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return null; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* {@inheritDoc} |
139
|
|
|
*/ |
140
|
|
|
public function getUses(): array |
141
|
|
|
{ |
142
|
|
|
return $this->uses; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* {@inheritDoc} |
147
|
|
|
*/ |
148
|
|
|
public function setUses(array $uses): void |
149
|
|
|
{ |
150
|
|
|
$this->uses = $uses; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* {@inheritDoc} |
155
|
|
|
*/ |
156
|
|
|
public function hasInternalUse(string $internalUseName): bool |
157
|
|
|
{ |
158
|
|
|
return isset($this->internalUses[$internalUseName]); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* {@inheritDoc} |
163
|
|
|
*/ |
164
|
|
|
public function getInternalUse(string $internalUseName): ?string |
165
|
|
|
{ |
166
|
|
|
$internalUseName = $this->cleanUse($internalUseName); |
167
|
|
|
if ($this->hasInternalUse($internalUseName)) { |
168
|
|
|
return $this->internalUses[$internalUseName]; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return null; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* {@inheritDoc} |
176
|
|
|
*/ |
177
|
|
|
public function getInternalUseName(string $use): ?string |
178
|
|
|
{ |
179
|
|
|
$use = $this->cleanUse($use); |
180
|
|
|
$internalUseName = array_search($use, $this->internalUses); |
181
|
|
|
|
182
|
|
|
if (false === $internalUseName) { |
183
|
|
|
return null; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
return $internalUseName; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* {@inheritDoc} |
191
|
|
|
*/ |
192
|
|
|
public function processInternalUseName(string $use, string $internalUseName = ''): void |
193
|
|
|
{ |
194
|
|
|
$use = $this->cleanUse($use); |
195
|
|
|
$existingInternalUseName = $this->getInternalUseName($use); |
196
|
|
|
if (null !== $existingInternalUseName) { |
197
|
|
|
return; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
if (empty($internalUseName)) { |
201
|
|
|
$useParts = explode('\\', $use); |
202
|
|
|
$internalUseName = end($useParts); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
$uniqInternalUseName = $internalUseName; |
206
|
|
|
if ($this->hasInternalUse($uniqInternalUseName)) { |
207
|
|
|
$uniqInternalUseName .= 'Alias'; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
$i = 1; |
211
|
|
|
while ($this->hasInternalUse($uniqInternalUseName)) { |
212
|
|
|
$uniqInternalUseName = $internalUseName . ++$i; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
if ($uniqInternalUseName !== $internalUseName) { |
216
|
|
|
$this->uses[$use] = $uniqInternalUseName; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
$this->internalUses[$uniqInternalUseName] = $use; |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|