WSHelper.class.php
5.2 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/**
* Class that generates a WSDL file and creates documentation
* for the webservices.
*
*@author KnowledgeTree Team
*@package Webservice
*@version Version 0.9
*/
class WSHelper {
private $uri;
private $class = null; //IPReflectionClass object
private $name; //class name
private $persistence = SOAP_PERSISTENCE_SESSION;
private $wsdlfile; //wsdl file name
private $server; //soap server object
public $actor;
public $structureMap = array();
public $classNameArr = array();
public $wsdlFolder; //WSDL cache folder
public $useWSDLCache = true;
public $type = SOAP_RPC;
public $use = SOAP_LITERAL;
/**
* Constructor
* @param string The Uri name
* @return void
*/
public function __construct($uri, $class=null){
$this->uri = $uri;
$this->setWSDLCacheFolder($_SERVER['DOCUMENT_ROOT'].dirname($_SERVER['PHP_SELF'])."/wsdl/");
if($class) $this->setClass($class);
}
/**
* Adds the given class name to the list of classes
* to be included in the documentation/WSDL/Request handlers
* @param string
* @return void
*/
public function setClass($name){
$this->name = $name;
$this->wsdlfile = $this->wsdlFolder.$this->name.".wsdl";
}
public function setWSDLCacheFolder($folder) {
$this->wsdlFolder = $folder;
//reset wsdlfile
$this->wsdlfile = $this->wsdlFolder.$this->name.".wsdl";
}
/**
* Sets the persistence level for the soap class
*/
public function setPersistence($persistence) {
$this->persistence = $persistence;
}
/**
* Handles everything. Makes sure the webservice is handled,
* documentations is generated, or the wsdl is generated,
* according to the page request
* @return void
*/
public function handle(){
if(substr($_SERVER['QUERY_STRING'], -4) == 'wsdl'){
$this->showWSDL();
}elseif(isset($GLOBALS['HTTP_RAW_POST_DATA']) && strlen($GLOBALS['HTTP_RAW_POST_DATA'])>0){
$this->handleRequest();
}else{
$this->createDocumentation();
}
}
/**
* Checks if the current WSDL is up-to-date, regenerates if necessary and outputs the WSDL
* @return void
*/
public function showWSDL(){
//check if it's a legal webservice class
if(!in_array($this->name, $this->classNameArr))
throw new Exception("No valid webservice class.");
//@TODO: nog een mooie oplossing voor het cachen zoeken
header("Content-type: text/xml");
if($this->useWSDLCache && file_exists($this->wsdlfile)){
readfile($this->wsdlfile);
}else{
//make sure to refresh PHP WSDL cache system
ini_set("soap.wsdl_cache_enabled",0);
echo $this->createWSDL();
}
}
private function createWSDL(){
$this->class = new IPReflectionClass($this->name);
$wsdl = new WSDLStruct($this->uri, "http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME']."?class=".$this->name, $this->type, $this->use);
$wsdl->setService($this->class);
try {
$gendoc = $wsdl->generateDocument();
} catch (WSDLException $exception) {
$exception->Display();
exit();
}
$fh = fopen($this->wsdlfile, "w+");
fwrite($fh, $gendoc);
fclose($fh);
return $gendoc;
}
/**
* Lets the native PHP5 soap implementation handle the request
* after registrating the class
* @return void
*/
private function handleRequest(){
//check if it's a legal webservice class
if(!in_array($this->name, $this->classNameArr))
throw new Exception("No valid webservice class.");
//check cache
//if(!file_exists($this->wsdlfile))
$this->createWSDL();
$options = Array('actor' => $this->actor, 'classmap' => $this->structureMap);
header("Content-type: text/xml");
$this->server = new SoapServer($this->wsdlfile, $options);
$this->server->setClass($this->name);
$this->server->setPersistence($this->persistence);
use_soap_error_handler(true);
$this->server->handle();
}
/**
* @param string code
* @param string string
* @param string actor
* @param mixed details
* @param string name
* @return void
*/
public function fault($code, $string, $actor, $details, $name='') {
return $this->server->fault($code, $string, $actor, $details, $name);
}
/**
* Generates the documentations for the webservice usage.
* @TODO: "int", "boolean", "double", "float", "string", "void"
* @param string Template filename
* @return void
*/
public function createDocumentation($template="classes/soap/templates/docclass.xsl") {
if(!is_file($template))
throw new WSException("Could not find the template file: '$template'");
$this->class = new IPReflectionClass($this->name);
$xtpl = new IPXSLTemplate($template);
$documentation = Array();
$documentation['menu'] = Array();
//loop menu items
sort($this->classNameArr);
foreach($this->classNameArr as $className) {
$documentation['menu'][] = new IPReflectionClass($className);
}
if($this->class){
$this->class->properties = $this->class->getProperties(false, false);
$this->class->methods = $this->class->getMethods(false, false);
foreach((array)$this->class->methods as $method) {
$method->params = $method->getParameters();
}
$documentation['class'] = $this->class;
}
echo $xtpl->execute($documentation);
}
}
?>