PdoCaster.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. if (isset($attr[$k = 'STATEMENT_CLASS'][1])) {
  74. if ($attr[$k][1]) {
  75. $attr[$k][1] = new ArgsStub($attr[$k][1], '__construct', $attr[$k][0]);
  76. }
  77. $attr[$k][0] = new ClassStub($attr[$k][0]);
  78. }
  79. $prefix = Caster::PREFIX_VIRTUAL;
  80. $a += array(
  81. $prefix.'inTransaction' => method_exists($c, 'inTransaction'),
  82. $prefix.'errorInfo' => $c->errorInfo(),
  83. $prefix.'attributes' => new EnumStub($attr),
  84. );
  85. if ($a[$prefix.'inTransaction']) {
  86. $a[$prefix.'inTransaction'] = $c->inTransaction();
  87. } else {
  88. unset($a[$prefix.'inTransaction']);
  89. }
  90. if (!isset($a[$prefix.'errorInfo'][1], $a[$prefix.'errorInfo'][2])) {
  91. unset($a[$prefix.'errorInfo']);
  92. }
  93. $c->setAttribute(\PDO::ATTR_ERRMODE, $errmode);
  94. return $a;
  95. }
  96. public static function castPdoStatement(\PDOStatement $c, array $a, Stub $stub, $isNested)
  97. {
  98. $prefix = Caster::PREFIX_VIRTUAL;
  99. $a[$prefix.'errorInfo'] = $c->errorInfo();
  100. if (!isset($a[$prefix.'errorInfo'][1], $a[$prefix.'errorInfo'][2])) {
  101. unset($a[$prefix.'errorInfo']);
  102. }
  103. return $a;
  104. }
  105. }