PdoCaster.php 3.2 KB

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