[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Application/ -> EventAware.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2005 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE
   8   */
   9  
  10  namespace Joomla\CMS\Application;
  11  
  12  use Joomla\CMS\Event\CoreEventAware;
  13  use Joomla\Event\DispatcherInterface;
  14  use Joomla\Event\Event;
  15  use Psr\Log\LoggerInterface;
  16  
  17  // phpcs:disable PSR1.Files.SideEffects
  18  \defined('JPATH_PLATFORM') or die;
  19  // phpcs:enable PSR1.Files.SideEffects
  20  
  21  /**
  22   * Trait for application classes which dispatch events
  23   *
  24   * @since  4.0.0
  25   */
  26  trait EventAware
  27  {
  28      use CoreEventAware;
  29  
  30      /**
  31       * Get the event dispatcher.
  32       *
  33       * @return  DispatcherInterface
  34       *
  35       * @since   4.0.0
  36       * @throws  \UnexpectedValueException May be thrown if the dispatcher has not been set.
  37       */
  38      abstract public function getDispatcher();
  39  
  40      /**
  41       * Get the logger.
  42       *
  43       * @return  LoggerInterface
  44       *
  45       * @since   4.0.0
  46       */
  47      abstract public function getLogger();
  48  
  49      /**
  50       * Registers a handler to a particular event group.
  51       *
  52       * @param   string    $event    The event name.
  53       * @param   callable  $handler  The handler, a function or an instance of an event object.
  54       *
  55       * @return  $this
  56       *
  57       * @since   4.0.0
  58       */
  59      public function registerEvent($event, callable $handler)
  60      {
  61          try {
  62              $this->getDispatcher()->addListener($event, $handler);
  63          } catch (\UnexpectedValueException $e) {
  64              // No dispatcher is registered, don't throw an error (mimics old behavior)
  65          }
  66  
  67          return $this;
  68      }
  69  
  70      /**
  71       * Calls all handlers associated with an event group.
  72       *
  73       * This is a legacy method, implementing old-style (Joomla! 3.x) plugin calls. It's best to go directly through the
  74       * Dispatcher and handle the returned EventInterface object instead of going through this method. This method is
  75       * deprecated and will be removed in Joomla! 5.x.
  76       *
  77       * This method will only return the 'result' argument of the event
  78       *
  79       * @param   string       $eventName  The event name.
  80       * @param   array|Event  $args       An array of arguments or an Event object (optional).
  81       *
  82       * @return  array  An array of results from each function call. Note this will be an empty array if no dispatcher is set.
  83       *
  84       * @since       4.0.0
  85       * @throws      \InvalidArgumentException
  86       * @deprecated  5.0
  87       */
  88      public function triggerEvent($eventName, $args = [])
  89      {
  90          try {
  91              $dispatcher = $this->getDispatcher();
  92          } catch (\UnexpectedValueException $exception) {
  93              $this->getLogger()->error(sprintf('Dispatcher not set in %s, cannot trigger events.', \get_class($this)));
  94  
  95              return [];
  96          }
  97  
  98          if ($args instanceof Event) {
  99              $event = $args;
 100          } elseif (\is_array($args)) {
 101              $className = self::getEventClassByEventName($eventName);
 102              $event     = new $className($eventName, $args);
 103          } else {
 104              throw new \InvalidArgumentException('The arguments must either be an event or an array');
 105          }
 106  
 107          $result = $dispatcher->dispatch($eventName, $event);
 108  
 109          // @todo - There are still test cases where the result isn't defined, temporarily leave the isset check in place
 110          return !isset($result['result']) || \is_null($result['result']) ? [] : $result['result'];
 111      }
 112  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer