[ Index ]

MailPress 7.2

[ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]     [ Statistics ]    

title

Body

[close]

/mp-includes/composer/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Events/ -> SimpleEventDispatcher.php (source)

   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   * @author Chris Corbyn
  15   */
  16  class Swift_Events_SimpleEventDispatcher implements Swift_Events_EventDispatcher
  17  {
  18      /** A map of event types to their associated listener types */
  19      private $eventMap = [];
  20  
  21      /** Event listeners bound to this dispatcher */
  22      private $listeners = [];
  23  
  24      /** Listeners queued to have an Event bubbled up the stack to them */
  25      private $bubbleQueue = [];
  26  
  27      /**
  28       * Create a new EventDispatcher.
  29       */
  30      public function __construct()
  31      {
  32          $this->eventMap = [
  33              'Swift_Events_CommandEvent' => 'Swift_Events_CommandListener',
  34              'Swift_Events_ResponseEvent' => 'Swift_Events_ResponseListener',
  35              'Swift_Events_SendEvent' => 'Swift_Events_SendListener',
  36              'Swift_Events_TransportChangeEvent' => 'Swift_Events_TransportChangeListener',
  37              'Swift_Events_TransportExceptionEvent' => 'Swift_Events_TransportExceptionListener',
  38              ];
  39      }
  40  
  41      /**
  42       * Create a new SendEvent for $source and $message.
  43       *
  44       * @return Swift_Events_SendEvent
  45       */
  46      public function createSendEvent(Swift_Transport $source, Swift_Mime_SimpleMessage $message)
  47      {
  48          return new Swift_Events_SendEvent($source, $message);
  49      }
  50  
  51      /**
  52       * Create a new CommandEvent for $source and $command.
  53       *
  54       * @param string $command      That will be executed
  55       * @param array  $successCodes That are needed
  56       *
  57       * @return Swift_Events_CommandEvent
  58       */
  59      public function createCommandEvent(Swift_Transport $source, $command, $successCodes = [])
  60      {
  61          return new Swift_Events_CommandEvent($source, $command, $successCodes);
  62      }
  63  
  64      /**
  65       * Create a new ResponseEvent for $source and $response.
  66       *
  67       * @param string $response
  68       * @param bool   $valid    If the response is valid
  69       *
  70       * @return Swift_Events_ResponseEvent
  71       */
  72      public function createResponseEvent(Swift_Transport $source, $response, $valid)
  73      {
  74          return new Swift_Events_ResponseEvent($source, $response, $valid);
  75      }
  76  
  77      /**
  78       * Create a new TransportChangeEvent for $source.
  79       *
  80       * @return Swift_Events_TransportChangeEvent
  81       */
  82      public function createTransportChangeEvent(Swift_Transport $source)
  83      {
  84          return new Swift_Events_TransportChangeEvent($source);
  85      }
  86  
  87      /**
  88       * Create a new TransportExceptionEvent for $source.
  89       *
  90       * @return Swift_Events_TransportExceptionEvent
  91       */
  92      public function createTransportExceptionEvent(Swift_Transport $source, Swift_TransportException $ex)
  93      {
  94          return new Swift_Events_TransportExceptionEvent($source, $ex);
  95      }
  96  
  97      /**
  98       * Bind an event listener to this dispatcher.
  99       */
 100      public function bindEventListener(Swift_Events_EventListener $listener)
 101      {
 102          foreach ($this->listeners as $l) {
 103              // Already loaded
 104              if ($l === $listener) {
 105                  return;
 106              }
 107          }
 108          $this->listeners[] = $listener;
 109      }
 110  
 111      /**
 112       * Dispatch the given Event to all suitable listeners.
 113       *
 114       * @param string $target method
 115       */
 116      public function dispatchEvent(Swift_Events_EventObject $evt, $target)
 117      {
 118          $this->prepareBubbleQueue($evt);
 119          $this->bubble($evt, $target);
 120      }
 121  
 122      /** Queue listeners on a stack ready for $evt to be bubbled up it */
 123      private function prepareBubbleQueue(Swift_Events_EventObject $evt)
 124      {
 125          $this->bubbleQueue = [];
 126          $evtClass = get_class($evt);
 127          foreach ($this->listeners as $listener) {
 128              if (array_key_exists($evtClass, $this->eventMap)
 129                  && ($listener instanceof $this->eventMap[$evtClass])) {
 130                  $this->bubbleQueue[] = $listener;
 131              }
 132          }
 133      }
 134  
 135      /** Bubble $evt up the stack calling $target() on each listener */
 136      private function bubble(Swift_Events_EventObject $evt, $target)
 137      {
 138          if (!$evt->bubbleCancelled() && $listener = array_shift($this->bubbleQueue)) {
 139              $listener->$target($evt);
 140              $this->bubble($evt, $target);
 141          }
 142      }
 143  }


Generated: Tue May 19 15:55:14 2020 Cross-referenced by PHPXref 0.7.1