Connection.php
141 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @author Slavko Babic |
| 4 | * @date 2017-08-21 |
| 5 | */ |
| 6 | |
| 7 | namespace AmeliaBooking\Infrastructure\DB\PDO; |
| 8 | |
| 9 | use AmeliaBooking\Domain\Services\Settings\SettingsService; |
| 10 | use AmeliaBooking\Infrastructure\WP\SettingsService\SettingsStorage; |
| 11 | use \PDO; |
| 12 | |
| 13 | /** |
| 14 | * Class Connection |
| 15 | * |
| 16 | * @package AmeliaBooking\Infrastructure\DB\PDO |
| 17 | */ |
| 18 | class Connection extends \AmeliaBooking\Infrastructure\Connection |
| 19 | { |
| 20 | /** @var PDO $pdo */ |
| 21 | protected $pdo; |
| 22 | |
| 23 | /** @var string $dns */ |
| 24 | private $dns; |
| 25 | |
| 26 | /** @var string $driver */ |
| 27 | private $driver = 'mysql'; |
| 28 | |
| 29 | /** |
| 30 | * Connection constructor. |
| 31 | * |
| 32 | * @param string $database |
| 33 | * @param string $username |
| 34 | * @param string $password |
| 35 | * @param string $host |
| 36 | * @param int $port |
| 37 | * @param string $charset |
| 38 | */ |
| 39 | public function __construct( |
| 40 | $host, |
| 41 | $database, |
| 42 | $username, |
| 43 | $password, |
| 44 | $charset = 'utf8', |
| 45 | $port = 3306 |
| 46 | ) { |
| 47 | parent::__construct( |
| 48 | $host, |
| 49 | $database, |
| 50 | $username, |
| 51 | $password, |
| 52 | $charset, |
| 53 | $port |
| 54 | ); |
| 55 | |
| 56 | $this->handler = new PDO( |
| 57 | $this->dns(), |
| 58 | $this->username, |
| 59 | $this->password, |
| 60 | $this->getOptions() |
| 61 | ); |
| 62 | |
| 63 | $settingsService = new SettingsService(new SettingsStorage()); |
| 64 | |
| 65 | $emulatePrepares = $settingsService->getSetting('db', 'pdoEmulatePrepares'); |
| 66 | |
| 67 | $this->handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
| 68 | $this->handler->setAttribute( |
| 69 | PDO::ATTR_EMULATE_PREPARES, |
| 70 | $emulatePrepares !== null ? $emulatePrepares : false |
| 71 | ); |
| 72 | |
| 73 | $this->handler->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); |
| 74 | |
| 75 | $this->handler->exec('SET SESSION sql_mode = "TRADITIONAL"'); |
| 76 | |
| 77 | $this->handler->exec('SET FOREIGN_KEY_CHECKS = 0'); |
| 78 | |
| 79 | if ($settingsService->getSetting('db', 'pdoBigSelect')) { |
| 80 | $this->handler->exec('SET SQL_BIG_SELECTS = 1'); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * @return string |
| 86 | */ |
| 87 | private function dns() |
| 88 | { |
| 89 | if ($this->dns) { |
| 90 | return $this->dns; |
| 91 | } |
| 92 | |
| 93 | $this->socketHandler(); |
| 94 | |
| 95 | $socketPath = property_exists($this, 'socketPath') && $this->socketPath ? ";unix_socket=$this->socketPath" : ''; |
| 96 | |
| 97 | return $this->dns = "{$this->driver}:host={$this->host};port={$this->port}';dbname={$this->database}{$socketPath}"; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * @return array |
| 102 | */ |
| 103 | private function getOptions() |
| 104 | { |
| 105 | $options = [ |
| 106 | PDO::ATTR_ERRMODE, |
| 107 | PDO::ERRMODE_EXCEPTION, |
| 108 | ]; |
| 109 | |
| 110 | if (defined('DB_CHARSET')) { |
| 111 | $options[PDO::MYSQL_ATTR_INIT_COMMAND] = 'set names ' . DB_CHARSET; |
| 112 | } |
| 113 | |
| 114 | $settingsService = new SettingsService(new SettingsStorage()); |
| 115 | |
| 116 | $ssl = apply_filters('amelia_change_ssl_settings', $settingsService->getSetting('db', 'ssl')); |
| 117 | |
| 118 | if ($ssl['enable']) { |
| 119 | if ($ssl['enable']) { |
| 120 | if ($ssl['key'] !== null) { |
| 121 | $options[PDO::MYSQL_ATTR_SSL_KEY] = $ssl['key']; |
| 122 | } |
| 123 | |
| 124 | if ($ssl['cert'] !== null) { |
| 125 | $options[PDO::MYSQL_ATTR_SSL_CERT] = $ssl['cert']; |
| 126 | } |
| 127 | |
| 128 | if ($ssl['ca'] !== null) { |
| 129 | $options[PDO::MYSQL_ATTR_SSL_CA] = $ssl['ca']; |
| 130 | } |
| 131 | |
| 132 | if ($ssl['verify_cert'] !== null) { |
| 133 | $options[PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT] = $ssl['verify_cert']; |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | return $options; |
| 139 | } |
| 140 | } |
| 141 |