Skip to content

[VarDumper] Add caster for AddressInfo objects #58989

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions src/Symfony/Component/VarDumper/Caster/AddressInfoCaster.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\VarDumper\Caster;

use Symfony\Component\VarDumper\Cloner\Stub;

/**
* @author Nicolas Grekas <p@tchwork.com>
*/
final class AddressInfoCaster
{
private const MAPS = [
'ai_flags' => [
1 => 'AI_PASSIVE',
2 => 'AI_CANONNAME',
4 => 'AI_NUMERICHOST',
8 => 'AI_V4MAPPED',
16 => 'AI_ALL',
32 => 'AI_ADDRCONFIG',
64 => 'AI_IDN',
128 => 'AI_CANONIDN',
1024 => 'AI_NUMERICSERV',
],
'ai_family' => [
1 => 'AF_UNIX',
2 => 'AF_INET',
10 => 'AF_INET6',
44 => 'AF_DIVERT',
],
'ai_socktype' => [
1 => 'SOCK_STREAM',
2 => 'SOCK_DGRAM',
3 => 'SOCK_RAW',
4 => 'SOCK_RDM',
5 => 'SOCK_SEQPACKET',
],
'ai_protocol' => [
1 => 'SOL_SOCKET',
6 => 'SOL_TCP',
17 => 'SOL_UDP',
136 => 'SOL_UDPLITE',
],
];

public static function castAddressInfo(\AddressInfo $h, array $a, Stub $stub, bool $isNested): array
{
static $resolvedMaps;

if (!$resolvedMaps) {
foreach (self::MAPS as $k => $map) {
foreach ($map as $v => $name) {
if (\defined($name)) {
$resolvedMaps[$k][\constant($name)] = $name;
} elseif (!isset($resolvedMaps[$k][$v])) {
$resolvedMaps[$k][$v] = $name;
}
}
}
}

foreach (socket_addrinfo_explain($h) as $k => $v) {
$a[Caster::PREFIX_VIRTUAL.$k] = match (true) {
'ai_flags' === $k => ConstStub::fromBitfield($v, $resolvedMaps[$k]),
isset($resolvedMaps[$k][$v]) => new ConstStub($resolvedMaps[$k][$v], $v),
default => $v,
};
}

return $a;
}
}
19 changes: 19 additions & 0 deletions src/Symfony/Component/VarDumper/Caster/ConstStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,23 @@ public function __toString(): string
{
return (string) $this->value;
}

/**
* @param array<int, string> $values
*/
public static function fromBitfield(int $value, array $values): self
{
$names = [];
foreach ($values as $v => $name) {
if ($value & $v) {
$names[] = $name;
}
}

if (!$names) {
$names[] = $values[0] ?? 0;
}

return new self(implode(' | ', $names), $value);
}
}
2 changes: 2 additions & 0 deletions src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ abstract class AbstractCloner implements ClonerInterface
public static array $defaultCasters = [
'__PHP_Incomplete_Class' => ['Symfony\Component\VarDumper\Caster\Caster', 'castPhpIncompleteClass'],

'AddressInfo' => ['Symfony\Component\VarDumper\Caster\AddressInfoCaster', 'castAddressInfo'],

'Symfony\Component\VarDumper\Caster\CutStub' => ['Symfony\Component\VarDumper\Caster\StubCaster', 'castStub'],
'Symfony\Component\VarDumper\Caster\CutArrayStub' => ['Symfony\Component\VarDumper\Caster\StubCaster', 'castCutArray'],
'Symfony\Component\VarDumper\Caster\ConstStub' => ['Symfony\Component\VarDumper\Caster\StubCaster', 'castStub'],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\VarDumper\Tests\Caster;

use PHPUnit\Framework\TestCase;
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;

/**
* @requires extension sockets
*/
class AddressInfoCasterTest extends TestCase
{
use VarDumperTestTrait;

public function testCaster()
{
$xDump = <<<EODUMP
AddressInfo {
ai_flags: 0
ai_family: AF_INET%A
}
EODUMP;

$this->assertDumpMatchesFormat($xDump, socket_addrinfo_lookup('localhost')[0]);
}
}