LoggingHttpClientAdapterSocket.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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: Socket.php 8064 2008-02-16 10:58:39Z thomas $
  19. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  20. * @license http://framework.zend.com/license/new-bsd New BSD License
  21. */
  22. require_once 'Zend/Http/Client/Adapter/Socket.php';
  23. /**
  24. * Overrides the traditional socket-based adapter class for Zend_Http_Client to
  25. * enable logging of requests. All requests are logged to a location specified
  26. * in the config as $config['logfile']. Requests and responses are logged after
  27. * they are sent and received/processed, thus an error could prevent logging.
  28. *
  29. * @category Zend
  30. * @package Zend_Gdata
  31. * @subpackage App
  32. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Gdata_App_LoggingHttpClientAdapterSocket extends Zend_Http_Client_Adapter_Socket
  36. {
  37. /**
  38. * The file handle for writing logs
  39. *
  40. * @var resource|null
  41. */
  42. protected $log_handle = null;
  43. /**
  44. * Log the given message to the log file. The log file is configured
  45. * as the config param 'logfile'. This method opens the file for
  46. * writing if necessary.
  47. *
  48. * @param string $message The message to log
  49. */
  50. protected function log($message)
  51. {
  52. if ($this->log_handle == null) {
  53. $this->log_handle = fopen($this->config['logfile'], 'a');
  54. }
  55. fwrite($this->log_handle, $message);
  56. }
  57. /**
  58. * Connect to the remote server
  59. *
  60. * @param string $host
  61. * @param int $port
  62. * @param boolean $secure
  63. * @param int $timeout
  64. */
  65. public function connect($host, $port = 80, $secure = false)
  66. {
  67. $this->log("Connecting to: ${host}:${port}");
  68. return parent::connect($host, $port, $secure);
  69. }
  70. /**
  71. * Send request to the remote server
  72. *
  73. * @param string $method
  74. * @param Zend_Uri_Http $uri
  75. * @param string $http_ver
  76. * @param array $headers
  77. * @param string $body
  78. * @return string Request as string
  79. */
  80. public function write($method, $uri, $http_ver = '1.1', $headers = array(), $body = '')
  81. {
  82. $request = parent::write($method, $uri, $http_ver, $headers, $body);
  83. $this->log("\n\n" . $request);
  84. return $request;
  85. }
  86. /**
  87. * Read response from server
  88. *
  89. * @return string
  90. */
  91. public function read()
  92. {
  93. $response = parent::read();
  94. $this->log("${response}\n\n");
  95. return $response;
  96. }
  97. /**
  98. * Close the connection to the server
  99. *
  100. */
  101. public function close()
  102. {
  103. $this->log("Closing socket\n\n");
  104. parent::close();
  105. }
  106. }