|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace WsdlToPhp\PackageGenerator\File; |
|
6
|
|
|
|
|
7
|
|
|
use WsdlToPhp\PackageBase\AbstractSoapClientBase; |
|
8
|
|
|
use WsdlToPhp\PackageGenerator\Model\Method as MethodModel; |
|
9
|
|
|
use WsdlToPhp\PackageGenerator\Model\Service as ServiceModel; |
|
10
|
|
|
use WsdlToPhp\PackageGenerator\Model\Struct as StructModel; |
|
11
|
|
|
use WsdlToPhp\PackageGenerator\Parser\Wsdl\TagHeader; |
|
12
|
|
|
use WsdlToPhp\PhpGenerator\Element\PhpAnnotation; |
|
13
|
|
|
use WsdlToPhp\PhpGenerator\Element\PhpAnnotationBlock; |
|
14
|
|
|
|
|
15
|
|
|
final class Tutorial extends AbstractFile |
|
16
|
|
|
{ |
|
17
|
26 |
|
public function writeFile(): void |
|
18
|
|
|
{ |
|
19
|
26 |
|
$this |
|
20
|
26 |
|
->addMainAnnotationBlock() |
|
21
|
26 |
|
->addAutoload() |
|
22
|
26 |
|
->addOptionsAnnotationBlock() |
|
23
|
26 |
|
->addOptions() |
|
24
|
26 |
|
->addContent() |
|
25
|
26 |
|
; |
|
26
|
26 |
|
parent::writeFile(); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
26 |
|
public function addMainAnnotationBlock(): self |
|
30
|
|
|
{ |
|
31
|
26 |
|
$this->getFile()->addAnnotationBlockElement($this->getAnnotationBlock()); |
|
32
|
|
|
|
|
33
|
26 |
|
return $this; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
26 |
|
public function addChild(PhpAnnotationBlock $block, string $content): self |
|
37
|
|
|
{ |
|
38
|
26 |
|
$block->addChild(new PhpAnnotation(PhpAnnotation::NO_NAME, $content, AbstractModelFile::ANNOTATION_LONG_LENGTH)); |
|
39
|
|
|
|
|
40
|
26 |
|
return $this; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
26 |
|
public function addAutoload(): self |
|
44
|
|
|
{ |
|
45
|
26 |
|
if ($this->getGenerator()->getOptionStandalone()) { |
|
46
|
22 |
|
$this->getFile()->getMainElement()->addChild(sprintf('require_once __DIR__ . \'/vendor/autoload.php\';')); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
26 |
|
return $this; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
26 |
|
public function addContent(): self |
|
53
|
|
|
{ |
|
54
|
26 |
|
foreach ($this->getGenerator()->getServices(true) as $service) { |
|
55
|
26 |
|
$serviceVariableName = lcfirst($service->getName()); |
|
56
|
26 |
|
$this->addAnnotationBlockFromService($service)->addServiceDeclaration($serviceVariableName, $service)->addServiceSoapHeadersDefinitions($serviceVariableName, $service)->addContentFromService($serviceVariableName, $service); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
26 |
|
return $this; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
26 |
|
protected function getAnnotationBlock(): PhpAnnotationBlock |
|
63
|
|
|
{ |
|
64
|
26 |
|
$block = new PhpAnnotationBlock(); |
|
65
|
26 |
|
$this |
|
66
|
26 |
|
->addChild($block, 'This file aims to show you how to use this generated package.') |
|
67
|
26 |
|
->addChild($block, 'In addition, the goal is to show which methods are available and the first needed parameter(s)') |
|
68
|
26 |
|
->addChild($block, 'You have to use an associative array such as:') |
|
69
|
26 |
|
->addChild($block, '- the key must be a constant beginning with WSDL_ from AbstractSoapClientBase class (each generated ServiceType class extends this class)') |
|
70
|
26 |
|
->addChild($block, '- the value must be the corresponding key value (each option matches a {@link http://www.php.net/manual/en/soapclient.soapclient.php} option)') |
|
71
|
26 |
|
->addChild($block, '$options = [') |
|
72
|
26 |
|
->addChild($block, sprintf('%s::WSDL_URL => \'%s\',', AbstractSoapClientBase::class, $this->getGenerator()->getWsdl()->getName())) |
|
73
|
26 |
|
->addChild($block, sprintf('%s::WSDL_TRACE => true,', AbstractSoapClientBase::class)) |
|
74
|
26 |
|
->addChild($block, sprintf('%s::WSDL_LOGIN => \'you_secret_login\',', AbstractSoapClientBase::class)) |
|
75
|
26 |
|
->addChild($block, sprintf('%s::WSDL_PASSWORD => \'you_secret_password\',', AbstractSoapClientBase::class)) |
|
76
|
26 |
|
->addChild($block, '];') |
|
77
|
26 |
|
->addChild($block, 'etc...') |
|
78
|
26 |
|
; |
|
79
|
|
|
|
|
80
|
26 |
|
if (!$this->getGenerator()->getOptionStandalone()) { |
|
81
|
4 |
|
$this |
|
82
|
4 |
|
->addChild($block, '################################################################################') |
|
83
|
4 |
|
->addChild($block, 'Don\'t forget to add wsdltophp/packagebase:~5.0 to your main composer.json.') |
|
84
|
4 |
|
->addChild($block, '################################################################################') |
|
85
|
4 |
|
; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
26 |
|
return $block; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
26 |
|
protected function addOptionsAnnotationBlock(): self |
|
92
|
|
|
{ |
|
93
|
26 |
|
$this->addAnnotationBlock([ |
|
94
|
26 |
|
'Minimal options', |
|
95
|
26 |
|
]); |
|
96
|
|
|
|
|
97
|
26 |
|
return $this; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
26 |
|
protected function addOptions(): self |
|
101
|
|
|
{ |
|
102
|
26 |
|
$this |
|
103
|
26 |
|
->getFile() |
|
104
|
26 |
|
->getMainElement() |
|
105
|
26 |
|
->addChild('$options = [') |
|
106
|
26 |
|
->addChild($this->getFile()->getMainElement()->getIndentedString(sprintf('WsdlToPhp\PackageBase\AbstractSoapClientBase::WSDL_URL => \'%s\',', $this->getGenerator()->getWsdl()->getName()), 1)) |
|
107
|
26 |
|
->addChild($this->getFile()->getMainElement()->getIndentedString(sprintf('WsdlToPhp\PackageBase\AbstractSoapClientBase::WSDL_CLASSMAP => %s::%s(),', $this->getGenerator()->getFiles()->getClassmapFile()->getModel()->getPackagedName(true), ClassMap::METHOD_NAME), 1)) |
|
108
|
26 |
|
->addChild('];') |
|
109
|
26 |
|
; |
|
110
|
|
|
|
|
111
|
26 |
|
return $this; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
26 |
|
protected function addAnnotationBlockFromService(ServiceModel $service): self |
|
115
|
|
|
{ |
|
116
|
26 |
|
return $this->addAnnotationBlock([ |
|
117
|
26 |
|
sprintf('Samples for %s ServiceType', $service->getName()), |
|
118
|
26 |
|
]); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
26 |
|
protected function addContentFromService(string $serviceVariableName, ServiceModel $service): self |
|
122
|
|
|
{ |
|
123
|
26 |
|
foreach ($service->getMethods() as $method) { |
|
124
|
26 |
|
$this->addAnnotationBlockFromMethod($method)->addContentFromMethod($serviceVariableName, $method); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
26 |
|
return $this; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
26 |
|
protected function addServiceDeclaration(string $serviceVariableName, ServiceModel $service): self |
|
131
|
|
|
{ |
|
132
|
26 |
|
$this->getFile()->getMainElement()->addChild(sprintf('$%s = new %s($options);', $serviceVariableName, $service->getPackagedName(true))); |
|
133
|
|
|
|
|
134
|
26 |
|
return $this; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
26 |
|
protected function addServiceSoapHeadersDefinitions(string $serviceVariableName, ServiceModel $service): self |
|
138
|
|
|
{ |
|
139
|
26 |
|
$added = []; |
|
140
|
26 |
|
foreach ($service->getMethods() as $method) { |
|
141
|
26 |
|
$added = array_merge($added, $this->addServiceSoapHeadersDefinition($serviceVariableName, $method, $added)); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
26 |
|
return $this; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
26 |
|
protected function addServiceSoapHeadersDefinition(string $serviceVariableName, MethodModel $method, array $added): array |
|
148
|
|
|
{ |
|
149
|
26 |
|
$addedNames = []; |
|
150
|
26 |
|
$soapHeaderNames = $method->getMetaValue(TagHeader::META_SOAP_HEADER_NAMES, []); |
|
151
|
26 |
|
foreach ($soapHeaderNames as $soapHeaderName) { |
|
152
|
4 |
|
if (in_array($soapHeaderName, $added, true)) { |
|
153
|
2 |
|
continue; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
4 |
|
$addedNames[] = $soapHeaderName; |
|
157
|
4 |
|
$this->getFile()->getMainElement()->addChild(sprintf('$%s->%s%s(%s);', $serviceVariableName, Service::METHOD_SET_HEADER_PREFIX, ucfirst($soapHeaderName), $this->getMethodParameter($soapHeaderName))); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
26 |
|
return $addedNames; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
26 |
|
protected function addAnnotationBlockFromMethod(MethodModel $method): self |
|
164
|
|
|
{ |
|
165
|
26 |
|
return $this->addAnnotationBlock([ |
|
166
|
26 |
|
sprintf('Sample call for %s operation/method', $method->getMethodName()), |
|
167
|
26 |
|
]); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
26 |
|
protected function addContentFromMethod(string $serviceVariableName, MethodModel $method): self |
|
171
|
|
|
{ |
|
172
|
26 |
|
$this |
|
173
|
26 |
|
->getFile() |
|
174
|
26 |
|
->getMainElement() |
|
175
|
26 |
|
->addChild(sprintf('if ($%s->%s(%s) !== false) {', $serviceVariableName, $method->getMethodName(), $this->getMethodParameters($method))) |
|
176
|
26 |
|
->addChild($this->getFile()->getMainElement()->getIndentedString(sprintf('print_r($%s->getResult());', $serviceVariableName), 1)) |
|
177
|
26 |
|
->addChild('} else {') |
|
178
|
26 |
|
->addChild($this->getFile()->getMainElement()->getIndentedString(sprintf('print_r($%s->getLastError());', $serviceVariableName), 1)) |
|
179
|
26 |
|
->addChild('}') |
|
180
|
26 |
|
; |
|
181
|
|
|
|
|
182
|
26 |
|
return $this; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
26 |
|
protected function getMethodParameters(MethodModel $method): string |
|
186
|
|
|
{ |
|
187
|
26 |
|
$parameters = []; |
|
188
|
26 |
|
if (is_array($method->getParameterType())) { |
|
189
|
24 |
|
foreach ($method->getParameterType() as $parameterName => $parameterType) { |
|
190
|
24 |
|
$parameters[] = $this->getMethodParameter($parameterType, $parameterName); |
|
191
|
|
|
} |
|
192
|
|
|
} else { |
|
193
|
8 |
|
$parameters[] = $this->getMethodParameter($method->getParameterType()); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
26 |
|
return implode(', ', $parameters); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
26 |
|
protected function getMethodParameter(?string $parameterType, ?string $parameterName = null): string |
|
200
|
|
|
{ |
|
201
|
26 |
|
$parameter = sprintf('%1$s%2$s', (empty($parameterType) && empty($parameterName)) ? '' : '$', empty($parameterName) ? $parameterType : $parameterName); |
|
202
|
26 |
|
$model = null !== $parameterType ? $this->getGenerator()->getStructByName($parameterType) : null; |
|
203
|
26 |
|
if ($model instanceof StructModel && $model->isStruct() && !$model->isRestriction()) { |
|
204
|
26 |
|
$parameter = sprintf('new %s()', $model->getPackagedName(true)); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
26 |
|
return $parameter; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
26 |
|
protected function addAnnotationBlock($content): self |
|
211
|
|
|
{ |
|
212
|
26 |
|
$this->getFile()->getMainElement()->addChild(new PhpAnnotationBlock($content)); |
|
213
|
|
|
|
|
214
|
26 |
|
return $this; |
|
215
|
|
|
} |
|
216
|
|
|
} |
|
217
|
|
|
|