SafeCaller.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Support;
  12. use Closure;
  13. use Hyperf\Contract\StdoutLoggerInterface;
  14. use Psr\Container\ContainerInterface;
  15. use Psr\Log\LogLevel;
  16. use Throwable;
  17. class SafeCaller
  18. {
  19. public function __construct(private ContainerInterface $container)
  20. {
  21. }
  22. /**
  23. * @template TReturn
  24. * @template TDefault
  25. *
  26. * @param Closure(): TReturn $closure
  27. * @param null|(Closure(): TDefault) $default
  28. * @return ($default is Closure? TDefault : null)|TReturn
  29. */
  30. public function call(Closure $closure, ?Closure $default = null, string $level = LogLevel::CRITICAL): mixed
  31. {
  32. try {
  33. return $closure();
  34. } catch (Throwable $exception) {
  35. if ($this->container->has(StdoutLoggerInterface::class) && $logger = $this->container->get(StdoutLoggerInterface::class)) {
  36. $logger->log($level, (string) $exception);
  37. }
  38. }
  39. return value($default);
  40. }
  41. }