ExceptionNormalizer.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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\Serializer;
  12. use ArrayObject;
  13. use Doctrine\Instantiator\Instantiator;
  14. use Hyperf\Di\ReflectionManager;
  15. use Hyperf\Serializer\Contract\CacheableSupportsMethodInterface;
  16. use ReflectionException;
  17. use RuntimeException;
  18. use Serializable;
  19. use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
  20. use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
  21. use Throwable;
  22. use TypeError;
  23. use function get_class;
  24. class ExceptionNormalizer implements NormalizerInterface, DenormalizerInterface, CacheableSupportsMethodInterface
  25. {
  26. protected ?Instantiator $instantiator = null;
  27. public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): mixed
  28. {
  29. if (is_string($data)) {
  30. $ex = unserialize($data);
  31. if ($ex instanceof Throwable) {
  32. return $ex;
  33. }
  34. // Retry handle it if the exception not instanceof \Throwable.
  35. $data = $ex;
  36. }
  37. if (is_array($data) && isset($data['message'], $data['code'])) {
  38. try {
  39. $exception = $this->getInstantiator()->instantiate($type);
  40. foreach (['code', 'message', 'file', 'line'] as $attribute) {
  41. if (isset($data[$attribute])) {
  42. $property = ReflectionManager::reflectProperty($type, $attribute);
  43. $property->setValue($exception, $data[$attribute]);
  44. }
  45. }
  46. return $exception;
  47. } catch (ReflectionException) {
  48. return new RuntimeException(sprintf(
  49. 'Bad data %s: %s',
  50. $data['class'],
  51. $data['message']
  52. ), $data['code']);
  53. } catch (TypeError) {
  54. return new RuntimeException(sprintf(
  55. 'Uncaught data %s: %s',
  56. $data['class'],
  57. $data['message']
  58. ), $data['code']);
  59. }
  60. }
  61. return new RuntimeException('Bad data data: ' . json_encode($data));
  62. }
  63. public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool
  64. {
  65. return class_exists($type) && is_a($type, Throwable::class, true);
  66. }
  67. public function normalize(mixed $object, ?string $format = null, array $context = []): null|array|ArrayObject|bool|float|int|string
  68. {
  69. if ($object instanceof Serializable) {
  70. return serialize($object);
  71. }
  72. /* @var \Throwable $object */
  73. return [
  74. 'message' => $object->getMessage(),
  75. 'code' => $object->getCode(),
  76. 'file' => $object->getFile(),
  77. 'line' => $object->getLine(),
  78. ];
  79. }
  80. public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
  81. {
  82. return $data instanceof Throwable;
  83. }
  84. public function hasCacheableSupportsMethod(): bool
  85. {
  86. return get_class($this) === __CLASS__;
  87. }
  88. public function getSupportedTypes(?string $format): array
  89. {
  90. return ['object' => static::class === __CLASS__];
  91. }
  92. protected function getInstantiator(): Instantiator
  93. {
  94. if ($this->instantiator instanceof Instantiator) {
  95. return $this->instantiator;
  96. }
  97. return $this->instantiator = new Instantiator();
  98. }
  99. }