|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace WsdlToPhp\PackageGenerator\File; |
|
6
|
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
use SoapFault; |
|
9
|
|
|
use WsdlToPhp\PackageGenerator\ConfigurationReader\GeneratorOptions; |
|
10
|
|
|
use WsdlToPhp\PackageGenerator\Container\PhpElement\Constant as ConstantContainer; |
|
11
|
|
|
use WsdlToPhp\PackageGenerator\Container\PhpElement\Property as PropertyContainer; |
|
12
|
|
|
use WsdlToPhp\PackageGenerator\File\Element\PhpFunctionParameter; |
|
13
|
|
|
use WsdlToPhp\PackageGenerator\File\Validation\Rules; |
|
14
|
|
|
use WsdlToPhp\PackageGenerator\Generator\Generator; |
|
15
|
|
|
use WsdlToPhp\PackageGenerator\Model\AbstractModel; |
|
16
|
|
|
use WsdlToPhp\PackageGenerator\Model\Method; |
|
17
|
|
|
use WsdlToPhp\PackageGenerator\Model\Method as MethodModel; |
|
18
|
|
|
use WsdlToPhp\PackageGenerator\Model\Service as ServiceModel; |
|
19
|
|
|
use WsdlToPhp\PackageGenerator\Model\Struct as StructModel; |
|
20
|
|
|
use WsdlToPhp\PackageGenerator\Model\StructAttribute as StructAttributeModel; |
|
21
|
|
|
use WsdlToPhp\PackageGenerator\Parser\Wsdl\TagHeader; |
|
22
|
|
|
use WsdlToPhp\PhpGenerator\Element\PhpAnnotation; |
|
23
|
|
|
use WsdlToPhp\PhpGenerator\Element\PhpAnnotationBlock; |
|
24
|
|
|
use WsdlToPhp\PhpGenerator\Element\PhpConstant; |
|
25
|
|
|
use WsdlToPhp\PhpGenerator\Element\PhpFunctionParameter as PhpFunctionParameterBase; |
|
26
|
|
|
use WsdlToPhp\PhpGenerator\Element\PhpMethod; |
|
27
|
|
|
use WsdlToPhp\PhpGenerator\Element\PhpProperty; |
|
28
|
|
|
|
|
29
|
|
|
final class Service extends AbstractModelFile |
|
30
|
|
|
{ |
|
31
|
|
|
public const METHOD_SET_HEADER_PREFIX = 'setSoapHeader'; |
|
32
|
|
|
public const PARAM_SET_HEADER_NAMESPACE = 'namespace'; |
|
33
|
|
|
public const PARAM_SET_HEADER_MUSTUNDERSTAND = 'mustUnderstand'; |
|
34
|
|
|
public const PARAM_SET_HEADER_ACTOR = 'actor'; |
|
35
|
|
|
public const METHOD_GET_RESULT = 'getResult'; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Method model can't be found in case the original method's name is unclean: |
|
39
|
|
|
* - ex: my.operation.name becomes my_operation_name |
|
40
|
|
|
* thus the Model from Model\Service::getMethod() can't be found |
|
41
|
|
|
* So we store the generated name associated to the original method object. |
|
42
|
|
|
*/ |
|
43
|
|
|
protected array $methodNames = []; |
|
44
|
|
|
|
|
45
|
42 |
|
public static function getOperationMethodReturnType(MethodModel $method, Generator $generator): string |
|
46
|
|
|
{ |
|
47
|
42 |
|
$returnType = $method->getReturnType(); |
|
48
|
|
|
|
|
49
|
42 |
|
if (is_null($returnType)) { |
|
50
|
2 |
|
return 'null'; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
40 |
|
if ((($struct = $generator->getStructByName($returnType)) instanceof StructModel) && !$struct->isRestriction()) { |
|
54
|
34 |
|
if ($struct->isStruct()) { |
|
55
|
34 |
|
$returnType = $struct->getPackagedName(true); |
|
56
|
7 |
|
} elseif ($struct->isArray()) { |
|
57
|
7 |
|
if (($structInheritance = $struct->getInheritanceStruct()) instanceof StructModel) { |
|
58
|
7 |
|
$returnType = sprintf('%s[]', $structInheritance->getPackagedName(true)); |
|
59
|
|
|
} else { |
|
60
|
4 |
|
$returnType = $struct->getInheritance(); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
40 |
|
return $returnType; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
44 |
|
public function setModel(AbstractModel $model): self |
|
69
|
|
|
{ |
|
70
|
44 |
|
if (!$model instanceof ServiceModel) { |
|
71
|
2 |
|
throw new InvalidArgumentException('Model must be an instance of a Service', __LINE__); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
42 |
|
return parent::setModel($model); |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
protected function fillClassConstants(ConstantContainer $constants): void |
|
78
|
|
|
{ |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
protected function getConstantAnnotationBlock(PhpConstant $constant): ?PhpAnnotationBlock |
|
82
|
|
|
{ |
|
83
|
|
|
return null; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
protected function fillClassProperties(PropertyContainer $properties): void |
|
87
|
|
|
{ |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
protected function getPropertyAnnotationBlock(PhpProperty $property): ?PhpAnnotationBlock |
|
91
|
|
|
{ |
|
92
|
|
|
return null; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
40 |
|
protected function defineUseStatements(): AbstractModelFile |
|
96
|
|
|
{ |
|
97
|
40 |
|
$this->getFile()->addUse(SoapFault::class); |
|
98
|
|
|
|
|
99
|
|
|
/** @var Method $method */ |
|
100
|
40 |
|
foreach ($this->getModel()->getMethods() as $method) { |
|
|
|
|
|
|
101
|
40 |
|
$soapHeaderTypes = $method->getMetaValue(TagHeader::META_SOAP_HEADER_TYPES, []); |
|
102
|
40 |
|
if (!is_array($soapHeaderTypes)) { |
|
103
|
|
|
continue; |
|
104
|
|
|
} |
|
105
|
40 |
|
foreach ($soapHeaderTypes as $soapHeaderType) { |
|
106
|
12 |
|
$model = $this->getModelByName($soapHeaderType); |
|
107
|
12 |
|
if (!$model instanceof StructModel) { |
|
108
|
|
|
continue; |
|
109
|
|
|
} |
|
110
|
12 |
|
if (!$model->isRestriction()) { |
|
111
|
12 |
|
continue; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
2 |
|
$this->getFile()->addUse(InvalidArgumentException::class); |
|
115
|
|
|
|
|
116
|
2 |
|
break 2; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
40 |
|
return parent::defineUseStatements(); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
40 |
|
protected function getClassDeclarationLineText(): string |
|
124
|
|
|
{ |
|
125
|
40 |
|
return GeneratorOptions::VALUE_NONE === $this->getGenerator()->getOptionGatherMethods() ? 'This class stands for all operations' : parent::getClassDeclarationLineText(); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
40 |
|
protected function fillClassMethods(): void |
|
129
|
|
|
{ |
|
130
|
|
|
$this |
|
131
|
40 |
|
->addSoapHeaderMethods() |
|
132
|
40 |
|
->addOperationsMethods() |
|
133
|
40 |
|
->addGetResultMethod() |
|
134
|
|
|
; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
40 |
|
protected function addSoapHeaderMethods(): self |
|
138
|
|
|
{ |
|
139
|
40 |
|
foreach ($this->getModel()->getMethods() as $method) { |
|
140
|
40 |
|
$this->addSoapHeaderFromMethod($method); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
40 |
|
return $this; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
40 |
|
protected function addSoapHeaderFromMethod(MethodModel $method): self |
|
147
|
|
|
{ |
|
148
|
40 |
|
$soapHeaderNames = $method->getMetaValue(TagHeader::META_SOAP_HEADER_NAMES, []); |
|
149
|
40 |
|
$soapHeaderNamespaces = $method->getMetaValue(TagHeader::META_SOAP_HEADER_NAMESPACES, []); |
|
150
|
40 |
|
$soapHeaderTypes = $method->getMetaValue(TagHeader::META_SOAP_HEADER_TYPES, []); |
|
151
|
40 |
|
if (is_array($soapHeaderNames) && is_array($soapHeaderNamespaces) && is_array($soapHeaderTypes)) { |
|
152
|
40 |
|
foreach ($soapHeaderNames as $index => $soapHeaderName) { |
|
153
|
12 |
|
$methodName = $this->getSoapHeaderMethodName($soapHeaderName); |
|
154
|
12 |
|
if (is_null($this->methods->get($methodName))) { |
|
155
|
12 |
|
$soapHeaderNamespace = array_key_exists($index, $soapHeaderNamespaces) ? $soapHeaderNamespaces[$index] : null; |
|
156
|
12 |
|
$soapHeaderType = array_key_exists($index, $soapHeaderTypes) ? $soapHeaderTypes[$index] : null; |
|
157
|
12 |
|
$this->methods->add($this->getSoapHeaderMethod($methodName, $soapHeaderName, $soapHeaderNamespace, $soapHeaderType)); |
|
|
|
|
|
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
40 |
|
return $this; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
12 |
|
protected function getSoapHeaderMethod(string $methodName, string $soapHeaderName, string $soapHeaderNamespace, string $soapHeaderType): PhpMethod |
|
166
|
|
|
{ |
|
167
|
|
|
try { |
|
168
|
12 |
|
$method = new PhpMethod($methodName, [ |
|
169
|
12 |
|
$firstParameter = new PhpFunctionParameter(lcfirst($soapHeaderName), PhpFunctionParameterBase::NO_VALUE, $this->getTypeFromName($soapHeaderType)), |
|
170
|
12 |
|
new PhpFunctionParameterBase(self::PARAM_SET_HEADER_NAMESPACE, $soapHeaderNamespace, self::TYPE_STRING), |
|
171
|
12 |
|
new PhpFunctionParameterBase(self::PARAM_SET_HEADER_MUSTUNDERSTAND, false, self::TYPE_BOOL), |
|
172
|
12 |
|
new PhpFunctionParameterBase(self::PARAM_SET_HEADER_ACTOR, null, '?'.self::TYPE_STRING), |
|
173
|
|
|
], self::TYPE_SELF); |
|
174
|
|
|
|
|
175
|
12 |
|
$model = $this->getModelByName($soapHeaderType); |
|
176
|
12 |
|
if ($model instanceof StructModel) { |
|
177
|
12 |
|
$rules = new Rules($this, $method, new StructAttributeModel($model->getGenerator(), $soapHeaderType, $model->getName(), $model), $this->methods); |
|
178
|
12 |
|
$rules->applyRules(lcfirst($soapHeaderName)); |
|
179
|
12 |
|
$firstParameter->setModel($model); |
|
180
|
|
|
} |
|
181
|
12 |
|
$method->addChild(sprintf('return $this->%s($%s, \'%s\', $%s, $%s, $%s);', self::METHOD_SET_HEADER_PREFIX, self::PARAM_SET_HEADER_NAMESPACE, $soapHeaderName, lcfirst($soapHeaderName), self::PARAM_SET_HEADER_MUSTUNDERSTAND, self::PARAM_SET_HEADER_ACTOR)); |
|
182
|
|
|
} catch (InvalidArgumentException $exception) { |
|
183
|
|
|
throw new InvalidArgumentException(sprintf('Unable to create function parameter for service "%s" with type "%s"', $this->getModel()->getName(), var_export($this->getTypeFromName($soapHeaderName), true)), __LINE__, $exception); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
12 |
|
return $method; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
12 |
|
protected function getTypeFromName(string $name): ?string |
|
190
|
|
|
{ |
|
191
|
12 |
|
return self::getPhpType( |
|
192
|
12 |
|
$this->getStructAttributeTypeAsPhpType(new StructAttributeModel($this->generator, 'any', $name)), |
|
193
|
12 |
|
$this->getGenerator()->getOptionXsdTypesPath(), |
|
194
|
12 |
|
$this->getStructAttributeTypeAsPhpType(new StructAttributeModel($this->generator, 'any', $name)) |
|
195
|
|
|
); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
12 |
|
protected function getSoapHeaderMethodName(string $soapHeaderName): string |
|
199
|
|
|
{ |
|
200
|
12 |
|
return sprintf('%s%s', self::METHOD_SET_HEADER_PREFIX, ucfirst($soapHeaderName)); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
40 |
|
protected function addOperationsMethods(): self |
|
204
|
|
|
{ |
|
205
|
40 |
|
foreach ($this->getModel()->getMethods() as $method) { |
|
206
|
40 |
|
$this->addMainMethod($method); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
40 |
|
return $this; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
40 |
|
protected function addGetResultMethod(): self |
|
213
|
|
|
{ |
|
214
|
40 |
|
$method = new PhpMethod(self::METHOD_GET_RESULT); |
|
215
|
40 |
|
$method->addChild('return parent::getResult();'); |
|
216
|
40 |
|
$this->methods->add($method); |
|
217
|
|
|
|
|
218
|
40 |
|
return $this; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
40 |
|
protected function addMainMethod(MethodModel $method): self |
|
222
|
|
|
{ |
|
223
|
40 |
|
$methodFile = new Operation($method, $this->getGenerator()); |
|
224
|
40 |
|
$mainMethod = $methodFile->getMainMethod(); |
|
225
|
40 |
|
$this->methods->add($mainMethod); |
|
226
|
40 |
|
$this->setModelFromMethod($mainMethod, $method); |
|
227
|
|
|
|
|
228
|
40 |
|
return $this; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
40 |
|
protected function getMethodAnnotationBlock(PhpMethod $method): PhpAnnotationBlock |
|
232
|
|
|
{ |
|
233
|
40 |
|
$annotationBlock = new PhpAnnotationBlock(); |
|
234
|
40 |
|
if (0 === mb_stripos($method->getName(), self::METHOD_SET_HEADER_PREFIX)) { |
|
235
|
12 |
|
$this->addAnnotationBlockForSoapHeaderMethod($annotationBlock, $method); |
|
236
|
40 |
|
} elseif (self::METHOD_GET_RESULT === $method->getName()) { |
|
237
|
40 |
|
$this->addAnnotationBlockForgetResultMethod($annotationBlock); |
|
238
|
|
|
} else { |
|
239
|
40 |
|
$this->addAnnotationBlockForOperationMethod($annotationBlock, $method); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
40 |
|
return $annotationBlock; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
12 |
|
protected function addAnnotationBlockForSoapHeaderMethod(PhpAnnotationBlock $annotationBlock, PhpMethod $method): self |
|
246
|
|
|
{ |
|
247
|
12 |
|
$methodParameters = $method->getParameters(); |
|
248
|
12 |
|
$firstParameter = array_shift($methodParameters); |
|
249
|
12 |
|
if ($firstParameter instanceof PhpFunctionParameter) { |
|
250
|
12 |
|
$annotationBlock->addChild(sprintf('Sets the %s SoapHeader param', ucfirst($firstParameter->getName()))); |
|
251
|
12 |
|
$firstParameterType = $firstParameter->getType(); |
|
252
|
12 |
|
if ($firstParameter->getModel() instanceof StructModel) { |
|
253
|
12 |
|
$firstParameterType = $this->getStructAttributeTypeAsPhpType(new StructAttributeModel($firstParameter->getModel()->getGenerator(), $firstParameter->getName(), $firstParameter->getModel()->getName(), $firstParameter->getModel())); |
|
254
|
12 |
|
if ($firstParameter->getModel()->isRestriction()) { |
|
255
|
|
|
$annotationBlock |
|
256
|
2 |
|
->addChild(new PhpAnnotation(self::ANNOTATION_USES, sprintf('%s::%s()', $firstParameter->getModel()->getPackagedName(true), StructEnum::METHOD_VALUE_IS_VALID))) |
|
257
|
2 |
|
->addChild(new PhpAnnotation(self::ANNOTATION_USES, sprintf('%s::%s()', $firstParameter->getModel()->getPackagedName(true), StructEnum::METHOD_GET_VALID_VALUES))) |
|
258
|
2 |
|
->addChild(new PhpAnnotation(self::ANNOTATION_THROWS, InvalidArgumentException::class)) |
|
259
|
|
|
; |
|
260
|
|
|
} |
|
261
|
|
|
} |
|
262
|
|
|
$annotationBlock |
|
263
|
12 |
|
->addChild(new PhpAnnotation(self::ANNOTATION_USES, sprintf('%s::%s()', $this->getModel()->getExtends(true), self::METHOD_SET_HEADER_PREFIX))) |
|
264
|
12 |
|
->addChild(new PhpAnnotation(self::ANNOTATION_PARAM, sprintf('%s $%s', $firstParameterType, $firstParameter->getName()))) |
|
265
|
12 |
|
->addChild(new PhpAnnotation(self::ANNOTATION_PARAM, sprintf('%s $%s', self::TYPE_STRING, self::PARAM_SET_HEADER_NAMESPACE))) |
|
266
|
12 |
|
->addChild(new PhpAnnotation(self::ANNOTATION_PARAM, sprintf('%s $%s', self::TYPE_BOOL, self::PARAM_SET_HEADER_MUSTUNDERSTAND))) |
|
267
|
12 |
|
->addChild(new PhpAnnotation(self::ANNOTATION_PARAM, sprintf('%s|null $%s', self::TYPE_STRING, self::PARAM_SET_HEADER_ACTOR))) |
|
268
|
12 |
|
->addChild(new PhpAnnotation(self::ANNOTATION_RETURN, $this->getModel()->getPackagedName(true))) |
|
269
|
|
|
; |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
12 |
|
return $this; |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
40 |
|
protected function addAnnotationBlockForOperationMethod(PhpAnnotationBlock $annotationBlock, PhpMethod $method): self |
|
276
|
|
|
{ |
|
277
|
40 |
|
if (($model = $this->getModelFromMethod($method)) instanceof MethodModel) { |
|
278
|
40 |
|
$operationAnnotationBlock = new OperationAnnotationBlock($model, $this->getGenerator()); |
|
279
|
40 |
|
$operationAnnotationBlock->addAnnotationBlockForOperationMethod($annotationBlock); |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
40 |
|
return $this; |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
40 |
|
protected function addAnnotationBlockForgetResultMethod(PhpAnnotationBlock $annotationBlock): self |
|
286
|
|
|
{ |
|
287
|
|
|
$annotationBlock |
|
288
|
40 |
|
->addChild('Returns the result')->addChild(new PhpAnnotation(self::ANNOTATION_SEE, sprintf('%s::getResult()', $this->getModel()->getExtends(true)))) |
|
289
|
40 |
|
->addChild(new PhpAnnotation(self::ANNOTATION_RETURN, $this->getServiceReturnTypes())) |
|
290
|
|
|
; |
|
291
|
|
|
|
|
292
|
40 |
|
return $this; |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
40 |
|
protected function getServiceReturnTypes(): string |
|
296
|
|
|
{ |
|
297
|
40 |
|
$returnTypes = []; |
|
298
|
40 |
|
foreach ($this->getModel()->getMethods() as $method) { |
|
299
|
40 |
|
$returnTypes[] = self::getOperationMethodReturnType($method, $this->getGenerator()); |
|
300
|
|
|
} |
|
301
|
40 |
|
natcasesort($returnTypes); |
|
302
|
|
|
|
|
303
|
40 |
|
return implode('|', array_unique($returnTypes)); |
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
40 |
|
protected function getModelFromMethod(PhpMethod $method): ?MethodModel |
|
307
|
|
|
{ |
|
308
|
40 |
|
$model = $this->getGenerator()->getServiceMethod($method->getName()); |
|
309
|
40 |
|
if (!$model instanceof MethodModel) { |
|
310
|
7 |
|
$model = array_key_exists($method->getName(), $this->methodNames) ? $this->methodNames[$method->getName()] : null; |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
40 |
|
return $model; |
|
314
|
|
|
} |
|
315
|
|
|
|
|
316
|
40 |
|
protected function setModelFromMethod(PhpMethod $phpMethod, MethodModel $methodModel): self |
|
317
|
|
|
{ |
|
318
|
40 |
|
$this->methodNames[$phpMethod->getName()] = $methodModel; |
|
319
|
|
|
|
|
320
|
40 |
|
return $this; |
|
321
|
|
|
} |
|
322
|
|
|
} |
|
323
|
|
|
|