forked from subdee/yii2-soap-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSoapAction.php
150 lines (138 loc) · 5.38 KB
/
SoapAction.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
/**
* SoapAction class file.
*
* @author Konstantinos Thermos <[email protected]>
*/
/**
* This class is based on the WsdlGenerator class from the Yii 1 framework.
*
* @link https://github.com/yiisoft/yii/blob/1.1.14/framework/web/services/CWebServiceAction.php
*
* SoapAction implements an action that provides Web services.
*
* SoapAction serves for two purposes. On the one hand, it displays
* the WSDL content specifying the Web service APIs. On the other hand, it
* invokes the requested Web service API. A GET parameter named <code>ws</code>
* is used to differentiate these two aspects: the existence of the GET parameter
* indicates performing the latter action.
*
* By default, SoapAction will use the current controller as
* the Web service provider. See {@link CWsdlGenerator} on how to declare
* methods that can be remotely invoked.
*
* Note, PHP SOAP extension is required for this action.
*
* @property SoapService $service The Web service instance.
*
* @author Qiang Xue <[email protected]>
* @package system.web.services
* @since 1.0
*/
namespace subdee\soapserver;
use yii\base\Action;
use yii\web\Response;
class SoapAction extends Action
{
/**
* @var mixed the Web service provider object or class name.
* If specified as a class name, it can be a path alias.
* Defaults to null, meaning the current controller is used as the service provider.
* If the provider implements the interface {@link IWebServiceProvider},
* it will be able to intercept the remote method invocation and perform
* additional tasks (e.g. authentication, logging).
*/
public $provider;
/**
* @var string the URL for the Web service. Defaults to null, meaning
* the URL for this action is used to provide Web services.
* In this case, a GET parameter named {@link serviceVar} will be used to
* deteremine whether the current request is for WSDL or Web service.
*/
public $serviceUrl;
/**
* @var string the URL for WSDL. Defaults to null, meaning
* the URL for this action is used to serve WSDL document.
*/
public $wsdlUrl;
/**
* @var string the name of the GET parameter that differentiates a WSDL request
* from a Web service request. If this GET parameter exists, the request is considered
* as a Web service request; otherwise, it is a WSDL request. Defaults to 'ws'.
*/
public $serviceVar = 'ws';
/**
* @var array a list of PHP classes that are declared as complex types in WSDL.
* This should be an array with WSDL types as keys and names of PHP classes as values.
* A PHP class can also be specified as a path alias.
* @see http://www.php.net/manual/en/soapclient.soapclient.php
*/
public $classMap;
/**
* @var array the initial property values for the {@link CWebService} object.
* The array keys are property names of {@link CWebService} and the array values
* are the corresponding property initial values.
*/
public $serviceOptions = [];
public $wsdlOptions = [];
private $_service;
/**
* Runs the action.
* If the GET parameter {@link serviceVar} exists, the action handle the remote method invocation.
* If not, the action will serve WSDL content;
*/
public function run()
{
\Yii::$app->response->format = Response::FORMAT_RAW;
$hostInfo = \Yii::$app->request->hostInfo;
$controller = $this->controller;
if (($serviceUrl = $this->serviceUrl) === null) {
$serviceUrl = $hostInfo . \Yii::$app->urlManager->createUrl(
[$controller->uniqueId . '/' . $this->id, $this->serviceVar => 1]
);
}
if (($wsdlUrl = $this->wsdlUrl) === null) {
$wsdlUrl = $hostInfo . \Yii::$app->urlManager->createUrl($controller->uniqueId . '/' . $this->id);
}
if (($provider = $this->provider) === null) {
$provider = $controller;
}
$this->_service = $this->createWebService($provider, $wsdlUrl, $serviceUrl);
if (is_array($this->classMap)) {
$this->_service->classMap = $this->classMap;
}
foreach ($this->serviceOptions as $name => $value) {
$this->_service->$name = $value;
}
if (isset($_GET[$this->serviceVar])) {
return $this->_service->run();
} else {
$wsdl = $this->_service->generateWsdl();
header('Content-Type: text/xml;charset=' . \Yii::$app->response->charset);
header('Content-Length: ' . (function_exists('mb_strlen') ? mb_strlen($wsdl, '8bit') : strlen($wsdl)));
return $wsdl;
}
}
/**
* Creates a {@link CWebService} instance.
* You may override this method to customize the created instance.
*
* @param mixed $provider the web service provider class name or object
* @param string $wsdlUrl the URL for WSDL.
* @param string $serviceUrl the URL for the Web service.
* @return SoapService the Web service instance
*/
protected function createWebService($provider, $wsdlUrl, $serviceUrl)
{
return new SoapService($provider, $wsdlUrl, $serviceUrl, $this->wsdlOptions);
}
/**
* Returns the Web service instance currently being used.
*
* @return SoapService the Web service instance
*/
public function getService()
{
return $this->_service;
}
}