|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Arris\Database; |
| 4 | + |
| 5 | +use Psr\Log\AbstractLogger; |
| 6 | +use Psr\Log\LoggerInterface; |
| 7 | +use Psr\Log\NullLogger; |
| 8 | + |
| 9 | +class DBConfig |
| 10 | +{ |
| 11 | + const DEFAULT_CHARSET = 'utf8'; |
| 12 | + const DEFAULT_CHARSET_COLLATE = 'utf8_general_ci'; |
| 13 | + |
| 14 | + /** |
| 15 | + * @var AbstractLogger |
| 16 | + */ |
| 17 | + public $logger; |
| 18 | + |
| 19 | + /** |
| 20 | + * @var array |
| 21 | + */ |
| 22 | + public $db_config; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var string |
| 26 | + */ |
| 27 | + public $driver; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var string |
| 31 | + */ |
| 32 | + public $hostname; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var int |
| 36 | + */ |
| 37 | + public $port; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var string |
| 41 | + */ |
| 42 | + public $username; |
| 43 | + |
| 44 | + /** |
| 45 | + * @var string |
| 46 | + */ |
| 47 | + public $password; |
| 48 | + |
| 49 | + /** |
| 50 | + * @var string |
| 51 | + */ |
| 52 | + public $database; |
| 53 | + |
| 54 | + /** |
| 55 | + * @var bool |
| 56 | + */ |
| 57 | + public $is_lazy; |
| 58 | + |
| 59 | + /** |
| 60 | + * @var string |
| 61 | + */ |
| 62 | + public $charset; |
| 63 | + |
| 64 | + /** |
| 65 | + * @var string |
| 66 | + */ |
| 67 | + public $charset_collate; |
| 68 | + |
| 69 | + /** |
| 70 | + * @var float |
| 71 | + */ |
| 72 | + public $slow_query_threshold; |
| 73 | + |
| 74 | + /** |
| 75 | + * @var int |
| 76 | + */ |
| 77 | + public $total_queries = 0; |
| 78 | + |
| 79 | + /** |
| 80 | + * @var float |
| 81 | + */ |
| 82 | + public $total_time = 0; |
| 83 | + |
| 84 | + public function __construct(array $connection_config, array $options = [], LoggerInterface $logger = null) |
| 85 | + { |
| 86 | + $this->logger = is_null($logger) ? new NullLogger() : $logger; |
| 87 | + |
| 88 | + if (empty($connection_config)) { |
| 89 | + $this->logger->emergency("[DBWrapper Error] Connection config is empty"); |
| 90 | + throw new \RuntimeException("[DBWrapper Error] Connection config is empty"); |
| 91 | + } |
| 92 | + |
| 93 | + $this->db_config = $connection_config; |
| 94 | + $this->driver = $this->db_config['driver'] ?? 'mysql'; |
| 95 | + $this->hostname = $this->db_config['hostname'] ?? '127.0.0.1'; |
| 96 | + $this->port = $this->db_config['port'] ?? 3306; |
| 97 | + $this->username = $this->db_config['username'] ?? 'root'; |
| 98 | + $this->password = $this->db_config['password']; |
| 99 | + $this->database = $this->db_config['database']; |
| 100 | + $this->is_lazy = true; |
| 101 | + |
| 102 | + if (!array_key_exists('charset', $this->db_config)) { |
| 103 | + $this->charset = self::DEFAULT_CHARSET; |
| 104 | + } elseif (!is_null($this->db_config['charset'])) { |
| 105 | + $this->charset = $this->db_config['charset']; |
| 106 | + } else { |
| 107 | + $this->charset = null; |
| 108 | + } |
| 109 | + |
| 110 | + if (!array_key_exists('charset_collate', $this->db_config)) { |
| 111 | + $this->charset_collate = self::DEFAULT_CHARSET_COLLATE; |
| 112 | + } elseif (!is_null($this->db_config['charset_collate'])) { |
| 113 | + $this->charset_collate = $this->db_config['charset_collate']; |
| 114 | + } else { |
| 115 | + $this->charset_collate = null; |
| 116 | + } |
| 117 | + |
| 118 | + // ms |
| 119 | + $this->slow_query_threshold = (array_key_exists('slow_query_threshold', $options)) ? (float)$options['slow_query_threshold'] : 1000; |
| 120 | + |
| 121 | + // $this->is_lazy = array_key_exists('lazy', $options) ? (bool)$options['lazy'] : true; |
| 122 | + $this->is_lazy = !array_key_exists('lazy', $options) || (bool)$options['lazy']; // if default = false -> remove '!' from first part. |
| 123 | + |
| 124 | + // microseconds |
| 125 | + $this->slow_query_threshold /= 1000; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * @param $time |
| 130 | + * @param int $decimals |
| 131 | + * @param string $decimal_separator |
| 132 | + * @param string $thousands_separator |
| 133 | + * @return string |
| 134 | + */ |
| 135 | + public function formatTime($time = 0, int $decimals = 6, string $decimal_separator = '.', string $thousands_separator = ''): string |
| 136 | + { |
| 137 | + return \number_format($time, $decimals, $decimal_separator, $thousands_separator); |
| 138 | + } |
| 139 | + |
| 140 | +} |
0 commit comments