PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Service.php

http://wsdl2phpgenerator.googlecode.com/
PHP | 246 lines | 135 code | 38 blank | 73 comment | 15 complexity | bc02ba3407c454684a3222077eecdec6 MD5 | raw file
  1. <?php
  2. /**
  3. * @package Wsdl2PhpGenerator
  4. */
  5. /**
  6. * @see phpSourcePhpClass
  7. */
  8. require_once dirname(__FILE__).'/../lib/phpSource/PhpClass.php';
  9. /**
  10. * @see phpSourcePhpDocElementFactory.php
  11. */
  12. require_once dirname(__FILE__).'/../lib/phpSource/PhpDocElementFactory.php';
  13. /**
  14. * @see Operation
  15. */
  16. require_once dirname(__FILE__).'/Operation.php';
  17. /**
  18. * Service represents the service in the wsdl
  19. *
  20. * @package Wsdl2PhpGenerator
  21. * @author Fredrik Wallgren <fredrik@wallgren.me>
  22. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  23. */
  24. class wsdl2phpService
  25. {
  26. /**
  27. *
  28. * @var phpSourcePhpClass The class used to create the service.
  29. */
  30. private $class;
  31. /**
  32. *
  33. * @var string The name of the service
  34. */
  35. private $identifier;
  36. /**
  37. *
  38. * @var array An array containing the operations of the service
  39. */
  40. private $operations;
  41. /**
  42. *
  43. * @var string The description of the service used as description in the phpdoc of the class
  44. */
  45. private $description;
  46. /**
  47. *
  48. * @var array An array of wsdl2phpTypes
  49. */
  50. private $types;
  51. /**
  52. *
  53. * @param string $identifier The name of the service
  54. * @param array $types The types the service knows about
  55. * @param string $description The description of the service
  56. */
  57. function __construct($identifier, array $types, $description)
  58. {
  59. $this->identifier = $identifier;
  60. $this->types = $types;
  61. $this->description = $description;
  62. }
  63. /**
  64. *
  65. * @return phpSourcePhpClass Returns the class, generates it if not done
  66. */
  67. public function getClass()
  68. {
  69. if($this->class == null)
  70. {
  71. $this->generateClass();
  72. }
  73. return $this->class;
  74. }
  75. /**
  76. * Generates the class if not already generated
  77. */
  78. public function generateClass()
  79. {
  80. $config = wsdl2phpGenerator::getInstance()->getConfig();
  81. // Add prefix and suffix
  82. $name = $config->getPrefix().$this->identifier.$config->getSuffix();
  83. // Generate a valid classname
  84. try
  85. {
  86. $name = wsdl2phpValidator::validateClass($name);
  87. }
  88. catch (wsdl2phpValidationException $e)
  89. {
  90. $name .= 'Custom';
  91. }
  92. // Create the class object
  93. $comment = new phpSourcePhpDocComment($this->description);
  94. $this->class = new phpSourcePhpClass($name, $config->getClassExists(), 'SoapClient', $comment);
  95. // Create the constructor
  96. $comment = new phpSourcePhpDocComment();
  97. $comment->addParam(phpSourcePhpDocElementFactory::getParam('array', 'config', 'A array of config values'));
  98. $comment->addParam(phpSourcePhpDocElementFactory::getParam('string', 'wsdl', 'The wsdl file to use'));
  99. $comment->setAccess(phpSourcePhpDocElementFactory::getPublicAccess());
  100. $source = ' foreach(self::$classmap as $key => $value)
  101. {
  102. if(!isset($options[\'classmap\'][$key]))
  103. {
  104. $options[\'classmap\'][$key] = $value;
  105. }
  106. }
  107. '.$this->generateServiceOptions($config).'
  108. parent::__construct($wsdl, $options);'.PHP_EOL;
  109. $function = new phpSourcePhpFunction('public', '__construct', 'array $options = array(), $wsdl = \''.$config->getInputFile().'\'', $source, $comment);
  110. // Add the constructor
  111. $this->class->addFunction($function);
  112. // Generate the classmap
  113. $name = 'classmap';
  114. $comment = new phpSourcePhpDocComment();
  115. $comment->setAccess(phpSourcePhpDocElementFactory::getPrivateAccess());
  116. $comment->setVar(phpSourcePhpDocElementFactory::getVar('array', $name, 'The defined classes'));
  117. $init = 'array('.PHP_EOL;
  118. foreach ($this->types as $type)
  119. {
  120. if($type instanceof wsdl2phpComplexType)
  121. {
  122. $init .= " '".$type->getIdentifier()."' => '".$type->getPhpIdentifier()."',".PHP_EOL;
  123. }
  124. }
  125. $init = substr($init, 0, strrpos($init, ','));
  126. $init .= ')';
  127. $var = new phpSourcePhpVariable('private static', $name, $init, $comment);
  128. // Add the classmap variable
  129. $this->class->addVariable($var);
  130. // Add all methods
  131. foreach ($this->operations as $operation)
  132. {
  133. $name = wsdl2phpValidator::validateNamingConvention($operation->getName());
  134. $comment = new phpSourcePhpDocComment($operation->getDescription());
  135. $comment->setAccess(phpSourcePhpDocElementFactory::getPublicAccess());
  136. foreach ($operation->getParams() as $param => $hint)
  137. {
  138. $arr = $operation->getPhpDocParams($param, $this->types);
  139. $comment->addParam(phpSourcePhpDocElementFactory::getParam($arr['type'], $arr['name'], $arr['desc']));
  140. }
  141. $source = ' return $this->__soapCall(\''.$name.'\', array('.$operation->getParamStringNoTypeHints().'));'.PHP_EOL;
  142. $paramStr = $operation->getParamString($this->types);
  143. $function = new phpSourcePhpFunction('public', $name, $paramStr, $source, $comment);
  144. if ($this->class->functionExists($function->getIdentifier()) == false)
  145. {
  146. $this->class->addFunction($function);
  147. }
  148. }
  149. }
  150. /**
  151. * Adds an operation to the service
  152. *
  153. * @param string $name
  154. * @param array $params
  155. * @param string $description
  156. */
  157. public function addOperation($name, $params, $description)
  158. {
  159. $this->operations[] = new wsdl2phpOperation($name, $params, $description);
  160. }
  161. /**
  162. *
  163. * @param wsdl2phpConfig $config The config containing the values to use
  164. *
  165. * @return string Returns the string for the options array
  166. */
  167. private function generateServiceOptions(wsdl2phpConfig $config)
  168. {
  169. $ret = '';
  170. if (count($config->getOptionFeatures()) > 0)
  171. {
  172. $i = 0;
  173. $ret .= "
  174. if (isset(\$options['features']) == false)
  175. {
  176. \$options['features'] = ";
  177. foreach ($config->getOptionFeatures() as $option)
  178. {
  179. if ($i++ > 0)
  180. {
  181. $ret .= ' | ';
  182. }
  183. $ret .= $option;
  184. }
  185. $ret .= ";
  186. }".PHP_EOL;
  187. }
  188. if (strlen($config->getWsdlCache()) > 0)
  189. {
  190. $ret .= "
  191. if (isset(\$options['wsdl_cache']) == false)
  192. {
  193. \$options['wsdl_cache'] = ".$config->getWsdlCache();
  194. $ret .= ";
  195. }".PHP_EOL;
  196. }
  197. if (strlen($config->getCompression()) > 0)
  198. {
  199. $ret .= "
  200. if (isset(\$options['compression']) == false)
  201. {
  202. \$options['compression'] = ".$config->getCompression();
  203. $ret .= ";
  204. }".PHP_EOL;
  205. }
  206. return $ret;
  207. }
  208. }