Event.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. declare(strict_types=1);
  3. namespace Yansongda\Artful;
  4. use Yansongda\Artful\Contract\EventDispatcherInterface;
  5. use Yansongda\Artful\Exception\ContainerException;
  6. use Yansongda\Artful\Exception\Exception;
  7. use Yansongda\Artful\Exception\InvalidParamsException;
  8. use Yansongda\Artful\Exception\ServiceNotFoundException;
  9. /**
  10. * @method static Event\Event dispatch(object $event)
  11. */
  12. class Event
  13. {
  14. /**
  15. * @throws ContainerException
  16. * @throws ServiceNotFoundException
  17. * @throws InvalidParamsException
  18. */
  19. public static function __callStatic(string $method, array $args): void
  20. {
  21. if (!Artful::hasContainer() || !Artful::has(EventDispatcherInterface::class)) {
  22. return;
  23. }
  24. $class = Artful::get(EventDispatcherInterface::class);
  25. if ($class instanceof \Psr\EventDispatcher\EventDispatcherInterface) {
  26. $class->{$method}(...$args);
  27. return;
  28. }
  29. throw new InvalidParamsException(Exception::PARAMS_EVENT_DRIVER_INVALID, '参数异常: 配置的 `EventDispatcherInterface` 不符合 PSR 规范');
  30. }
  31. }