Project

General

Profile

1
<?php
2

    
3
/*
4
 * This file is part of the Predis package.
5
 *
6
 * (c) Daniele Alessandri <suppakilla@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

    
12
require 'SharedConfigurations.php';
13

    
14
use Predis\Command\CommandInterface;
15
use Predis\Connection\StreamConnection;
16

    
17
class SimpleDebuggableConnection extends StreamConnection
18
{
19
    private $tstart = 0;
20
    private $debugBuffer = array();
21

    
22
    public function connect()
23
    {
24
        $this->tstart = microtime(true);
25

    
26
        parent::connect();
27
    }
28

    
29
    private function storeDebug(CommandInterface $command, $direction)
30
    {
31
        $firtsArg  = $command->getArgument(0);
32
        $timestamp = round(microtime(true) - $this->tstart, 4);
33

    
34
        $debug  = $command->getId();
35
        $debug .= isset($firtsArg) ? " $firtsArg " : ' ';
36
        $debug .= "$direction $this";
37
        $debug .= " [{$timestamp}s]";
38

    
39
        $this->debugBuffer[] = $debug;
40
    }
41

    
42
    public function writeCommand(CommandInterface $command)
43
    {
44
        parent::writeCommand($command);
45

    
46
        $this->storeDebug($command, '->');
47
    }
48

    
49
    public function readResponse(CommandInterface $command)
50
    {
51
        $reply = parent::readResponse($command);
52
        $this->storeDebug($command, '<-');
53

    
54
        return $reply;
55
    }
56

    
57
    public function getDebugBuffer()
58
    {
59
        return $this->debugBuffer;
60
    }
61
}
62

    
63
$options = array(
64
    'connections' => array(
65
        'tcp' => 'SimpleDebuggableConnection',
66
    ),
67
);
68

    
69
$client = new Predis\Client($single_server, $options);
70
$client->set('foo', 'bar');
71
$client->get('foo');
72
$client->info();
73

    
74
print_r($client->getConnection()->getDebugBuffer());
75

    
76
/* OUTPUT:
77
Array
78
(
79
    [0] => SELECT 15 -> 127.0.0.1:6379 [0.0008s]
80
    [1] => SELECT 15 <- 127.0.0.1:6379 [0.0012s]
81
    [2] => SET foo -> 127.0.0.1:6379 [0.0014s]
82
    [3] => SET foo <- 127.0.0.1:6379 [0.0014s]
83
    [4] => GET foo -> 127.0.0.1:6379 [0.0016s]
84
    [5] => GET foo <- 127.0.0.1:6379 [0.0018s]
85
    [6] => INFO -> 127.0.0.1:6379 [0.002s]
86
    [7] => INFO <- 127.0.0.1:6379 [0.0025s]
87
)
88
*/
(15-15/17)