AddressInfoCaster.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\VarDumper\Caster;
  11. use Symfony\Component\VarDumper\Cloner\Stub;
  12. /**
  13. * @author Nicolas Grekas <p@tchwork.com>
  14. *
  15. * @internal since Symfony 7.3
  16. */
  17. final class AddressInfoCaster
  18. {
  19. private const MAPS = [
  20. 'ai_flags' => [
  21. 1 => 'AI_PASSIVE',
  22. 2 => 'AI_CANONNAME',
  23. 4 => 'AI_NUMERICHOST',
  24. 8 => 'AI_V4MAPPED',
  25. 16 => 'AI_ALL',
  26. 32 => 'AI_ADDRCONFIG',
  27. 64 => 'AI_IDN',
  28. 128 => 'AI_CANONIDN',
  29. 1024 => 'AI_NUMERICSERV',
  30. ],
  31. 'ai_family' => [
  32. 1 => 'AF_UNIX',
  33. 2 => 'AF_INET',
  34. 10 => 'AF_INET6',
  35. 44 => 'AF_DIVERT',
  36. ],
  37. 'ai_socktype' => [
  38. 1 => 'SOCK_STREAM',
  39. 2 => 'SOCK_DGRAM',
  40. 3 => 'SOCK_RAW',
  41. 4 => 'SOCK_RDM',
  42. 5 => 'SOCK_SEQPACKET',
  43. ],
  44. 'ai_protocol' => [
  45. 1 => 'SOL_SOCKET',
  46. 6 => 'SOL_TCP',
  47. 17 => 'SOL_UDP',
  48. 136 => 'SOL_UDPLITE',
  49. ],
  50. ];
  51. public static function castAddressInfo(\AddressInfo $h, array $a, Stub $stub, bool $isNested): array
  52. {
  53. static $resolvedMaps;
  54. if (!$resolvedMaps) {
  55. foreach (self::MAPS as $k => $map) {
  56. foreach ($map as $v => $name) {
  57. if (\defined($name)) {
  58. $resolvedMaps[$k][\constant($name)] = $name;
  59. } elseif (!isset($resolvedMaps[$k][$v])) {
  60. $resolvedMaps[$k][$v] = $name;
  61. }
  62. }
  63. }
  64. }
  65. foreach (socket_addrinfo_explain($h) as $k => $v) {
  66. $a[Caster::PREFIX_VIRTUAL.$k] = match (true) {
  67. 'ai_flags' === $k => ConstStub::fromBitfield($v, $resolvedMaps[$k]),
  68. isset($resolvedMaps[$k][$v]) => new ConstStub($resolvedMaps[$k][$v], $v),
  69. default => $v,
  70. };
  71. }
  72. return $a;
  73. }
  74. }