Skip to content

Commit bcb56c1

Browse files
authored
Fix: PHP 8.5 PDO MySQL Constant Deprecations (Backward Compatible) (#184)
2 parents b995d86 + ccf68ff commit bcb56c1

File tree

1 file changed

+60
-14
lines changed

1 file changed

+60
-14
lines changed

src/Phaseolies/Database/Drivers/MySQLDriver.php

Lines changed: 60 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Phaseolies\Database\Drivers;
44

55
use PDO;
6+
use Pdo\Mysql;
67
use Phaseolies\Database\Contracts\Driver\DriverInterface;
78

89
class MySQLDriver implements DriverInterface
@@ -30,26 +31,41 @@ public function connect(array $config): PDO
3031
PDO::ATTR_STRINGIFY_FETCHES => false,
3132
];
3233

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');
3635

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];
4338

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];
4761
}
4862
}
4963
}
5064

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];
5369
}
5470

5571
$pdo = new PDO(
@@ -238,4 +254,34 @@ public function deleteAll(PDO $pdo, string $table): int
238254
{
239255
return (int) $pdo->exec("DELETE FROM {$table}");
240256
}
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+
}
241287
}

0 commit comments

Comments
 (0)