From 607fdbb57f2791e4adc65123708432f5f0ea1b4e Mon Sep 17 00:00:00 2001 From: Florian Morello Date: Fri, 9 Nov 2018 16:21:09 +0100 Subject: [PATCH] make masterConnection / slaveConnection public --- features/bootstrap/FeatureContext.php | 2 +- .../Connection/MasterSlavesConnection.php | 33 ++++++++++++++----- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php index 4df9467..01d15d7 100644 --- a/features/bootstrap/FeatureContext.php +++ b/features/bootstrap/FeatureContext.php @@ -511,7 +511,7 @@ public function tableCanBeCreatedAutomaticallyOn($tableName, $connectionName) // drop table first ?? $retryStrategy = $this->connections[$connectionName]['params']['retryStrategy']; $this->wrpdcnx($connectionName)->exec('DROP TABLE IF EXISTS ' . $tableName); - $retryStrategy->addHandler(function (Exception $exception,RetryConnection $connection) use ($tableName) { + $retryStrategy->addHandler(function (Exception $exception, RetryConnection $connection) use ($tableName) { if (strpos($exception->getMessage(), $tableName) !== false) { $connection->exec("CREATE TABLE {$tableName} (id INT)"); return true; diff --git a/src/Driver/Connection/MasterSlavesConnection.php b/src/Driver/Connection/MasterSlavesConnection.php index e9dbb70..d85ef66 100644 --- a/src/Driver/Connection/MasterSlavesConnection.php +++ b/src/Driver/Connection/MasterSlavesConnection.php @@ -55,7 +55,8 @@ public function __construct(array $master, array $slaves, $cache = null) $this->cache = $cache; } - public function disableCache() { + public function disableCache(): void + { $this->cache->disableCache(); } @@ -63,7 +64,7 @@ public function disableCache() { * @param array $slaves * @return Slave[] */ - private function filteredSlaves(array $slaves) + private function filteredSlaves(array $slaves): array { // keep slave with weight > 0 return array_filter(array_map(function ($slave) { @@ -79,7 +80,7 @@ private function filteredSlaves(array $slaves) * @return Connection * @throws \Doctrine\DBAL\DBALException */ - private function masterConnection(bool $forced = null): Connection + public function masterConnection(bool $forced = null): Connection { if ($forced !== null) { $this->forceMaster = $forced; @@ -92,7 +93,7 @@ private function masterConnection(bool $forced = null): Connection * @return Connection * @throws \Doctrine\DBAL\DBALException */ - private function slaveConnection(): ?Connection + public function slaveConnection(): ?Connection { if (empty($this->slaves)) { return $this->masterConnection(); @@ -117,17 +118,20 @@ private function slaveConnection(): ?Connection /** * @param Slave[] $slaves - * @return Slave + * @return Slave|null */ - private function randomSlave(array $slaves): Slave + private function randomSlave(array $slaves): ?Slave { $weights = []; foreach ($slaves as $slaveIndex => $slave) { - if (!$this->isSlaveOk($slave)) { + if (!$this->isSlaveOK($slave)) { continue; } $weights = array_merge($weights, array_fill(0, $slave->getWeight(), $slaveIndex)); } + if (!\count($weights)) { + return null; + } return $slaves[$weights[array_rand($weights)]]; } @@ -153,6 +157,7 @@ public function slaves(): ?array * @param string $prepareString * * @return \Doctrine\DBAL\Driver\Statement + * @throws \Doctrine\DBAL\DBALException */ public function prepare($prepareString) { @@ -186,6 +191,7 @@ public function forceMaster(bool $force): void * Executes an SQL statement, returning a result set as a Statement object. * * @return \Doctrine\DBAL\Driver\Statement + * @throws \Doctrine\DBAL\DBALException */ public function query() { @@ -196,10 +202,11 @@ public function query() /** * Quotes a string for use in a query. * - * @param string $input + * @param string $input * @param integer $type * * @return string + * @throws \Doctrine\DBAL\DBALException */ public function quote($input, $type = \PDO::PARAM_STR) { @@ -212,6 +219,7 @@ public function quote($input, $type = \PDO::PARAM_STR) * @param string $statement * * @return integer + * @throws \Doctrine\DBAL\DBALException */ public function exec($statement) { @@ -224,6 +232,7 @@ public function exec($statement) * @param string|null $name * * @return string + * @throws \Doctrine\DBAL\DBALException */ public function lastInsertId($name = null) { @@ -234,6 +243,7 @@ public function lastInsertId($name = null) * Initiates a transaction. * * @return boolean TRUE on success or FALSE on failure. + * @throws \Doctrine\DBAL\DBALException */ public function beginTransaction() { @@ -244,6 +254,7 @@ public function beginTransaction() * Commits a transaction. * * @return boolean TRUE on success or FALSE on failure. + * @throws \Doctrine\DBAL\DBALException */ public function commit() { @@ -254,6 +265,7 @@ public function commit() * Rolls back the current transaction, as initiated by beginTransaction(). * * @return boolean TRUE on success or FALSE on failure. + * @throws \Doctrine\DBAL\DBALException */ public function rollBack() { @@ -264,6 +276,7 @@ public function rollBack() * Returns the error code associated with the last operation on the database handle. * * @return string|null The error code, or null if no operation has been run on the database handle. + * @throws \Doctrine\DBAL\DBALException */ public function errorCode() { @@ -274,6 +287,7 @@ public function errorCode() * Returns extended error information associated with the last operation on the database handle. * * @return array + * @throws \Doctrine\DBAL\DBALException */ public function errorInfo() { @@ -288,7 +302,8 @@ private function getCacheKey(Slave $slave) { return 'Slave_' . md5(serialize($slave->dbalConfig())); } - public function setSlaveStatus(Slave $slave, bool $running, int $delay = null) { + public function setSlaveStatus(Slave $slave, bool $running, int $delay = null): array + { if ($this->hasCache()) { $this->cache->setCacheItem($this->getCacheKey($slave), ['running' => $running, 'delay' => $delay], $this->slaveStatusCacheTtl); }