Connector.php 3.4 KB

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