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