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: /**
17: * @var EventDispatcherInterface
18: */
19: protected $eventDispatcher;
20:
21: /**
22: * {@inheritdoc}
23: */
24: public static function getAllEvents()
25: {
26: return array('body.read', 'body.write');
27: }
28:
29: /**
30: * {@inheritdoc}
31: * @codeCoverageIgnore
32: */
33: public function setEventDispatcher(EventDispatcherInterface $eventDispatcher)
34: {
35: $this->eventDispatcher = $eventDispatcher;
36:
37: return $this;
38: }
39:
40: /**
41: * {@inheritdoc}
42: */
43: public function getEventDispatcher()
44: {
45: if (!$this->eventDispatcher) {
46: $this->eventDispatcher = new EventDispatcher();
47: }
48:
49: return $this->eventDispatcher;
50: }
51:
52: /**
53: * {@inheritdoc}
54: */
55: public function dispatch($eventName, array $context = array())
56: {
57: $this->getEventDispatcher()->dispatch($eventName, new Event($context));
58: }
59:
60: /**
61: * {@inheritdoc}
62: * @codeCoverageIgnore
63: */
64: public function addSubscriber(EventSubscriberInterface $subscriber)
65: {
66: $this->getEventDispatcher()->addSubscriber($subscriber);
67:
68: return $this;
69: }
70:
71: /**
72: * {@inheritdoc}
73: */
74: public function read($length)
75: {
76: $event = array(
77: 'body' => $this,
78: 'length' => $length,
79: 'read' => $this->body->read($length)
80: );
81: $this->dispatch('body.read', $event);
82:
83: return $event['read'];
84: }
85:
86: /**
87: * {@inheritdoc}
88: */
89: public function write($string)
90: {
91: $event = array(
92: 'body' => $this,
93: 'write' => $string,
94: 'result' => $this->body->write($string)
95: );
96: $this->dispatch('body.write', $event);
97:
98: return $event['result'];
99: }
100: }
101: