1: <?php
2:
3: /*
4: * This file is part of SwiftMailer.
5: * (c) 2004-2009 Chris Corbyn
6: *
7: * For the full copyright and license information, please view the LICENSE
8: * file that was distributed with this source code.
9: */
10:
11: /**
12: * The EventDispatcher which handles the event dispatching layer.
13: *
14: * @package Swift
15: * @subpackage Events
16: * @author Chris Corbyn
17: */
18: class Swift_Events_SimpleEventDispatcher implements Swift_Events_EventDispatcher
19: {
20: /** A map of event types to their associated listener types */
21: private $_eventMap = array();
22:
23: /** Event listeners bound to this dispatcher */
24: private $_listeners = array();
25:
26: /** Listeners queued to have an Event bubbled up the stack to them */
27: private $_bubbleQueue = array();
28:
29: /**
30: * Create a new EventDispatcher.
31: */
32: public function __construct()
33: {
34: $this->_eventMap = array(
35: 'Swift_Events_CommandEvent' => 'Swift_Events_CommandListener',
36: 'Swift_Events_ResponseEvent' => 'Swift_Events_ResponseListener',
37: 'Swift_Events_SendEvent' => 'Swift_Events_SendListener',
38: 'Swift_Events_TransportChangeEvent' => 'Swift_Events_TransportChangeListener',
39: 'Swift_Events_TransportExceptionEvent' => 'Swift_Events_TransportExceptionListener'
40: );
41: }
42:
43: /**
44: * Create a new SendEvent for $source and $message.
45: *
46: * @param Swift_Transport $source
47: * @param Swift_Mime_Message
48: * @return Swift_Events_SendEvent
49: */
50: public function createSendEvent(Swift_Transport $source, Swift_Mime_Message $message)
51: {
52: return new Swift_Events_SendEvent($source, $message);
53: }
54:
55: /**
56: * Create a new CommandEvent for $source and $command.
57: *
58: * @param Swift_Transport $source
59: * @param string $command That will be executed
60: * @param array $successCodes That are needed
61: * @return Swift_Events_CommandEvent
62: */
63: public function createCommandEvent(Swift_Transport $source, $command, $successCodes = array())
64: {
65: return new Swift_Events_CommandEvent($source, $command, $successCodes);
66: }
67:
68: /**
69: * Create a new ResponseEvent for $source and $response.
70: *
71: * @param Swift_Transport $source
72: * @param string $response
73: * @param boolean $valid If the response is valid
74: * @return Swift_Events_ResponseEvent
75: */
76: public function createResponseEvent(Swift_Transport $source, $response, $valid)
77: {
78: return new Swift_Events_ResponseEvent($source, $response, $valid);
79: }
80:
81: /**
82: * Create a new TransportChangeEvent for $source.
83: *
84: * @param Swift_Transport $source
85: * @return Swift_Events_TransportChangeEvent
86: */
87: public function createTransportChangeEvent(Swift_Transport $source)
88: {
89: return new Swift_Events_TransportChangeEvent($source);
90: }
91:
92: /**
93: * Create a new TransportExceptionEvent for $source.
94: *
95: * @param Swift_Transport $source
96: * @param Swift_TransportException $ex
97: * @return Swift_Events_TransportExceptionEvent
98: */
99: public function createTransportExceptionEvent(Swift_Transport $source, Swift_TransportException $ex)
100: {
101: return new Swift_Events_TransportExceptionEvent($source, $ex);
102: }
103:
104: /**
105: * Bind an event listener to this dispatcher.
106: *
107: * @param Swift_Events_EventListener $listener
108: */
109: public function bindEventListener(Swift_Events_EventListener $listener)
110: {
111: foreach ($this->_listeners as $l) {
112: //Already loaded
113: if ($l === $listener) {
114: return;
115: }
116: }
117: $this->_listeners[] = $listener;
118: }
119:
120: /**
121: * Dispatch the given Event to all suitable listeners.
122: *
123: * @param Swift_Events_EventObject $evt
124: * @param string $target method
125: */
126: public function dispatchEvent(Swift_Events_EventObject $evt, $target)
127: {
128: $this->_prepareBubbleQueue($evt);
129: $this->_bubble($evt, $target);
130: }
131:
132: // -- Private methods
133:
134: /** Queue listeners on a stack ready for $evt to be bubbled up it */
135: private function _prepareBubbleQueue(Swift_Events_EventObject $evt)
136: {
137: $this->_bubbleQueue = array();
138: $evtClass = get_class($evt);
139: foreach ($this->_listeners as $listener) {
140: if (array_key_exists($evtClass, $this->_eventMap)
141: && ($listener instanceof $this->_eventMap[$evtClass]))
142: {
143: $this->_bubbleQueue[] = $listener;
144: }
145: }
146: }
147:
148: /** Bubble $evt up the stack calling $target() on each listener */
149: private function _bubble(Swift_Events_EventObject $evt, $target)
150: {
151: if (!$evt->bubbleCancelled() && $listener = array_shift($this->_bubbleQueue)) {
152: $listener->$target($evt);
153: $this->_bubble($evt, $target);
154: }
155: }
156: }
157: