Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 30 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| WrappedMasterSlaveConnection | |
0.00% |
0 / 30 |
|
0.00% |
0 / 2 |
272 | |
0.00% |
0 / 1 |
| connect | |
0.00% |
0 / 26 |
|
0.00% |
0 / 1 |
182 | |||
| configureLogger | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2021 (original work) Open Assessment Technologies SA |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | declare(strict_types=1); |
| 23 | |
| 24 | namespace oat\generis\persistence\sql\dbal\MasterSlaveConnection; |
| 25 | |
| 26 | use Doctrine\DBAL\Connections\MasterSlaveConnection; |
| 27 | use Doctrine\DBAL\Event\ConnectionEventArgs; |
| 28 | use Doctrine\DBAL\Events; |
| 29 | use InvalidArgumentException; |
| 30 | |
| 31 | class WrappedMasterSlaveConnection extends MasterSlaveConnection |
| 32 | { |
| 33 | public function connect($connectionName = null) |
| 34 | { |
| 35 | $requestedConnectionChange = ($connectionName !== null); |
| 36 | $connectionName = $connectionName ?: 'slave'; |
| 37 | |
| 38 | if ($connectionName !== 'slave' && $connectionName !== 'master') { |
| 39 | throw new InvalidArgumentException('Invalid option to connect(), only master or slave allowed.'); |
| 40 | } |
| 41 | |
| 42 | // If we have a connection open, and this is not an explicit connection |
| 43 | // change request, then abort right here, because we are already done. |
| 44 | // This prevents writes to the slave in case of "keepSlave" option enabled. |
| 45 | if ($this->_conn !== null && ! $requestedConnectionChange) { |
| 46 | return false; |
| 47 | } |
| 48 | |
| 49 | $forceMasterAsSlave = false; |
| 50 | |
| 51 | if ($this->getTransactionNestingLevel() > 0) { |
| 52 | $connectionName = 'master'; |
| 53 | $forceMasterAsSlave = true; |
| 54 | } |
| 55 | |
| 56 | if (isset($this->connections[$connectionName])) { |
| 57 | $this->_conn = $this->connections[$connectionName]; |
| 58 | |
| 59 | if ($forceMasterAsSlave && ! $this->keepSlave) { |
| 60 | $this->connections['slave'] = $this->_conn; |
| 61 | } |
| 62 | $this->configureLogger($connectionName); |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | if ($connectionName === 'master') { |
| 67 | $this->connections['master'] = $this->_conn = $this->connectTo($connectionName); |
| 68 | |
| 69 | // Set slave connection to master to avoid invalid reads |
| 70 | if (! $this->keepSlave) { |
| 71 | $this->connections['slave'] = $this->connections['master']; |
| 72 | } |
| 73 | } else { |
| 74 | $this->connections['slave'] = $this->_conn = $this->connectTo($connectionName); |
| 75 | } |
| 76 | |
| 77 | if ($this->_eventManager->hasListeners(Events::postConnect)) { |
| 78 | $eventArgs = new ConnectionEventArgs($this); |
| 79 | $this->_eventManager->dispatchEvent(Events::postConnect, $eventArgs); |
| 80 | } |
| 81 | $this->configureLogger($connectionName); |
| 82 | return true; |
| 83 | } |
| 84 | |
| 85 | private function configureLogger($label) |
| 86 | { |
| 87 | $logger = $this->_config->getSQLLogger(); |
| 88 | |
| 89 | if ($logger && $logger instanceof MasterSlaveSqlLogger) { |
| 90 | $logger->setLabel($label); |
| 91 | $this->_config->setSQLLogger($logger); |
| 92 | } |
| 93 | } |
| 94 | } |