PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/jelix-legacy/utils/jSoapClient.class.php

https://github.com/gmarrot/jelix
PHP | 196 lines | 139 code | 22 blank | 35 comment | 23 complexity | eef63dc840140ba054fe1e2d4398e7e4 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /**
  3. * @package jelix
  4. * @subpackage utils
  5. * @author Laurent Jouanneau
  6. * @copyright 2011-2014 Laurent Jouanneau
  7. * @link http://jelix.org
  8. * @licence GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html
  9. */
  10. /**
  11. * class that handles a dump of a php value, for a logger
  12. */
  13. class jLogSoapMessage extends jLogMessage {
  14. /**
  15. * @var string
  16. */
  17. protected $headers;
  18. /**
  19. * @var string
  20. */
  21. protected $request;
  22. /**
  23. * @var string
  24. */
  25. protected $response;
  26. /**
  27. * @var string
  28. */
  29. protected $duration;
  30. public function __construct($function_name, $soapClient, $category='default', $duration = 0) {
  31. $this->category = $category;
  32. $this->headers = $soapClient->__getLastRequestHeaders();
  33. $this->request = $soapClient->__getLastRequest ();
  34. $this->response = $soapClient->__getLastResponse();
  35. $this->functionName = $function_name;
  36. $this->duration = $duration;
  37. $this->message = 'Soap call: '.$function_name.'()';
  38. }
  39. public function getHeaders() {
  40. return $this->headers;
  41. }
  42. public function getResponse() {
  43. return $this->response;
  44. }
  45. public function getRequest() {
  46. return $this->request;
  47. }
  48. public function getDuration() {
  49. return $this->duration;
  50. }
  51. public function getFormatedMessage() {
  52. $message = 'Soap call: '.$this->functionName."()\n";
  53. $message .= "DURATION: ".$this->duration."s\n";
  54. $message .= "HEADERS:\n\t".str_replace("\n","\n\t",$this->headers)."\n";
  55. $message .= "REQUEST:\n\t".str_replace("\n","\n\t",$this->request)."\n";
  56. $message .= "RESPONSE:\n\t".str_replace("\n","\n\t",$this->response)."\n";
  57. return $message;
  58. }
  59. }
  60. class SoapClientDebug extends SoapClient {
  61. public function __call ( $function_name , $arguments) {
  62. $timeExecutionBegin = $this->_microtimeFloat();
  63. $ex = false;
  64. try {
  65. $result = parent::__call($function_name , $arguments);
  66. }
  67. catch(Exception $e) {
  68. $ex = $e;
  69. }
  70. $timeExecutionEnd = $this->_microtimeFloat();
  71. $log = new jLogSoapMessage($function_name, $this, 'soap', $timeExecutionEnd - $timeExecutionBegin);
  72. jLog::log($log,'soap');
  73. if ($ex)
  74. throw $ex;
  75. return $result;
  76. }
  77. public function __soapCall ( $function_name , $arguments, $options=array(), $input_headers=null, &$output_headers=null) {
  78. $timeExecutionBegin = $this->_microtimeFloat();
  79. $ex = false;
  80. try {
  81. $result = parent::__soapCall($function_name , $arguments, $options, $input_headers, $output_headers);
  82. }
  83. catch(Exception $e) {
  84. $ex = $e;
  85. }
  86. $timeExecutionEnd = $this->_microtimeFloat();
  87. $log = new jLogSoapMessage($function_name, $this, 'soap', $timeExecutionEnd - $timeExecutionBegin);
  88. jLog::log($log,'soap');
  89. if ($ex)
  90. throw $ex;
  91. return $result;
  92. }
  93. protected function _microtimeFloat() {
  94. list($usec, $sec) = explode(" ", microtime());
  95. return ((float)$usec + (float)$sec);
  96. }
  97. }
  98. /**
  99. * provide a soap client where configuration information are stored in the profile file
  100. * @package jelix
  101. * @subpackage utils
  102. */
  103. class jSoapClient {
  104. protected static $classmap = array();
  105. /**
  106. * @param string $profile the profile name
  107. */
  108. public static function get($profile = '') {
  109. return jProfiles::getOrStoreInPool('jsoapclient', $profile, array('jSoapClient', '_getClient'));
  110. }
  111. /**
  112. * callback method for jprofiles. Internal use.
  113. */
  114. public static function _getClient($profile) {
  115. $wsdl = null;
  116. $client = 'SoapClient';
  117. if (isset($profile['wsdl'])) {
  118. $wsdl = $profile['wsdl'];
  119. if ($wsdl == '') {
  120. $wsdl = null;
  121. }
  122. else if (!preg_match("!^https?\\://!", $wsdl)){
  123. $wsdl = jFile::parseJelixPath($wsdl);
  124. }
  125. unset ($profile['wsdl']);
  126. }
  127. if (isset($profile['trace'])) {
  128. $profile['trace'] = intval($profile['trace']); // SoapClient recognize only true integer
  129. if ($profile['trace'])
  130. $client = 'SoapClientDebug';
  131. }
  132. if (isset($profile['exceptions'])) {
  133. $profile['exceptions'] = intval($profile['exceptions']); // SoapClient recognize only true integer
  134. }
  135. if (isset($profile['connection_timeout'])) {
  136. $profile['connection_timeout'] = intval($profile['connection_timeout']); // SoapClient recognize only true integer
  137. }
  138. // deal with classmap
  139. $classMap = array();
  140. if (isset($profile['classmap_file']) && ($f = trim($profile['classmap_file'])) != '') {
  141. if (!isset(self::$classmap[$f])) {
  142. if (!file_exists(jApp::configPath($f))) {
  143. trigger_error("jSoapClient: classmap file ".$f." does not exists.", E_USER_WARNING);
  144. self::$classmap[$f] = array();
  145. }
  146. else {
  147. self::$classmap[$f] = parse_ini_file(jApp::configPath($f), true);
  148. }
  149. }
  150. if (isset(self::$classmap[$f]['__common__'])) {
  151. $classMap = array_merge($classMap, self::$classmap[$f]['__common__']);
  152. }
  153. if (isset(self::$classmap[$f][$profile['_name']])) {
  154. $classMap = array_merge($classMap, self::$classmap[$f][$profile['_name']]);
  155. }
  156. unset($profile['classmap_file']);
  157. }
  158. if (isset($profile['classmap']) && is_string ($profile['classmap']) && $profile['classmap'] != '') {
  159. $map = (array)json_decode(str_replace("'", '"',$profile['classmap']));
  160. $classMap = array_merge($classMap, $map);
  161. unset($profile['classmap']);
  162. }
  163. if (count($classMap)) {
  164. $profile['classmap'] = $classMap;
  165. }
  166. //$context = stream_context_create( array('http' => array('max_redirects' => 3)));
  167. //$profile['stream_context'] = $context;
  168. unset ($profile['_name']);
  169. return new $client($wsdl, $profile);
  170. }
  171. }