Subversion Repositories php-qbpwcf

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 liveuser 1
<?php
2
 
3
namespace Guzzle\Http;
4
 
5
use Guzzle\Common\Event;
6
use Guzzle\Common\HasDispatcherInterface;
7
use Symfony\Component\EventDispatcher\EventDispatcher;
8
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
9
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
10
 
11
/**
12
 * EntityBody decorator that emits events for read and write methods
13
 */
14
class IoEmittingEntityBody extends AbstractEntityBodyDecorator implements HasDispatcherInterface
15
{
16
    /** @var EventDispatcherInterface */
17
    protected $eventDispatcher;
18
 
19
    public static function getAllEvents()
20
    {
21
        return array('body.read', 'body.write');
22
    }
23
 
24
    /**
25
     * {@inheritdoc}
26
     * @codeCoverageIgnore
27
     */
28
    public function setEventDispatcher(EventDispatcherInterface $eventDispatcher)
29
    {
30
        $this->eventDispatcher = $eventDispatcher;
31
 
32
        return $this;
33
    }
34
 
35
    public function getEventDispatcher()
36
    {
37
        if (!$this->eventDispatcher) {
38
            $this->eventDispatcher = new EventDispatcher();
39
        }
40
 
41
        return $this->eventDispatcher;
42
    }
43
 
44
    public function dispatch($eventName, array $context = array())
45
    {
46
        return $this->getEventDispatcher()->dispatch($eventName, new Event($context));
47
    }
48
 
49
    /**
50
     * {@inheritdoc}
51
     * @codeCoverageIgnore
52
     */
53
    public function addSubscriber(EventSubscriberInterface $subscriber)
54
    {
55
        $this->getEventDispatcher()->addSubscriber($subscriber);
56
 
57
        return $this;
58
    }
59
 
60
    public function read($length)
61
    {
62
        $event = array(
63
            'body'   => $this,
64
            'length' => $length,
65
            'read'   => $this->body->read($length)
66
        );
67
        $this->dispatch('body.read', $event);
68
 
69
        return $event['read'];
70
    }
71
 
72
    public function write($string)
73
    {
74
        $event = array(
75
            'body'   => $this,
76
            'write'  => $string,
77
            'result' => $this->body->write($string)
78
        );
79
        $this->dispatch('body.write', $event);
80
 
81
        return $event['result'];
82
    }
83
}