MasterSlaveConnection.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace Doctrine\DBAL\Connections;
  3. use Doctrine\Common\EventManager;
  4. use Doctrine\DBAL\Configuration;
  5. use Doctrine\DBAL\Driver;
  6. use Doctrine\Deprecations\Deprecation;
  7. use InvalidArgumentException;
  8. /**
  9. * @deprecated Use PrimaryReadReplicaConnection instead
  10. *
  11. * @psalm-import-type Params from \Doctrine\DBAL\DriverManager
  12. */
  13. class MasterSlaveConnection extends PrimaryReadReplicaConnection
  14. {
  15. /**
  16. * Creates Primary Replica Connection.
  17. *
  18. * @internal The connection can be only instantiated by the driver manager.
  19. *
  20. * @param array<string,mixed> $params
  21. *
  22. * @throws InvalidArgumentException
  23. *
  24. * @phpstan-param array<string,mixed> $params
  25. * @psalm-param Params $params
  26. */
  27. public function __construct(
  28. array $params,
  29. Driver $driver,
  30. ?Configuration $config = null,
  31. ?EventManager $eventManager = null
  32. ) {
  33. $this->deprecated(self::class, PrimaryReadReplicaConnection::class);
  34. if (isset($params['master'])) {
  35. $this->deprecated('Params key "master"', '"primary"');
  36. $params['primary'] = $params['master'];
  37. }
  38. if (isset($params['slaves'])) {
  39. $this->deprecated('Params key "slaves"', '"replica"');
  40. $params['replica'] = $params['slaves'];
  41. }
  42. if (isset($params['keepSlave'])) {
  43. $this->deprecated('Params key "keepSlave"', '"keepReplica"');
  44. $params['keepReplica'] = $params['keepSlave'];
  45. }
  46. parent::__construct($params, $driver, $config, $eventManager);
  47. }
  48. /**
  49. * Checks if the connection is currently towards the primary or not.
  50. */
  51. public function isConnectedToMaster(): bool
  52. {
  53. $this->deprecated('isConnectedtoMaster()', 'isConnectedToPrimary()');
  54. return $this->isConnectedToPrimary();
  55. }
  56. /**
  57. * @param string|null $connectionName
  58. *
  59. * @return bool
  60. */
  61. public function connect($connectionName = null)
  62. {
  63. if ($connectionName === 'master') {
  64. $connectionName = 'primary';
  65. $this->deprecated('connect("master")', 'ensureConnectedToPrimary()');
  66. }
  67. if ($connectionName === 'slave') {
  68. $connectionName = 'replica';
  69. $this->deprecated('connect("slave")', 'ensureConnectedToReplica()');
  70. }
  71. return $this->performConnect($connectionName);
  72. }
  73. private function deprecated(string $thing, string $instead): void
  74. {
  75. Deprecation::trigger(
  76. 'doctrine/dbal',
  77. 'https://github.com/doctrine/dbal/pull/4054',
  78. '%s is deprecated since doctrine/dbal 2.11 and will be removed in 3.0, use %s instead.',
  79. $thing,
  80. $instead
  81. );
  82. }
  83. }