123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- <?php
-
- namespace Illuminate\Database\Connectors;
-
- use PDO;
- use Exception;
- use Throwable;
- use Doctrine\DBAL\Driver\PDOConnection;
- use Illuminate\Database\DetectsLostConnections;
-
- class Connector
- {
- use DetectsLostConnections;
-
- /**
- * The default PDO connection options.
- *
- * @var array
- */
- protected $options = [
- PDO::ATTR_CASE => PDO::CASE_NATURAL,
- PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
- PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL,
- PDO::ATTR_STRINGIFY_FETCHES => false,
- PDO::ATTR_EMULATE_PREPARES => false,
- ];
-
- /**
- * Create a new PDO connection.
- *
- * @param string $dsn
- * @param array $config
- * @param array $options
- * @return \PDO
- */
- public function createConnection($dsn, array $config, array $options)
- {
- list($username, $password) = [
- $config['username'] ?? null, $config['password'] ?? null,
- ];
-
- try {
- return $this->createPdoConnection(
- $dsn, $username, $password, $options
- );
- } catch (Exception $e) {
- return $this->tryAgainIfCausedByLostConnection(
- $e, $dsn, $username, $password, $options
- );
- }
- }
-
- /**
- * Create a new PDO connection instance.
- *
- * @param string $dsn
- * @param string $username
- * @param string $password
- * @param array $options
- * @return \PDO
- */
- protected function createPdoConnection($dsn, $username, $password, $options)
- {
- if (class_exists(PDOConnection::class) && ! $this->isPersistentConnection($options)) {
- return new PDOConnection($dsn, $username, $password, $options);
- }
-
- return new PDO($dsn, $username, $password, $options);
- }
-
- /**
- * Determine if the connection is persistent.
- *
- * @param array $options
- * @return bool
- */
- protected function isPersistentConnection($options)
- {
- return isset($options[PDO::ATTR_PERSISTENT]) &&
- $options[PDO::ATTR_PERSISTENT];
- }
-
- /**
- * Handle an exception that occurred during connect execution.
- *
- * @param \Throwable $e
- * @param string $dsn
- * @param string $username
- * @param string $password
- * @param array $options
- * @return \PDO
- *
- * @throws \Exception
- */
- protected function tryAgainIfCausedByLostConnection(Throwable $e, $dsn, $username, $password, $options)
- {
- if ($this->causedByLostConnection($e)) {
- return $this->createPdoConnection($dsn, $username, $password, $options);
- }
-
- throw $e;
- }
-
- /**
- * Get the PDO options based on the configuration.
- *
- * @param array $config
- * @return array
- */
- public function getOptions(array $config)
- {
- $options = $config['options'] ?? [];
-
- return array_diff_key($this->options, $options) + $options;
- }
-
- /**
- * Get the default PDO connection options.
- *
- * @return array
- */
- public function getDefaultOptions()
- {
- return $this->options;
- }
-
- /**
- * Set the default PDO connection options.
- *
- * @param array $options
- * @return void
- */
- public function setDefaultOptions(array $options)
- {
- $this->options = $options;
- }
- }
|