Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d65d78e

Browse files
[VarDumper] Simplify things
1 parent 164264c commit d65d78e

13 files changed

+158
-352
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: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,32 @@
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+
/**
36+
* @param resource|\Dba\Connection $dba
37+
*/
38+
public static function castDba($dba, array $a, Stub $stub, bool $isNested): array
39+
{
40+
if (\PHP_VERSION_ID < 80402 && !\is_resource($dba)) {
41+
// @see https://github.com/php/php-src/issues/16990
42+
return $a;
43+
}
44+
45+
$list = dba_list();
46+
$a['file'] = $list[(int) $dba];
47+
48+
return $a;
49+
}
50+
2551
public static function castProcess($process, array $a, Stub $stub, bool $isNested): array
2652
{
2753
return proc_get_status($process);
@@ -42,11 +68,63 @@ public static function castStreamContext($stream, array $a, Stub $stub, bool $is
4268
return @stream_context_get_params($stream) ?: $a;
4369
}
4470

71+
/**
72+
* @param \GdImage $gd
73+
*/
4574
public static function castGd($gd, array $a, Stub $stub, bool $isNested): array
4675
{
4776
$a['size'] = imagesx($gd).'x'.imagesy($gd);
4877
$a['trueColor'] = imageistruecolor($gd);
4978

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

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

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,37 +13,22 @@
1313

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

16-
/**
17-
* @author Nicolas Grekas <[email protected]>
18-
* @author Alexandre Daubois <[email protected]>
19-
*/
2016
final class SocketCaster
2117
{
22-
public static function castSocket(\Socket $h, array $a, Stub $stub, bool $isNested): array
18+
public static function castSocket(\Socket $socket, array $a, Stub $stub, bool $isNested): array
2319
{
24-
socket_getsockname($h, $addr, $port);
25-
$info = stream_get_meta_data(socket_export_stream($h));
20+
socket_getsockname($socket, $addr, $port);
21+
$info = stream_get_meta_data(socket_export_stream($socket));
2622

2723
$a += [
2824
Caster::PREFIX_VIRTUAL.'address' => $addr,
2925
Caster::PREFIX_VIRTUAL.'port' => $port,
30-
Caster::PREFIX_VIRTUAL.'info' => new EnumStub(array_intersect_key(
31-
$info,
32-
[
33-
'timed_out' => new ConstStub($info['timed_out'] ? 'true' : 'false'),
34-
'blocked' => new ConstStub($info['blocked'] ? 'true' : 'false'),
35-
'eof' => new ConstStub($info['eof'] ? 'true' : 'false'),
36-
'unread_bytes' => new ScalarStub($info['unread_bytes']),
37-
'stream_type' => new ConstStub($info['stream_type']),
38-
'wrapper_type' => new ConstStub($info['wrapper_type'] ?? ''),
39-
'wrapper_data' => new ConstStub($info['wrapper_data'] ?? ''),
40-
'mode' => new ConstStub($info['mode']),
41-
'seekable' => new ConstStub($info['seekable'] ? 'true' : 'false'),
42-
'uri' => new ConstStub($info['uri'] ?? ''),
43-
]
44-
)),
4526
];
4627

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

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

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

27+
for ($i = 0; $i < $result->numColumns(); ++$i) {
28+
$a[Caster::PREFIX_VIRTUAL.'columnName'][$i] = $result->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
@@ -177,39 +177,31 @@ abstract class AbstractCloner implements ClonerInterface
177177

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

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

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

187186
'GdImage' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castGd'],
188-
':gd' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castGd'],
189187

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

192190
'PgSql\Lob' => ['Symfony\Component\VarDumper\Caster\PgSqlCaster', 'castLargeObject'],
193-
':pgsql large object' => ['Symfony\Component\VarDumper\Caster\PgSqlCaster', 'castLargeObject'],
194191
'PgSql\Connection' => ['Symfony\Component\VarDumper\Caster\PgSqlCaster', 'castLink'],
195-
':pgsql link' => ['Symfony\Component\VarDumper\Caster\PgSqlCaster', 'castLink'],
196-
':pgsql link persistent' => ['Symfony\Component\VarDumper\Caster\PgSqlCaster', 'castLink'],
197192
'PgSql\Result' => ['Symfony\Component\VarDumper\Caster\PgSqlCaster', 'castResult'],
198-
':pgsql result' => ['Symfony\Component\VarDumper\Caster\PgSqlCaster', 'castResult'],
199193

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

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

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

211204
'XmlParser' => ['Symfony\Component\VarDumper\Caster\XmlResourceCaster', 'castXml'],
212-
':xml' => ['Symfony\Component\VarDumper\Caster\XmlResourceCaster', 'castXml'],
213205

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

0 commit comments

Comments
 (0)