PageRenderTime 54ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/libs/Zend/Gdata/App/LoggingHttpClientAdapterSocket.php

https://bitbucket.org/herloct/gagapi
PHP | 119 lines | 35 code | 10 blank | 74 comment | 2 complexity | 05260e4a919d6234cd494af1127b1e44 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Gdata
  17. * @subpackage App
  18. * @version $Id: LoggingHttpClientAdapterSocket.php 23775 2011-03-01 17:25:24Z ralph $
  19. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  20. * @license http://framework.zend.com/license/new-bsd New BSD License
  21. */
  22. /**
  23. * @see Zend_Http_Client_Adapter_Socket
  24. */
  25. require_once 'Zend/Http/Client/Adapter/Socket.php';
  26. /**
  27. * Overrides the traditional socket-based adapter class for Zend_Http_Client to
  28. * enable logging of requests. All requests are logged to a location specified
  29. * in the config as $config['logfile']. Requests and responses are logged after
  30. * they are sent and received/processed, thus an error could prevent logging.
  31. *
  32. * @category Zend
  33. * @package Zend_Gdata
  34. * @subpackage App
  35. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. */
  38. class Zend_Gdata_App_LoggingHttpClientAdapterSocket extends Zend_Http_Client_Adapter_Socket
  39. {
  40. /**
  41. * The file handle for writing logs
  42. *
  43. * @var resource|null
  44. */
  45. protected $log_handle = null;
  46. /**
  47. * Log the given message to the log file. The log file is configured
  48. * as the config param 'logfile'. This method opens the file for
  49. * writing if necessary.
  50. *
  51. * @param string $message The message to log
  52. */
  53. protected function log($message)
  54. {
  55. if ($this->log_handle == null) {
  56. $this->log_handle = fopen($this->config['logfile'], 'a');
  57. }
  58. fwrite($this->log_handle, $message);
  59. }
  60. /**
  61. * Connect to the remote server
  62. *
  63. * @param string $host
  64. * @param int $port
  65. * @param boolean $secure
  66. * @param int $timeout
  67. */
  68. public function connect($host, $port = 80, $secure = false)
  69. {
  70. $this->log("Connecting to: ${host}:${port}");
  71. return parent::connect($host, $port, $secure);
  72. }
  73. /**
  74. * Send request to the remote server
  75. *
  76. * @param string $method
  77. * @param Zend_Uri_Http $uri
  78. * @param string $http_ver
  79. * @param array $headers
  80. * @param string $body
  81. * @return string Request as string
  82. */
  83. public function write($method, $uri, $http_ver = '1.1', $headers = array(), $body = '')
  84. {
  85. $request = parent::write($method, $uri, $http_ver, $headers, $body);
  86. $this->log("\n\n" . $request);
  87. return $request;
  88. }
  89. /**
  90. * Read response from server
  91. *
  92. * @return string
  93. */
  94. public function read()
  95. {
  96. $response = parent::read();
  97. $this->log("${response}\n\n");
  98. return $response;
  99. }
  100. /**
  101. * Close the connection to the server
  102. *
  103. */
  104. public function close()
  105. {
  106. $this->log("Closing socket\n\n");
  107. parent::close();
  108. }
  109. }