1
|
|
|
<?php |
|
|
|
|
2
|
|
|
use Ouzo\Utilities\Arrays; |
3
|
|
|
use Ouzo\Utilities\Functions; |
4
|
|
|
use WSDL\Annotation\BindingType; |
5
|
|
|
use WSDL\Annotation\SoapBinding; |
6
|
|
|
use WSDL\Builder\Method; |
7
|
|
|
use WSDL\Builder\Parameter; |
8
|
|
|
use WSDL\Builder\WSDLBuilder; |
9
|
|
|
use WSDL\Lexer\Tokenizer; |
10
|
|
|
use WSDL\WSDL; |
11
|
|
|
|
12
|
|
|
require_once '../../vendor/autoload.php'; |
13
|
|
|
|
14
|
|
|
ini_set("soap.wsdl_cache_enabled", 0); |
15
|
|
|
|
16
|
|
|
$tokenizer = new Tokenizer(); |
17
|
|
|
|
18
|
|
|
$parameters1 = [Parameter::fromTokens($tokenizer->lex('string $userName'))]; |
19
|
|
|
$return1 = Parameter::fromTokens($tokenizer->lex('string $uppercasedUserName')); |
20
|
|
|
|
21
|
|
|
$parameters2 = [ |
22
|
|
|
Parameter::fromTokens($tokenizer->lex('int[] $numbers')), |
23
|
|
|
Parameter::fromTokens($tokenizer->lex('string $prefix')) |
24
|
|
|
]; |
25
|
|
|
$return2 = Parameter::fromTokens($tokenizer->lex('string[] $numbersWithPrefix')); |
26
|
|
|
|
27
|
|
|
$parameters3 = [Parameter::fromTokens($tokenizer->lex('object $user { string $name int $age }'))]; |
28
|
|
|
$return3 = Parameter::fromTokens($tokenizer->lex('object $userContext { int $id object $userInfo { string $name int $age } }')); |
29
|
|
|
|
30
|
|
|
$parameters4 = [Parameter::fromTokens($tokenizer->lex('object[] $companies { string $name int $postcode }'))]; |
31
|
|
|
$return4 = Parameter::fromTokens($tokenizer->lex('string[] $companiesNames')); |
32
|
|
|
|
33
|
|
|
$parameters5 = [Parameter::fromTokens($tokenizer->lex('string[] $errors'))]; |
34
|
|
|
$return5 = Parameter::fromTokens($tokenizer->lex('object $result { boolean $result string[] $errors }')); |
35
|
|
|
|
36
|
|
|
$parameters6 = [ |
37
|
|
|
Parameter::fromTokens($tokenizer->lex('object $serviceAuth { string $token int $id }'), true), |
38
|
|
|
Parameter::fromTokens($tokenizer->lex('string $name')), |
39
|
|
|
Parameter::fromTokens($tokenizer->lex('string $surname')) |
40
|
|
|
]; |
41
|
|
|
$return6 = Parameter::fromTokens($tokenizer->lex('string $nameWithSurname')); |
42
|
|
|
|
43
|
|
|
$parameters7 = [Parameter::fromTokens($tokenizer->lex('string $userToken'))]; |
44
|
|
|
|
45
|
|
|
$return8 = Parameter::fromTokens($tokenizer->lex('string $responseForMethodWithoutParameters')); |
46
|
|
|
|
47
|
|
|
$builder = WSDLBuilder::instance() |
48
|
|
|
->setName('RpcLiteralService') |
49
|
|
|
->setTargetNamespace('http://foo.bar/rpcliteralservice') |
50
|
|
|
->setNs('http://foo.bar/rpcliteralservice/types') |
51
|
|
|
->setLocation('http://localhost:7777/wsdl-creator/examples/rpc_literal/service.php') |
52
|
|
|
->setStyle(SoapBinding::RPC) |
53
|
|
|
->setUse(SoapBinding::LITERAL) |
54
|
|
|
->setSoapVersion(BindingType::SOAP_11) |
55
|
|
|
->setMethod(new Method('uppercaseUserName', $parameters1, $return1)) |
56
|
|
|
->setMethod(new Method('appendPrefixToNumbers', $parameters2, $return2)) |
57
|
|
|
->setMethod(new Method('getUserContext', $parameters3, $return3)) |
58
|
|
|
->setMethod(new Method('extractCompaniesNames', $parameters4, $return4)) |
59
|
|
|
->setMethod(new Method('wrapErrors', $parameters5, $return5)) |
60
|
|
|
->setMethod(new Method('authorizedMethod', $parameters6, $return6)) |
61
|
|
|
->setMethod(new Method('methodWithoutReturn', $parameters7, null)) |
62
|
|
|
->setMethod(new Method('methodWithoutParameters', [], $return8)); |
63
|
|
|
|
64
|
|
|
$wsdl = WSDL::fromBuilder($builder); |
65
|
|
|
|
66
|
|
|
if (isset($_GET['wsdl'])) { |
67
|
|
|
header("Content-Type: text/xml"); |
68
|
|
|
echo $wsdl->create(); |
69
|
|
|
exit; |
70
|
|
|
} |
71
|
|
|
$server = new SoapServer('http://localhost:7777/wsdl-creator/examples/rpc_literal/service.php?wsdl', [ |
72
|
|
|
'uri' => $builder->getTargetNamespace(), |
73
|
|
|
'location' => $builder->getLocation(), |
74
|
|
|
'style' => SOAP_RPC, |
75
|
|
|
'use' => SOAP_LITERAL |
76
|
|
|
]); |
77
|
|
|
$server->setClass('RpcLiteralService'); |
78
|
|
|
$server->handle(); |
79
|
|
|
|
80
|
|
|
class RpcLiteralService |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
private $clientId = null; |
83
|
|
|
|
84
|
|
|
public function uppercaseUserName($userName) |
85
|
|
|
{ |
86
|
|
|
return strtoupper($userName); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function appendPrefixToNumbers($numbers, $prefix) |
90
|
|
|
{ |
91
|
|
|
return Arrays::map($numbers, Functions::prepend($prefix)); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function getUserContext($user) |
95
|
|
|
{ |
96
|
|
|
$userContext = new stdClass(); |
97
|
|
|
$userContext->id = time(); |
98
|
|
|
$userContext->UserInfo = new stdClass(); |
99
|
|
|
$userContext->UserInfo->name = $user->name; |
100
|
|
|
$userContext->UserInfo->age = $user->age; |
101
|
|
|
return $userContext; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function extractCompaniesNames($companies) |
105
|
|
|
{ |
106
|
|
|
return Arrays::map($companies, Functions::extractField('name')); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function wrapErrors($errors) |
110
|
|
|
{ |
111
|
|
|
$result = new stdClass(); |
112
|
|
|
$result->result = false; |
113
|
|
|
$result->errors = $errors; |
114
|
|
|
return $result; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function serviceAuth($object) |
118
|
|
|
{ |
119
|
|
|
if ($object->token != 'test_token') { |
120
|
|
|
throw new SoapFault('WT', 'Wrong token'); |
121
|
|
|
} else { |
122
|
|
|
$this->clientId = $object->id; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function authorizedMethod($name, $surname) |
127
|
|
|
{ |
128
|
|
|
return 'clientId [' . $this->clientId . '] name [' . $name . '] surname [' . $surname . ']'; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function methodWithoutReturn($userToken) |
132
|
|
|
{ |
133
|
|
|
file_put_contents('/tmp/wsdl-creator-example-log', $userToken . PHP_EOL, FILE_APPEND); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function methodWithoutParameters() |
137
|
|
|
{ |
138
|
|
|
return 'method without parameters'; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.