Unverified Commit e27d6558 authored by Sergei Morozov's avatar Sergei Morozov Committed by GitHub

Merge pull request #3943 from morozov/issues/3942

Do not require hostname for non-persistent MySQL connection and require for persistent
parents 14f5f154 b8a5cb22
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Driver\Mysqli;
/**
* @internal
*/
final class HostRequired extends MysqliException
{
public static function forPersistentConnection() : self
{
return new self('The "host" parameter is required for a persistent connection');
}
}
......@@ -49,11 +49,16 @@ class MysqliConnection implements PingableConnection, ServerInfoAwareConnection
{
$socket = $params['unix_socket'] ?? ini_get('mysqli.default_socket');
$dbname = $params['dbname'] ?? null;
$host = $params['host'];
$port = $params['port'] ?? null;
if (! empty($params['persistent'])) {
$host = 'p:' . $host;
if (! isset($params['host'])) {
throw HostRequired::forPersistentConnection();
}
$host = 'p:' . $params['host'];
} else {
$host = $params['host'] ?? null;
}
$flags = $driverOptions[static::OPTION_FLAGS] ?? null;
......
......@@ -2,6 +2,7 @@
namespace Doctrine\DBAL\Tests\Driver\Mysqli;
use Doctrine\DBAL\Driver\Mysqli\HostRequired;
use Doctrine\DBAL\Driver\Mysqli\MysqliConnection;
use Doctrine\DBAL\Driver\Mysqli\MysqliException;
use Doctrine\DBAL\Platforms\MySqlPlatform;
......@@ -60,4 +61,10 @@ class MysqliConnectionTest extends FunctionalTestCase
restore_error_handler();
restore_error_handler();
}
public function testHostnameIsRequiredForPersistentConnection() : void
{
$this->expectException(HostRequired::class);
new MysqliConnection(['persistent' => 'true'], '', '');
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment