|
3 | 3 | namespace Phaseolies\Database\Drivers; |
4 | 4 |
|
5 | 5 | use PDO; |
| 6 | +use Pdo\Mysql; |
6 | 7 | use Phaseolies\Database\Contracts\Driver\DriverInterface; |
7 | 8 |
|
8 | 9 | class MySQLDriver implements DriverInterface |
@@ -30,26 +31,41 @@ public function connect(array $config): PDO |
30 | 31 | PDO::ATTR_STRINGIFY_FETCHES => false, |
31 | 32 | ]; |
32 | 33 |
|
33 | | - if (!empty($config['options'][PDO::MYSQL_ATTR_SSL_CA])) { |
34 | | - $options[PDO::MYSQL_ATTR_SSL_CA] = $config['options'][PDO::MYSQL_ATTR_SSL_CA]; |
35 | | - $options[PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT] = false; |
| 34 | + $sslCa = $this->mysqlAttr('ATTR_SSL_CA', 'MYSQL_ATTR_SSL_CA'); |
36 | 35 |
|
37 | | - $sslOptions = [ |
38 | | - PDO::MYSQL_ATTR_SSL_CERT, |
39 | | - PDO::MYSQL_ATTR_SSL_KEY, |
40 | | - PDO::MYSQL_ATTR_SSL_CAPATH, |
41 | | - PDO::MYSQL_ATTR_SSL_CIPHER |
42 | | - ]; |
| 36 | + if ($sslCa !== null && !empty($config['options'][$sslCa])) { |
| 37 | + $options[$sslCa] = $config['options'][$sslCa]; |
43 | 38 |
|
44 | | - foreach ($sslOptions as $sslOption) { |
45 | | - if (!empty($config['options'][$sslOption])) { |
46 | | - $options[$sslOption] = $config['options'][$sslOption]; |
| 39 | + $verify = $this->mysqlAttr( |
| 40 | + 'ATTR_SSL_VERIFY_SERVER_CERT', |
| 41 | + 'MYSQL_ATTR_SSL_VERIFY_SERVER_CERT' |
| 42 | + ); |
| 43 | + |
| 44 | + if ($verify !== null) { |
| 45 | + $options[$verify] = false; |
| 46 | + } |
| 47 | + |
| 48 | + foreach ( |
| 49 | + [ |
| 50 | + ['ATTR_SSL_CERT', 'MYSQL_ATTR_SSL_CERT'], |
| 51 | + ['ATTR_SSL_KEY', 'MYSQL_ATTR_SSL_KEY'], |
| 52 | + ['ATTR_SSL_CAPATH', 'MYSQL_ATTR_SSL_CAPATH'], |
| 53 | + ['ATTR_SSL_CIPHER', 'MYSQL_ATTR_SSL_CIPHER'], |
| 54 | + ] as [$new, $old] |
| 55 | + ) { |
| 56 | + |
| 57 | + $attr = $this->mysqlAttr($new, $old); |
| 58 | + |
| 59 | + if ($attr !== null && isset($config['options'][$attr])) { |
| 60 | + $options[$attr] = $config['options'][$attr]; |
47 | 61 | } |
48 | 62 | } |
49 | 63 | } |
50 | 64 |
|
51 | | - if (!empty($config['options'][PDO::MYSQL_ATTR_INIT_COMMAND])) { |
52 | | - $options[PDO::MYSQL_ATTR_INIT_COMMAND] = $config['options'][PDO::MYSQL_ATTR_INIT_COMMAND]; |
| 65 | + $attr = $this->mysqlAttr('ATTR_INIT_COMMAND', 'MYSQL_ATTR_INIT_COMMAND'); |
| 66 | + |
| 67 | + if ($attr !== null && isset($config['options'][$attr])) { |
| 68 | + $options[$attr] = $config['options'][$attr]; |
53 | 69 | } |
54 | 70 |
|
55 | 71 | $pdo = new PDO( |
@@ -238,4 +254,34 @@ public function deleteAll(PDO $pdo, string $table): int |
238 | 254 | { |
239 | 255 | return (int) $pdo->exec("DELETE FROM {$table}"); |
240 | 256 | } |
| 257 | + |
| 258 | + /** |
| 259 | + * Resolve a MySQL PDO attribute constant in a version-compatible way |
| 260 | + * |
| 261 | + * PHP 8.5 moved MySQL-specific PDO constants from PDO::MYSQL_ATTR_* |
| 262 | + * to the namespaced Pdo\Mysql::ATTR_* class and deprecated the old ones. |
| 263 | + * |
| 264 | + * This helper safely resolves the correct constant for: |
| 265 | + * - PHP 8.5+ (Pdo\Mysql::ATTR_*) |
| 266 | + * - PHP 8.3 / 8.4 (PDO::MYSQL_ATTR_*) |
| 267 | + * |
| 268 | + * If neither constant exists (e.g., extension not available), |
| 269 | + * null is returned. |
| 270 | + * |
| 271 | + * @param string $new The new constant name (e.g. 'ATTR_SSL_CA') |
| 272 | + * @param string $old The legacy constant name (e.g. 'MYSQL_ATTR_SSL_CA') |
| 273 | + * @return int|null |
| 274 | + */ |
| 275 | + private function mysqlAttr(string $new, string $old): ?int |
| 276 | + { |
| 277 | + if (class_exists(Mysql::class) && defined("Pdo\\Mysql::$new")) { |
| 278 | + return constant("Pdo\\Mysql::$new"); |
| 279 | + } |
| 280 | + |
| 281 | + if (defined("PDO::$old")) { |
| 282 | + return constant("PDO::$old"); |
| 283 | + } |
| 284 | + |
| 285 | + return null; |
| 286 | + } |
241 | 287 | } |
0 commit comments