Common
1 year ago
ContainerConfig
1 year ago
DB
1 year ago
Licence
1 year ago
Repository
1 year ago
Routes
1 year ago
Services
1 year ago
WP
1 year ago
Connection.php
1 year ago
Connection.php
112 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @author Slavko Babic |
| 5 | * @date 2017-08-21 |
| 6 | */ |
| 7 | |
| 8 | namespace AmeliaBooking\Infrastructure; |
| 9 | |
| 10 | use mysqli; |
| 11 | use PDO; |
| 12 | |
| 13 | /** |
| 14 | * Class Connection |
| 15 | * |
| 16 | * @package Infrastructure |
| 17 | */ |
| 18 | abstract class Connection |
| 19 | { |
| 20 | /** @var string $username */ |
| 21 | protected $username; |
| 22 | |
| 23 | /** @var string $password */ |
| 24 | protected $password; |
| 25 | |
| 26 | /** @var string $charset */ |
| 27 | protected $charset; |
| 28 | |
| 29 | /** @var PDO|mysqli $handler */ |
| 30 | protected $handler; |
| 31 | |
| 32 | /** @var int port */ |
| 33 | protected $port; |
| 34 | |
| 35 | /** @var string $host */ |
| 36 | protected $host; |
| 37 | |
| 38 | /** @var string $name */ |
| 39 | protected $database; |
| 40 | |
| 41 | /** @var string $socket */ |
| 42 | protected $socket; |
| 43 | |
| 44 | /** @var string $socketPath */ |
| 45 | protected $socketPath; |
| 46 | |
| 47 | /** |
| 48 | * Connection constructor. |
| 49 | * |
| 50 | * @param string $database |
| 51 | * @param string $username |
| 52 | * @param string $password |
| 53 | * @param string $host |
| 54 | * @param int $port |
| 55 | * @param string $charset |
| 56 | */ |
| 57 | public function __construct( |
| 58 | $host, |
| 59 | $database, |
| 60 | $username, |
| 61 | $password, |
| 62 | $charset = 'utf8', |
| 63 | $port = 3306 |
| 64 | ) { |
| 65 | $this->database = (string)$database; |
| 66 | $this->username = (string)$username; |
| 67 | $this->password = (string)$password; |
| 68 | $this->host = $this->socket = (string)$host; |
| 69 | $this->port = (int)$port; |
| 70 | $this->charset = (string)$charset; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * @return PDO|mysqli |
| 75 | */ |
| 76 | public function __invoke() |
| 77 | { |
| 78 | return $this->handler; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * |
| 83 | */ |
| 84 | protected function socketHandler() |
| 85 | { |
| 86 | if (strpos($this->socket, ':') === false) { |
| 87 | $this->host = $this->socket; |
| 88 | |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | $data = explode(':', $this->socket); |
| 93 | |
| 94 | $this->host = $data[0]; |
| 95 | |
| 96 | if (isset($data[1]) && is_numeric($data[1]) && (int)$data[1]) { |
| 97 | $this->port = $data[1]; |
| 98 | } elseif (isset($data[1]) && strpos($data[1], '/') !== false) { |
| 99 | $position = strpos($data[1], '/'); |
| 100 | |
| 101 | if ($position === 0) { |
| 102 | $this->socketPath = $data[1]; |
| 103 | } else { |
| 104 | $this->port = substr($data[1], 0, $position); |
| 105 | $this->socketPath = substr($data[1], $position); |
| 106 | } |
| 107 | } elseif (isset($data[1]) && (int)$data[1]) { |
| 108 | $this->port = $data[1]; |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 |