Connector.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace Illuminate\Database\Connectors;
  3. use Exception;
  4. use Illuminate\Database\DetectsLostConnections;
  5. use PDO;
  6. use Throwable;
  7. class Connector
  8. {
  9. use DetectsLostConnections;
  10. /**
  11. * The default PDO connection options.
  12. *
  13. * @var array
  14. */
  15. protected $options = [
  16. PDO::ATTR_CASE => PDO::CASE_NATURAL,
  17. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  18. PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL,
  19. PDO::ATTR_STRINGIFY_FETCHES => false,
  20. PDO::ATTR_EMULATE_PREPARES => false,
  21. ];
  22. /**
  23. * Create a new PDO connection.
  24. *
  25. * @param string $dsn
  26. * @param array $config
  27. * @param array $options
  28. * @return \PDO
  29. *
  30. * @throws \Exception
  31. */
  32. public function createConnection($dsn, array $config, array $options)
  33. {
  34. [$username, $password] = [
  35. $config['username'] ?? null, $config['password'] ?? null,
  36. ];
  37. try {
  38. return $this->createPdoConnection(
  39. $dsn, $username, $password, $options
  40. );
  41. } catch (Exception $e) {
  42. return $this->tryAgainIfCausedByLostConnection(
  43. $e, $dsn, $username, $password, $options
  44. );
  45. }
  46. }
  47. /**
  48. * Create a new PDO connection instance.
  49. *
  50. * @param string $dsn
  51. * @param string $username
  52. * @param string $password
  53. * @param array $options
  54. * @return \PDO
  55. */
  56. protected function createPdoConnection($dsn, $username, $password, $options)
  57. {
  58. return new PDO($dsn, $username, $password, $options);
  59. }
  60. /**
  61. * Handle an exception that occurred during connect execution.
  62. *
  63. * @param \Throwable $e
  64. * @param string $dsn
  65. * @param string $username
  66. * @param string $password
  67. * @param array $options
  68. * @return \PDO
  69. *
  70. * @throws \Throwable
  71. */
  72. protected function tryAgainIfCausedByLostConnection(Throwable $e, $dsn, $username, $password, $options)
  73. {
  74. if ($this->causedByLostConnection($e)) {
  75. return $this->createPdoConnection($dsn, $username, $password, $options);
  76. }
  77. throw $e;
  78. }
  79. /**
  80. * Get the PDO options based on the configuration.
  81. *
  82. * @param array $config
  83. * @return array
  84. */
  85. public function getOptions(array $config)
  86. {
  87. $options = $config['options'] ?? [];
  88. return array_diff_key($this->options, $options) + $options;
  89. }
  90. /**
  91. * Get the default PDO connection options.
  92. *
  93. * @return array
  94. */
  95. public function getDefaultOptions()
  96. {
  97. return $this->options;
  98. }
  99. /**
  100. * Set the default PDO connection options.
  101. *
  102. * @param array $options
  103. * @return void
  104. */
  105. public function setDefaultOptions(array $options)
  106. {
  107. $this->options = $options;
  108. }
  109. }