PdoCaster.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 PDO related classes to array representation.
  14. *
  15. * @author Nicolas Grekas <p@tchwork.com>
  16. *
  17. * @final since Symfony 4.4
  18. */
  19. class PdoCaster
  20. {
  21. private const PDO_ATTRIBUTES = [
  22. 'CASE' => [
  23. \PDO::CASE_LOWER => 'LOWER',
  24. \PDO::CASE_NATURAL => 'NATURAL',
  25. \PDO::CASE_UPPER => 'UPPER',
  26. ],
  27. 'ERRMODE' => [
  28. \PDO::ERRMODE_SILENT => 'SILENT',
  29. \PDO::ERRMODE_WARNING => 'WARNING',
  30. \PDO::ERRMODE_EXCEPTION => 'EXCEPTION',
  31. ],
  32. 'TIMEOUT',
  33. 'PREFETCH',
  34. 'AUTOCOMMIT',
  35. 'PERSISTENT',
  36. 'DRIVER_NAME',
  37. 'SERVER_INFO',
  38. 'ORACLE_NULLS' => [
  39. \PDO::NULL_NATURAL => 'NATURAL',
  40. \PDO::NULL_EMPTY_STRING => 'EMPTY_STRING',
  41. \PDO::NULL_TO_STRING => 'TO_STRING',
  42. ],
  43. 'CLIENT_VERSION',
  44. 'SERVER_VERSION',
  45. 'STATEMENT_CLASS',
  46. 'EMULATE_PREPARES',
  47. 'CONNECTION_STATUS',
  48. 'STRINGIFY_FETCHES',
  49. 'DEFAULT_FETCH_MODE' => [
  50. \PDO::FETCH_ASSOC => 'ASSOC',
  51. \PDO::FETCH_BOTH => 'BOTH',
  52. \PDO::FETCH_LAZY => 'LAZY',
  53. \PDO::FETCH_NUM => 'NUM',
  54. \PDO::FETCH_OBJ => 'OBJ',
  55. ],
  56. ];
  57. public static function castPdo(\PDO $c, array $a, Stub $stub, $isNested)
  58. {
  59. $attr = [];
  60. $errmode = $c->getAttribute(\PDO::ATTR_ERRMODE);
  61. $c->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
  62. foreach (self::PDO_ATTRIBUTES as $k => $v) {
  63. if (!isset($k[0])) {
  64. $k = $v;
  65. $v = [];
  66. }
  67. try {
  68. $attr[$k] = 'ERRMODE' === $k ? $errmode : $c->getAttribute(\constant('PDO::ATTR_'.$k));
  69. if ($v && isset($v[$attr[$k]])) {
  70. $attr[$k] = new ConstStub($v[$attr[$k]], $attr[$k]);
  71. }
  72. } catch (\Exception $e) {
  73. }
  74. }
  75. if (isset($attr[$k = 'STATEMENT_CLASS'][1])) {
  76. if ($attr[$k][1]) {
  77. $attr[$k][1] = new ArgsStub($attr[$k][1], '__construct', $attr[$k][0]);
  78. }
  79. $attr[$k][0] = new ClassStub($attr[$k][0]);
  80. }
  81. $prefix = Caster::PREFIX_VIRTUAL;
  82. $a += [
  83. $prefix.'inTransaction' => method_exists($c, 'inTransaction'),
  84. $prefix.'errorInfo' => $c->errorInfo(),
  85. $prefix.'attributes' => new EnumStub($attr),
  86. ];
  87. if ($a[$prefix.'inTransaction']) {
  88. $a[$prefix.'inTransaction'] = $c->inTransaction();
  89. } else {
  90. unset($a[$prefix.'inTransaction']);
  91. }
  92. if (!isset($a[$prefix.'errorInfo'][1], $a[$prefix.'errorInfo'][2])) {
  93. unset($a[$prefix.'errorInfo']);
  94. }
  95. $c->setAttribute(\PDO::ATTR_ERRMODE, $errmode);
  96. return $a;
  97. }
  98. public static function castPdoStatement(\PDOStatement $c, array $a, Stub $stub, $isNested)
  99. {
  100. $prefix = Caster::PREFIX_VIRTUAL;
  101. $a[$prefix.'errorInfo'] = $c->errorInfo();
  102. if (!isset($a[$prefix.'errorInfo'][1], $a[$prefix.'errorInfo'][2])) {
  103. unset($a[$prefix.'errorInfo']);
  104. }
  105. return $a;
  106. }
  107. }