1
|
|
|
<?php |
2
|
|
|
namespace GoetasWebservices\SoapServices\SoapClient\StubGeneration; |
3
|
|
|
|
4
|
|
|
use Doctrine\Common\Inflector\Inflector; |
5
|
|
|
use GoetasWebservices\SoapServices\SoapClient\StubGeneration\Tag\MethodTag; |
6
|
|
|
use GoetasWebservices\SoapServices\SoapClient\StubGeneration\Tag\ParamTag; |
7
|
|
|
use GoetasWebservices\XML\WSDLReader\Wsdl\Message\Part; |
8
|
|
|
use GoetasWebservices\XML\WSDLReader\Wsdl\PortType; |
9
|
|
|
use GoetasWebservices\XML\WSDLReader\Wsdl\Service; |
10
|
|
|
use GoetasWebservices\Xsd\XsdToPhp\Naming\NamingStrategy; |
11
|
|
|
use GoetasWebservices\Xsd\XsdToPhp\Php\PhpConverter; |
12
|
|
|
use Zend\Code\Generator\ClassGenerator; |
13
|
|
|
use Zend\Code\Generator\DocBlockGenerator; |
14
|
|
|
|
15
|
|
|
class ClientStubGenerator |
16
|
|
|
{ |
17
|
|
|
protected $reservedWords = [ |
18
|
|
|
'int', |
19
|
|
|
'float', |
20
|
|
|
'bool', |
21
|
|
|
'string', |
22
|
|
|
'true', |
23
|
|
|
'false', |
24
|
|
|
'null', |
25
|
|
|
'resource', |
26
|
|
|
'object', |
27
|
|
|
'mixed', |
28
|
|
|
'numeric', |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
private $baseNs = [ |
32
|
|
|
'headers' => '\\SoapEnvelope\\Headers', |
33
|
|
|
'parts' => '\\SoapEnvelope\\Parts', |
34
|
|
|
'messages' => '\\SoapEnvelope\\Messages', |
35
|
|
|
]; |
36
|
|
|
/** |
37
|
|
|
* @var NamingStrategy |
38
|
|
|
*/ |
39
|
|
|
private $namingStrategy; |
40
|
|
|
/** |
41
|
|
|
* @var PhpConverter |
42
|
|
|
*/ |
43
|
|
|
private $phpConverter; |
44
|
|
|
/** |
45
|
|
|
* @var bool |
46
|
|
|
*/ |
47
|
|
|
private $unwrapReturn = false; |
48
|
|
|
|
49
|
|
|
public function __construct(PhpConverter $phpConverter, NamingStrategy $namingStrategy, $unwrapReturn = false, array $baseNs = array()) |
50
|
|
|
{ |
51
|
|
|
foreach ($baseNs as $k => $ns) { |
52
|
|
|
if (isset($this->baseNs[$k])) { |
53
|
|
|
$this->baseNs[$k] = $ns; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
$this->baseNs = $baseNs; |
57
|
|
|
$this->namingStrategy = $namingStrategy; |
58
|
|
|
$this->phpConverter = $phpConverter; |
59
|
|
|
$this->unwrapReturn = $unwrapReturn; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function setUnwrap($mode = true) |
63
|
|
|
{ |
64
|
|
|
$this->unwrapReturn = (bool)$mode; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param PortType[] $ports |
69
|
|
|
* @return ClassGenerator |
70
|
|
|
*/ |
71
|
|
|
public function generate(array $ports) |
72
|
|
|
{ |
73
|
|
|
$classes = array(); |
74
|
|
|
foreach ($ports as $port) { |
75
|
|
|
$class = new ClassGenerator(); |
76
|
|
|
if ($this->visitPortType($class, $port) !== false) { |
77
|
|
|
$classes[] = $class; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
return $classes; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
private function visitPortType(ClassGenerator $class, PortType $portType) |
84
|
|
|
{ |
85
|
|
|
if (!count($portType->getOperations())) { |
86
|
|
|
return false; |
87
|
|
|
} |
88
|
|
|
$docBlock = new DocBlockGenerator("Class representing " . $portType->getName()); |
89
|
|
|
$docBlock->setWordWrap(false); |
90
|
|
|
if ($portType->getDocumentation()) { |
91
|
|
|
$docBlock->setLongDescription($portType->getDocumentation()); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$namespaces = $this->phpConverter->getNamespaces(); |
95
|
|
|
$class->setNamespaceName($namespaces[$portType->getDefinition()->getTargetNamespace()] . "\\SoapStubs"); |
96
|
|
|
$class->setName(Inflector::classify($portType->getName())); |
97
|
|
|
$class->setDocblock($docBlock); |
98
|
|
|
|
99
|
|
|
foreach ($portType->getOperations() as $operation) { |
100
|
|
|
$operationTag = $this->visitOperation($operation); |
101
|
|
|
$docBlock->setTag($operationTag); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
private function visitOperation(PortType\Operation $operation) |
106
|
|
|
{ |
107
|
|
|
$types = $this->getOperationReturnTypes($operation); |
108
|
|
|
$operationTag = new MethodTag( |
109
|
|
|
Inflector::camelize($operation->getName()), |
110
|
|
|
$types, |
111
|
|
|
preg_replace("/[\n\r]+/", " ", $operation->getDocumentation()) |
112
|
|
|
); |
113
|
|
|
$params = $this->getOperationParams($operation); |
114
|
|
|
$operationTag->setParams($params); |
115
|
|
|
return $operationTag; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
private function getOperationParams(PortType\Operation $operation) |
119
|
|
|
{ |
120
|
|
|
if (!$operation->getInput()) { |
121
|
|
|
return []; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$parts = $operation->getInput()->getMessage()->getParts(); |
|
|
|
|
125
|
|
|
if (!$parts) { |
126
|
|
|
return []; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
if (count($parts) > 1) { |
130
|
|
|
$params = []; |
131
|
|
|
foreach ($parts as $part) { |
132
|
|
|
$partName = $this->namingStrategy->getPropertyName($part); |
133
|
|
|
$class = $this->getClassFromPart($part); |
134
|
|
|
|
135
|
|
|
$typeName = $class->getPhpType(); |
136
|
|
|
if ($t = $class->isSimpleType()){ |
137
|
|
|
$typeName = $t->getType()->getPhpType(); |
138
|
|
|
} |
139
|
|
|
$params[] = $param = new ParamTag($partName, [$typeName]); |
|
|
|
|
140
|
|
|
} |
141
|
|
|
} else { |
142
|
|
|
$params = $this->extractSinglePartParameters(reset($parts)); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $params; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
private function getOperationReturnTypes(PortType\Operation $operation) |
149
|
|
|
{ |
150
|
|
|
if (!$operation->getOutput() || !$operation->getOutput()->getMessage()->getParts()) { |
|
|
|
|
151
|
|
|
return ['void']; |
152
|
|
|
} |
153
|
|
|
$parts = $operation->getOutput()->getMessage()->getParts(); |
|
|
|
|
154
|
|
|
if (count($parts) > 1) { |
155
|
|
|
return ['array']; |
156
|
|
|
} |
157
|
|
|
/** |
158
|
|
|
* @var $part \GoetasWebservices\XML\WSDLReader\Wsdl\Message\Part |
159
|
|
|
*/ |
160
|
|
|
$part = reset($parts); |
161
|
|
|
|
162
|
|
|
$class = $this->getClassFromPart($part); |
163
|
|
|
if ($this->unwrapReturn) { |
164
|
|
|
foreach ($class->getProperties() as $property) { |
165
|
|
|
$propertyClass = $property->getType(); |
166
|
|
|
if ($t = $propertyClass->isSimpleType()){ |
167
|
|
|
return [$t->getType()->getPhpType()]; |
168
|
|
|
} |
169
|
|
|
return [$propertyClass->getPhpType()]; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
if ($t = $class->isSimpleType()){ |
174
|
|
|
return [$t->getType()->getPhpType()]; |
175
|
|
|
} |
176
|
|
|
return [$class->getPhpType()]; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
private function getClassFromPart(Part $part){ |
180
|
|
|
if ($part->getType()) { |
181
|
|
|
return $this->phpConverter->visitType($part->getType()); |
182
|
|
|
} else { |
183
|
|
|
return $this->phpConverter->visitElementDef($part->getElement()); |
|
|
|
|
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param $part |
189
|
|
|
* @return array |
190
|
|
|
*/ |
191
|
|
|
private function extractSinglePartParameters(Part $part) |
192
|
|
|
{ |
193
|
|
|
$params = []; |
194
|
|
|
$class = $this->getClassFromPart($part); |
195
|
|
|
|
196
|
|
|
foreach ($class->getProperties() as $property) { |
197
|
|
|
$t = $property->getType()->getPhpType(); |
198
|
|
|
$params[] = $param = new ParamTag($property->getName(), [$t]); |
|
|
|
|
199
|
|
|
} |
200
|
|
|
return $params; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.