[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/joomla/event/src/ -> LazyServiceEventListener.php (source)

   1  <?php
   2  /**
   3   * Part of the Joomla Framework Event Package
   4   *
   5   * @copyright  Copyright (C) 2005 - 2021 Open Source Matters, Inc. All rights reserved.
   6   * @license    GNU General Public License version 2 or later; see LICENSE
   7   */
   8  
   9  namespace Joomla\Event;
  10  
  11  use Psr\Container\ContainerInterface;
  12  
  13  /**
  14   * Decorator for an event listener to be pulled from the service container.
  15   *
  16   * @since  2.0.0
  17   */
  18  final class LazyServiceEventListener
  19  {
  20      /**
  21       * The service container to load the service from
  22       *
  23       * @var    string
  24       * @since  2.0.0
  25       */
  26      private $container;
  27  
  28      /**
  29       * The ID of the service from the container to be used
  30       *
  31       * @var    string
  32       * @since  2.0.0
  33       */
  34      private $serviceId;
  35  
  36      /**
  37       * The method from the service to be called
  38       *
  39       * @var    string
  40       * @since  2.0.0
  41       */
  42      private $method;
  43  
  44      /**
  45       * Constructor.
  46       *
  47       * @param   ContainerInterface  $container  The service container to load the service from when it shall be executed
  48       * @param   string              $serviceId  The ID of the service from the container to be used
  49       * @param   string              $method     The method from the service to be called if necessary. If left empty, the service must be a callable;
  50       *                                          (i.e. have an `__invoke()` method on a class)
  51       *
  52       * @since   2.0.0
  53       * @throws  \InvalidArgumentException if the service ID is empty
  54       */
  55  	public function __construct(ContainerInterface $container, string $serviceId, string $method = '')
  56      {
  57          if (empty($serviceId))
  58          {
  59              throw new \InvalidArgumentException(
  60                  sprintf(
  61                      'The $serviceId parameter cannot be empty in %s',
  62                      self::class
  63                  )
  64              );
  65          }
  66  
  67          $this->container = $container;
  68          $this->serviceId = $serviceId;
  69          $this->method    = $method;
  70      }
  71  
  72      /**
  73       * Load a service from the container to listen to an event.
  74       *
  75       * @param   EventInterface  $event  The event to process
  76       *
  77       * @return  void
  78       *
  79       * @since   2.0.0
  80       * @throws  \InvalidArgumentException if the constructor's $method parameter is empty when not executing a callable service
  81       * @throws  \RuntimeException if the service cannot be executed
  82       */
  83  	public function __invoke(EventInterface $event): void
  84      {
  85          if (!$this->container->has($this->serviceId))
  86          {
  87              throw new \RuntimeException(
  88                  sprintf(
  89                      'The "%s" service has not been registered to the service container',
  90                      $this->serviceId
  91                  )
  92              );
  93          }
  94  
  95          $service = $this->container->get($this->serviceId);
  96  
  97          // If the service is callable on its own, just execute it
  98          if (\is_callable($service))
  99          {
 100              \call_user_func($service, $event);
 101  
 102              return;
 103          }
 104  
 105          if (empty($this->method))
 106          {
 107              throw new \InvalidArgumentException(
 108                  sprintf(
 109                      'The $method argument is required when creating a "%s" to call a method from the "%s" service.',
 110                      self::class,
 111                      $this->serviceId
 112                  )
 113              );
 114          }
 115  
 116          if (!method_exists($service, $this->method))
 117          {
 118              throw new \RuntimeException(
 119                  sprintf(
 120                      'The "%s" method does not exist on "%s" (from service "%s")',
 121                      $this->method,
 122                      \get_class($service),
 123                      $this->serviceId
 124                  )
 125              );
 126          }
 127  
 128          \call_user_func([$service, $this->method], $event);
 129      }
 130  }


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