summaryrefslogtreecommitdiff
path: root/framework/3rdParty/WsdlGen/WsdlGenerator.php
blob: 3d78876998e3e055396f45039be045d7db002e5c (plain)
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<?php
/**
 * WsdlGenerator file.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the BSD License.
 *
 * Copyright(c) 2005 by Marcus Nyeholt. All rights reserved.
 *
 * To contact the author write to {@link mailto:tanus@users.sourceforge.net Marcus Nyeholt}
 * This file is part of the PRADO framework from {@link http://www.xisc.com}
 *
 * @author Marcus Nyeholt		<tanus@users.sourceforge.net>
 * @version $Id$
 * @package System.Web.Services.SOAP
 */

require_once(dirname(__FILE__).'/Wsdl.php');
require_once(dirname(__FILE__).'/WsdlMessage.php');
require_once(dirname(__FILE__).'/WsdlOperation.php');

/**
 * Generator for the wsdl. 
 * Special thanks to Cristian Losada for implementing the Complex Types section of the WSDL.
 * @author 		Marcus Nyeholt		<tanus@users.sourceforge.net>
 * @author 		Cristian Losada		<cristian@teaxul.com>
 * @version 	$Revision$
 */
class WsdlGenerator
{
	/**
	 * The instance.
	 * var		WsdlGenerator
	 */
	private static $instance;
	
	/**
	 * The name of this service (the classname)
	 * @var 	string
	 */
	private $serviceName = '';
	
	/**
	 * The complex types to use in the wsdl
	 * @var 	Array
	 */
	private $types = array();
	
	/**
	 * The operations available in this wsdl
	 * @var 	ArrayObject
	 */
	private $operations;
	
	/**
	 * The wsdl object.
	 * @var 	object
	 */
	private $wsdlDocument;
	
	/**
	 * The actual wsdl string
	 * @var 	string
	 */
	private $wsdl = '';
	
	/**
	 * The singleton instance for the generator
	 */
	public static function getInstance()
	{
		if (is_null(self::$instance)) {
			self::$instance = new WsdlGenerator();
		}
		return self::$instance;
	}
	
	/**
	 * Get the Wsdl generated
	 * @return 	string		The Wsdl for this wsdl
	 */
	public function getWsdl()
	{
		return $this->wsdl;
	}
	
	/**
	 * Generates WSDL for a passed in class, and saves it in the current object. The
	 * WSDL can then be retrieved by calling 
	 * @param 	string		$className		The name of the class to generate for
	 * @param 	string		$serviceUri		The URI of the service that handles this WSDL
	 * @return 	void
	 */
	public function generateWsdl($className, $serviceUri='')
	{
		$this->wsdlDocument = new Wsdl($className, $serviceUri);
					
		$classReflect = new ReflectionClass($className);		
		$methods = $classReflect->getMethods();
		
		foreach ($methods as $method) {
			// Only process public methods
			if ($method->isPublic()) {
				$this->processMethod($method);
			}
		}
		
		foreach($this->types as $type => $elements) {
			$this->wsdlDocument->addComplexType($type, $elements);
		}
				
		$this->wsdl = $this->wsdlDocument->getWsdl();
	}
	
	/**
	 * Static method that generates and outputs the generated wsdl
	 * @param 		string		$className		The name of the class to export
	 * @param 		string		$serviceUri		The URI of the service that handles this WSDL
	 */
	public static function generate($className, $serviceUri='')
	{
		$generator = WsdlGenerator::getInstance();
		$generator->generateWsdl($className, $serviceUri);		
		//header('Content-type: text/xml');
		return $generator->getWsdl();
		//exit();
		
	}
	
	/**
	 * Process a method found in the passed in class.
	 * @param 		ReflectionMethod		$method		The method to process
	 */
	protected function processMethod(ReflectionMethod $method)
	{
		$comment = $method->getDocComment();
		
		if (strpos($comment, '@soapmethod') === false) {
			return;
		}
		$comment = preg_replace("/(^[\\s]*\\/\\*\\*)
                                 |(^[\\s]\\*\\/)
                                 |(^[\\s]*\\*?\\s)
                                 |(^[\\s]*)
                                 |(^[\\t]*)/ixm", "", $comment);

	    $comment = str_replace("\r", "", $comment);
	    $comment = preg_replace("/([\\t])+/", "\t", $comment);
	    $commentLines = explode("\n", $comment);
	    
		$methodDoc = '';
		$params = array();
		$return = array();
		$gotDesc = false;
		$gotParams = false;
		
		foreach ($commentLines as $line) {
			if ($line == '') continue;
			if ($line{0} == '@') {
				$gotDesc = true;
				if (preg_match('/^@param\s+([\w\[\]()]+)\s+\$([\w()]+)\s*(.*)/i', $line, $match)) {
					$param = array();
					$param['type'] = $this->convertType($match[1]);					
					$param['name'] = $match[2];
					$param['desc'] = $match[3];					
					$params[] = $param;
				}
				else if (preg_match('/^@return\s+([\w\[\]()]+)\s*(.*)/i', $line, $match)) {			
					$gotParams = true;
					$return['type'] = $this->convertType($match[1]);
					$return['desc'] = $match[2];
					$return['name'] = 'return';
				}
			}
			else {
				if (!$gotDesc) {
					$methodDoc .= trim($line);
				}
				else if (!$gotParams) {
					$params[count($params)-1]['desc'] .= trim($line);
				}
				else {
					if ($line == '*/') continue;
					$return['desc'] .= trim($line);
				}
			}
		}
		
		$methodName = $method->getName();
		$operation = new WsdlOperation($methodName, $methodDoc);
		
		$operation->setInputMessage(new WsdlMessage($methodName.'Request', $params));
		$operation->setOutputMessage(new WsdlMessage($methodName.'Response', array($return)));
		
		$this->wsdlDocument->addOperation($operation);
		
	}
	
	/**
	 * Converts from a PHP type into a WSDL type. This is borrowed from 
	 * Cerebral Cortex (let me know and I'll remove asap).
	 *
	 * TODO: date and dateTime
	 * @param 		string		$type		The php type to convert
	 * @return 		string					The XSD type.
	 */
	private function convertType($type)
	{
		 switch ($type) {
             case 'string':
             case 'str':
                 return 'xsd:string';
                 break;
             case 'int':
             case 'integer':
                 return 'xsd:int';
                 break;
             case 'float':
             case 'double':
                 return 'xsd:float';
                 break;
             case 'boolean':
             case 'bool':
                 return 'xsd:boolean';
                 break;
             case 'array':
                 return 'soap-enc:Array';
                 break;
             case 'object':
                 return 'xsd:struct';
                 break;
             case 'mixed':
                 return 'xsd:anyType';
                 break;
             case 'void':
                 return '';
             default:    
             	 if(strpos($type, '[]'))  // if it is an array
             	 { 
             	 	$className = substr($type, 0, strlen($type) - 2);
             	 	$type = $className . 'Array';
             	 	$this->types[$type] = '';
             	 	$this->convertType($className);
             	 } 
             	 else 
             	 {
             	 	 if(!isset($this->types[$type])) 
	             		$this->extractClassProperties($type);
             	 }             	 
                 return 'tns:' . $type;   
         }
	}
	
	/**
	 * Extract the type and the name of all properties of the $className class and saves it in the $types array
	 * This method extract properties from PHPDoc formatted comments for variables. Unfortunately the reflectionproperty
	 * class doesn't have a getDocComment method to extract comments about it, so we have to extract the information
	 * about the variables manually. Thanks heaps to Cristian Losada for implementing this.
	 * @param string $className The name of the class  
	 */
	private function extractClassProperties($className) 
	{
		// Lets get the class' file so we can read the full contents of it.
		$classReflect = new ReflectionClass($className);
		
		if (!file_exists($classReflect->getFileName())) {
			throw new Exception('Could not find class file for '.$className);
		}
		
		$file = file_get_contents($classReflect->getFileName());
		if ($file == '') {
			throw new Exception("File {$classReflect->getFileName()} could not be opened");
		}
		
		$pos = strpos($file, 'class ' . $className);
		$pos = strpos($file, '@var', $pos) + 5;
		for($t = 0; $pos > 5; $t++) 
		{	
			$type = $this->convertType(substr($file, $pos, strpos($file, ' ', $pos) - $pos));
			$pos = strpos($file, '$', $pos) + 1;
			$name = substr($file, $pos, strpos($file, ';', $pos) - $pos);			
			$this->types[$className][$t] = array('type' => $type, 'name' => $name);  
			$pos = strpos($file, '@var', $pos) + 5;
		}
	}
	
}
?>