XOOPS RMCommon Utilities  2.1.8.91RC
 All Classes Namespaces Files Functions Variables
SimpleEventDispatcher.php
Go to the documentation of this file.
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 //@require 'Swift/Events/EventDispatcher.php';
12 //@require 'Swift/Events/EventListener.php';
13 //@require 'Swift/Events/EventObject.php';
14 //@require 'Swift/Events/CommandEvent.php';
15 //@require 'Swift/Events/ResponseEvent.php';
16 //@require 'Swift/Events/SendEvent.php';
17 //@require 'Swift/Events/TransportChangeEvent.php';
18 //@require 'Swift/Events/TransportExceptionEvent.php';
19 
28 {
29 
31  private $_eventMap = array();
32 
34  private $_listeners = array();
35 
37  private $_bubbleQueue = array();
38 
42  public function __construct()
43  {
44  $this->_eventMap = array(
45  'Swift_Events_CommandEvent' => 'Swift_Events_CommandListener',
46  'Swift_Events_ResponseEvent' => 'Swift_Events_ResponseListener',
47  'Swift_Events_SendEvent' => 'Swift_Events_SendListener',
48  'Swift_Events_TransportChangeEvent' => 'Swift_Events_TransportChangeListener',
49  'Swift_Events_TransportExceptionEvent' => 'Swift_Events_TransportExceptionListener'
50  );
51  }
52 
60  public function createSendEvent(Swift_Transport $source,
61  Swift_Mime_Message $message)
62  {
63  return new Swift_Events_SendEvent($source, $message);
64  }
65 
74  public function createCommandEvent(Swift_Transport $source,
75  $command, $successCodes = array())
76  {
77  return new Swift_Events_CommandEvent($source, $command, $successCodes);
78  }
79 
88  public function createResponseEvent(Swift_Transport $source,
89  $response, $valid)
90  {
91  return new Swift_Events_ResponseEvent($source, $response, $valid);
92  }
93 
101  {
102  return new Swift_Events_TransportChangeEvent($source);
103  }
104 
114  {
115  return new Swift_Events_TransportExceptionEvent($source, $ex);
116  }
117 
124  {
125  foreach ($this->_listeners as $l)
126  {
127  //Already loaded
128  if ($l === $listener)
129  {
130  return;
131  }
132  }
133  $this->_listeners[] = $listener;
134  }
135 
143  {
144  $this->_prepareBubbleQueue($evt);
145  $this->_bubble($evt, $target);
146  }
147 
148  // -- Private methods
149 
152  {
153  $this->_bubbleQueue = array();
154  $evtClass = get_class($evt);
155  foreach ($this->_listeners as $listener)
156  {
157  if (array_key_exists($evtClass, $this->_eventMap)
158  && ($listener instanceof $this->_eventMap[$evtClass]))
159  {
160  $this->_bubbleQueue[] = $listener;
161  }
162  }
163  }
164 
166  private function _bubble(Swift_Events_EventObject $evt, $target)
167  {
168  if (!$evt->bubbleCancelled() && $listener = array_shift($this->_bubbleQueue))
169  {
170  $listener->$target($evt);
171  $this->_bubble($evt, $target);
172  }
173  }
174 
175 }