10000 [VarDumper] Simplify things · symfony/symfony@ed8fdf2 · GitHub
[go: up one dir, main page]

Skip to content

Commit ed8fdf2

Browse files
[VarDumper] Simplify things
1 parent 1ff8865 commit ed8fdf2

13 files changed

+152
-347
lines changed

src/Symfony/Component/VarDumper/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ CHANGELOG
44
7.3
55
---
66

7-
* Add `CurlCaster`, `OpenSslCaster`, `SqliteCaster`, `SocketCaster` and `DbaCaster`
7+
* Add casters for `Dba\Connection`, `SQLite3Result`, `OpenSSLAsymmetricKey` and `OpenSSLCertificateSigningRequest`
88

99
7.2
1010
---

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

Lines changed: 0 additions & 30 deletions
This file was deleted.

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

Lines changed: 0 additions & 35 deletions
This file was deleted.

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

Lines changed: 0 additions & 92 deletions
This file was deleted.

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

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,29 @@
2222
*/
2323
class ResourceCaster
2424
{
25+
public static function castCurl(\CurlHandle $h, array $a, Stub $stub, bool $isNested): array
26+
{
27+
$info = curl_getinfo($h);
28+
foreach ($info as $key => $val) {
29+
$a[Caster::PREFIX_VIRTUAL.$key] = $val;
30+
}
31+
32+
return $a;
33+
}
34+
35+
public static function castDba($dba, array $a, Stub $stub, bool $isNested): array
36+
{
37+
if (\PHP_VERSION_ID < 80402) {
57AE
38+
// @see https://github.com/php/php-src/issues/16990
39+
return $a;
40+
}
41+
42+
$list = dba_list();
43+
$a['file'] = $list[(int) $dba];
44+
45+
return $a;
46+
}
47+
2548
public static function castProcess($process, array $a, Stub $stub, bool $isNested): array
2649
{
2750
B41A return proc_get_status($process);
@@ -42,11 +65,63 @@ public static function castStreamContext($stream, array $a, Stub $stub, bool $is
4265
return @stream_context_get_params($stream) ?: $a;
4366
}
4467

68+
/**
69+
* @param \GdImage $gd
70+
*/
4571
public static function castGd($gd, array $a, Stub $stub, bool $isNested): array
4672
{
4773
$a['size'] = imagesx($gd).'x'.imagesy($gd);
4874
$a['trueColor'] = imageistruecolor($gd);
4975

5076
return $a;
5177
}
78+
79+
/**
80+
* @param \OpenSSLCertificate $h
81+
*/
82+
public static function castOpensslX509($h, array $a, Stub $stub, bool $isNested): array
83+
{
84+
$stub->cut = -1;
85+
$info = openssl_x509_parse($h, false);
86+
87+
$pin = openssl_pkey_get_public($h);
88+
$pin = openssl_pkey_get_details($pin)['key'];
89+
$pin = \array_slice(explode("\n", $pin), 1, -2);
90+
$pin = base64_decode(implode('', $pin));
91+
$pin = base64_encode(hash('sha256', $pin, true));
92+
93+
$a += [
94+
'subject' => new EnumStub(array_intersect_key($info['subject'], ['organizationName' => true, 'commonName' => true])),
95+
'issuer' => new EnumStub(array_intersect_key($info['issuer'], ['organizationName' => true, 'commonName' => true])),
96+
'expiry' => new ConstStub(date(\DateTimeInterface::ISO8601, $info['validTo_time_t']), $info['validTo_time_t']),
97+
'fingerprint' => new EnumStub([
98+
'md5' => new ConstStub(wordwrap(strtoupper(openssl_x509_fingerprint($h, 'md5')), 2, ':', true)),
99+
'sha1' => new ConstStub(wordwrap(strtoupper(openssl_x509_fingerprint($h, 'sha1')), 2, ':', true)),
100+
'sha256' => new ConstStub(wordwrap(strtoupper(openssl_x509_fingerprint($h, 'sha256')), 2, ':', true)),
101+
'pin-sha256' => new ConstStub($pin),
102+
]),
103+
];
104+
105+
return $a;
106+
}
107+
108+
public static function castOpensslAsymmetricKey(\OpenSSLAsymmetricKey $h, array $a, Stub $stub, bool $isNested): array
109+
{
110+
foreach (openssl_pkey_get_details($h) as $k => $v) {
111+
$a[Caster::PREFIX_VIRTUAL.$k] = $v;
112+
}
113+
114+
unset($a[Caster::PREFIX_VIRTUAL.'rsa']); // binary data
115+
116+
return $a;
117+
}
118+
119+
public static function castOpensslCsr(\OpenSSLCertificateSigningRequest $h, array $a, Stub $stub, bool $isNested): array
120+
{
121+
foreach (openssl_csr_get_subject($h, false) as $k => $v) {
122+
$a[Caster::PREFIX_VIRTUAL.$k] = $v;
123+
}
124+
125+
return $a;
126+
}
52127
}

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

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@
1313

1414
use Symfony\Component\VarDumper\Cloner\Stub;
1515

16-
/**
17-
* @author Alexandre Daubois <alex.daubois@gmail.com>
18-
*/
19-
class SocketCaster
16+
final class SocketCaster
2017
{
2118
public static function castSocket(\Socket $h, array $a, Stub $stub, bool $isNested): array
2219
{
@@ -26,23 +23,12 @@ public static function castSocket(\Socket $h, array $a, Stub $stub, bool $isNest
2623
$a += [
2724
Caster::PREFIX_VIRTUAL.'address' => $addr,
2825
Caster::PREFIX_VIRTUAL.'port' => $port,
29-
Caster::PREFIX_VIRTUAL.'info' => new EnumStub(array_intersect_key(
30-
$info,
31-
[
32-
'timed_out' => new ConstStub($info['timed_out'] ? 'true' : 'false'),
33-
'blocked' => new ConstStub($info['blocked'] ? 'true' : 'false'),
34-
'eof' => new ConstStub($info['eof'] ? 'true' : 'false'),
35-
'unread_bytes' => new ScalarStub($info['unread_bytes']),
36-
'stream_type' => new ConstStub($info['stream_type']),
37-
'wrapper_type' => new ConstStub($info['wrapper_type'] ?? ''),
38-
'wrapper_data&# 1C6A 39; => new ConstStub($info['wrapper_data'] ?? ''),
39-
'mode' => new ConstStub($info['mode']),
40-
'seekable' => new ConstStub($info['seekable'] ? 'true' : 'false'),
41-
'uri' => new ConstStub($info['uri'] ?? ''),
42-
]
43-
)),
4426
];
4527

28+
foreach ($info as $key => $val) {
29+
$a[Caster::PREFIX_VIRTUAL.$key] = $val;
30+
}
31+
4632
return $a;
4733
}
4834
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,18 @@
1616
/**
1717
* @author Alexandre Daubois <alex.daubois@gmail.com>
1818
*/
19-
class SqliteCaster
19+
final class SqliteCaster
2020
{
2121
public static function castSqlite3Result(\SQLite3Result $c, array $a, Stub $stub, bool $isNested): array
2222
{
2323
$a += [
2424
Caster::PREFIX_VIRTUAL.'numColumns' => $c->numColumns(),
25-
Caster::PREFIX_VIRTUAL.'result' => $c->fetchArray(\SQLITE3_ASSOC),
2625
];
2726

27+
for ($i = 0; $i < $c->numColumns(); ++$i) {
28+
$a[Caster::PREFIX_VIRTUAL.'columnName'][$i] = $c->columnName($i);
29+
}
30+
2831
return $a;
2932
}
3033
}

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

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -176,39 +176,31 @@ abstract class AbstractCloner implements ClonerInterface
176176

177177
'mysqli_driver' => ['Symfony\Component\VarDumper\Caster\MysqliCaster', 'castMysqliDriver'],
178178

179-
'CurlHandle' => ['Symfony\Component\VarDumper\Caster\CurlCaster', 'castCurl'],
180-
'CurlMultiHandle' => ['Symfony\Component\VarDumper\Caster\CurlCaster', 'castCurlMulti'],
179+
'CurlHandle' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castCurl'],
181180

182-
'Dba\Connection' => ['Symfony\Component\VarDumper\Caster\DbaCaster', 'castDbaConnection'],
183-
':dba' => ['Symfony\Component\VarDumper\Caster\DbaCaster', 'castDbaResource'],
184-
':dba persistent' => ['Symfony\Component\VarDumper\Caster\DbaCaster', 'castDbaResource'],
181+
'Dba\Connection' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castDba'],
182+
':dba' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castDba'],
183+
':dba persistent' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castDba'],
185184

186185
'GdImage' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castGd'],
187-
':gd' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castGd'],
188186

189187
'SQLite3Result' => ['Symfony\Component\VarDumper\Caster\SqliteCaster', 'castSqlite3Result'],
190188

191189
'PgSql\Lob' => ['Symfony\Component\VarDumper\Caster\PgSqlCaster', 'castLargeObject'],
192-
':pgsql large object' => ['Symfony\Component\VarDumper\Caster\PgSqlCaster', 'castLargeObject'],
193190
'PgSql\Connection' => ['Symfony\Component\VarDumper\Caster\PgSqlCaster', 'castLink'],
194-
':pgsql link' => ['Symfony\Component\VarDumper\Caster\PgSqlCaster', 'castLink'],
195-
':pgsql link persistent' => ['Symfony\Component\VarDumper\Caster\PgSqlCaster', 'castLink'],
196191
'PgSql\Result' => ['Symfony\Component\VarDumper\Caster\PgSqlCaster', 'castResult'],
197-
':pgsql result' => ['Symfony\Component\VarDumper\Caster\PgSqlCaster', 'castResult'],
198192

199193
':process' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castProcess'],
200194
':stream' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castStream'],
201195

202-
'OpenSSLAsymmetricKey' => ['Symfony\Component\VarDumper\Caster\OpenSslCaster', 'castOpensslAsymmetricKey'],
203-
'OpenSSLCertificateSigningRequest' => ['Symfony\Component\VarDumper\Caster\OpenSslCaster', 'castOpensslCsr'],
204-
'OpenSSLCertificate' => ['Symfony\Component\VarDumper\Caster\OpenSslCaster', 'castOpensslX509'],
205-
':OpenSSL X.509' => ['Symfony\Component\VarDumper\Caster\OpenSslCaster', 'castOpensslX509'],
196+
'OpenSSLAsymmetricKey' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castOpensslAsymmetricKey'],
197+
'OpenSSLCertificateSigningRequest' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castOpensslCsr'],
198+
'OpenSSLCertificate' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castOpensslX509'],
206199

207200
':persistent stream' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castStream'],
208201
':stream-context' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castStreamContext'],
209202

210203
'XmlParser' => ['Symfony\Component\VarDumper\Caster\XmlResourceCaster', 'castXml'],
211-
':xml' => ['Symfony\Component\VarDumper\Caster\XmlResourceCaster', 'castXml'],
212204

213205
'Socket' => ['Symfony\Component\VarDumper\Caster\SocketCaster', 'castSocket'],
214206

0 commit comments

Comments
 (0)
0