Do not require hostname for non-persistent MySQL connection and require for persistent

parent 14f5f154
<?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 ...@@ -49,11 +49,16 @@ class MysqliConnection implements PingableConnection, ServerInfoAwareConnection
{ {
$socket = $params['unix_socket'] ?? ini_get('mysqli.default_socket'); $socket = $params['unix_socket'] ?? ini_get('mysqli.default_socket');
$dbname = $params['dbname'] ?? null; $dbname = $params['dbname'] ?? null;
$host = $params['host'];
$port = $params['port'] ?? null; $port = $params['port'] ?? null;
if (! empty($params['persistent'])) { 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; $flags = $driverOptions[static::OPTION_FLAGS] ?? null;
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace Doctrine\DBAL\Tests\Driver\Mysqli; namespace Doctrine\DBAL\Tests\Driver\Mysqli;
use Doctrine\DBAL\Driver\Mysqli\HostRequired;
use Doctrine\DBAL\Driver\Mysqli\MysqliConnection; use Doctrine\DBAL\Driver\Mysqli\MysqliConnection;
use Doctrine\DBAL\Driver\Mysqli\MysqliException; use Doctrine\DBAL\Driver\Mysqli\MysqliException;
use Doctrine\DBAL\Platforms\MySqlPlatform; use Doctrine\DBAL\Platforms\MySqlPlatform;
...@@ -60,4 +61,10 @@ class MysqliConnectionTest extends FunctionalTestCase ...@@ -60,4 +61,10 @@ class MysqliConnectionTest extends FunctionalTestCase
restore_error_handler(); restore_error_handler();
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