8000 [VarDumper] Add caster for AddressInfo objects · symfony/symfony@2bb72bf · GitHub
[go: up one dir, main page]

Skip to content

Commit 2bb72bf

Browse files
[VarDumper] Add caster for AddressInfo objects
1 parent e5fe662 commit 2bb72bf

File tree

4 files changed

+119
-0
lines changed

4 files changed

+119
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\VarDumper\Caster;
13+
14+
use Symfony\Component\VarDumper\Cloner\Stub;
15+
16+
/**
17+
* @author Nicolas Grekas <p@tchwork.com>
18+
*
19+
* @final
20+
*/
21+
class AddressInfoCaster
22+
{
23+
private const MAPS = [
24+
'ai_flags' => [
25+
1 => 'AI_PASSIVE',
26+
2 => 'AI_CANONNAME',
27+
4 => 'AI_NUMERICHOST',
28+
8 => 'AI_V4MAPPED',
29+
16 => 'AI_ALL',
30+
32 => 'AI_ADDRCONFIG',
31+
64 => 'AI_IDN',
32+
128 => 'AI_CANONIDN',
33+
1024 => 'AI_NUMERICSERV',
34+
],
35+
'ai_family' => [
36+
1 => 'AF_UNIX',
37+
2 => 'AF_INET',
38+
10 => 'AF_INET6',
39+
],
40+
'ai_socktype' => [
41+
1 => 'SOCK_STREAM',
42+
2 => 'SOCK_DGRAM',
43+
3 => 'SOCK_RAW',
44+
4 => 'SOCK_RDM',
45+
5 => 'SOCK_SEQPACKET',
46+
],
47+
];
48+
49+
public static function castAddressInfo(\AddressInfo $h, array $a, Stub $stub, bool $isNested)
50+
{
51+
foreach (socket_addrinfo_explain($h) as $k => $v) {
52+
$a[Caster::PREFIX_VIRTUAL.$k] = match ($k) {
53+
'ai_flags' => ConstStub::fromBitfield($v, self::MAPS[$k]),
54+
'ai_family',
55+
'ai_socktype' => new ConstStub(self::MAPS[$k][$v], $v),
56+
default => $v,
57+
};
58+
}
59+
60+
return $a;
61+
}
62+
}

src/Symfony/Component/VarDumper/Caster/ConstStub.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,20 @@ public function __toString(): string
3030
{
3131
return (string) $this->value;
3232
}
33+
34+
public static function fromBitfield(int $value, array $values): self
35+
{
36+
$names = [];
37+
foreach ($values as $v => $name) {
38+
if ($value & $v) {
39+
$names[] = $name;
40+
}
41+
}
42+
43+
if (!$names) {
44+
$names[] = $values[0] ?? 0;
45+
}
46+
47+
return new self(implode(' | ', $names), $value);
48+
}
3349
}

src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ abstract class AbstractCloner implements ClonerInterface
2424
public static array $defaultCasters = [
2525
'__PHP_Incomplete_Class' => ['Symfony\Component\VarDumper\Caster\Caster', 'castPhpIncompleteClass'],
2626

27+
'AddressInfo' => ['Symfony\Component\VarDumper\Caster\AddressInfoCaster', 'castAddressInfo'],
28+
2729
'Symfony\Component\VarDumper\Caster\CutStub' => ['Symfony\Component\VarDumper\Caster\StubCaster', 'castStub'],
2830
'Symfony\Component\VarDumper\Caster\CutArrayStub' => ['Symfony\Component\VarDumper\Caster\StubCaster', 'castCutArray'],
2931
'Symfony\Component\VarDumper\Caster\ConstStub' => ['Symfony\Component\VarDumper\Caster\StubCaster', 'castStub'],
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\VarDumper\Tests\Caster;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\VarDumper\Caster\Caster;
16+
use Symfony\Component\VarDumper\Caster\DateCaster;
17+
use Symfony\Component\VarDumper\Cloner\Stub;
18+
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;
19+
use Symfony\Component\VarDumper\Tests\Fixtures\DateTimeChild;
20+
21+
/**
22+
* @requires extension sockets
23+
*/
24+
class AddressInfoCasterTest extends TestCase
25+
{
26+
use VarDumperTestTrait;
27+
28+
public function testCaster()
29+
{
30+
$xDump = <<<EODUMP
31+
AddressInfo {
32+
ai_flags: 0
33+
ai_family: AF_INET%A
34+
}
35+
EODUMP;
36+
37+
$this->assertDumpMatchesFormat($xDump, socket_addrinfo_lookup('localhost')[0]);
38+
}
39+
}

0 commit comments

Comments
 (0)
0