PageRenderTime 22ms CodeModel.GetById 13ms app.highlight 5ms RepoModel.GetById 2ms app.codeStats 0ms

/Gdata/App/LoggingHttpClientAdapterSocket.php

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