ResourceCaster.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. * Casts common resource types to array representation.
  14. *
  15. * @author Nicolas Grekas <p@tchwork.com>
  16. *
  17. * @final
  18. */
  19. class ResourceCaster
  20. {
  21. public static function castCurl(\CurlHandle $h, array $a, Stub $stub, bool $isNested): array
  22. {
  23. return curl_getinfo($h);
  24. }
  25. public static function castDba($dba, array $a, Stub $stub, bool $isNested): array
  26. {
  27. $list = dba_list();
  28. $a['file'] = $list[(int) $dba];
  29. return $a;
  30. }
  31. public static function castProcess($process, array $a, Stub $stub, bool $isNested): array
  32. {
  33. return proc_get_status($process);
  34. }
  35. public static function castStream($stream, array $a, Stub $stub, bool $isNested): array
  36. {
  37. $a = stream_get_meta_data($stream) + static::castStreamContext($stream, $a, $stub, $isNested);
  38. if ($a['uri'] ?? false) {
  39. $a['uri'] = new LinkStub($a['uri']);
  40. }
  41. return $a;
  42. }
  43. public static function castStreamContext($stream, array $a, Stub $stub, bool $isNested): array
  44. {
  45. return @stream_context_get_params($stream) ?: $a;
  46. }
  47. public static function castGd($gd, array $a, Stub $stub, bool $isNested): array
  48. {
  49. $a['size'] = imagesx($gd).'x'.imagesy($gd);
  50. $a['trueColor'] = imageistruecolor($gd);
  51. return $a;
  52. }
  53. public static function castOpensslX509($h, array $a, Stub $stub, bool $isNested): array
  54. {
  55. $stub->cut = -1;
  56. $info = openssl_x509_parse($h, false);
  57. $pin = openssl_pkey_get_public($h);
  58. $pin = openssl_pkey_get_details($pin)['key'];
  59. $pin = \array_slice(explode("\n", $pin), 1, -2);
  60. $pin = base64_decode(implode('', $pin));
  61. $pin = base64_encode(hash('sha256', $pin, true));
  62. $a += [
  63. 'subject' => new EnumStub(array_intersect_key($info['subject'], ['organizationName' => true, 'commonName' => true])),
  64. 'issuer' => new EnumStub(array_intersect_key($info['issuer'], ['organizationName' => true, 'commonName' => true])),
  65. 'expiry' => new ConstStub(date(\DateTimeInterface::ISO8601, $info['validTo_time_t']), $info['validTo_time_t']),
  66. 'fingerprint' => new EnumStub([
  67. 'md5' => new ConstStub(wordwrap(strtoupper(openssl_x509_fingerprint($h, 'md5')), 2, ':', true)),
  68. 'sha1' => new ConstStub(wordwrap(strtoupper(openssl_x509_fingerprint($h, 'sha1')), 2, ':', true)),
  69. 'sha256' => new ConstStub(wordwrap(strtoupper(openssl_x509_fingerprint($h, 'sha256')), 2, ':', true)),
  70. 'pin-sha256' => new ConstStub($pin),
  71. ]),
  72. ];
  73. return $a;
  74. }
  75. }