|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace LaravelDoctrine\ORM\Configuration\Connections; |
| 4 | + |
| 5 | +use Doctrine\DBAL\Connections\MasterSlaveConnection as MasterSlaveDoctrineWrapper; |
| 6 | +use Illuminate\Contracts\Config\Repository; |
| 7 | + |
| 8 | +/** |
| 9 | + * Handles master slave connection settings. |
| 10 | + */ |
| 11 | +class MasterSlaveConnection extends Connection |
| 12 | +{ |
| 13 | + /** |
| 14 | + * @var array|Connection |
| 15 | + */ |
| 16 | + private $resolvedBaseSettings; |
| 17 | + |
| 18 | + /** |
| 19 | + * @var array Ignored configuration fields for master slave configuration. |
| 20 | + */ |
| 21 | + private $masterSlaveConfigIgnored = ['driver']; |
| 22 | + |
| 23 | + /** |
| 24 | + * MasterSlaveConnection constructor. |
| 25 | + * |
| 26 | + * @param Repository $config |
| 27 | + * @param array|Connection $resolvedBaseSettings |
| 28 | + */ |
| 29 | + public function __construct(Repository $config, $resolvedBaseSettings) |
| 30 | + { |
| 31 | + parent::__construct($config); |
| 32 | + |
| 33 | + $this->resolvedBaseSettings = $resolvedBaseSettings; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * {@inheritdoc} |
| 38 | + */ |
| 39 | + public function resolve(array $settings = []) |
| 40 | + { |
| 41 | + $driver = $this->resolvedBaseSettings['driver']; |
| 42 | + |
| 43 | + return [ |
| 44 | + 'wrapperClass' => MasterSlaveDoctrineWrapper::class, |
| 45 | + 'driver' => $driver, |
| 46 | + 'master' => $this->getConnectionData(isset($settings['write']) ? $settings['write'] : [], $driver), |
| 47 | + 'slaves' => $this->getSlavesConfig($settings['read'], $driver), |
| 48 | + ]; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Returns config for slave connections. |
| 53 | + * |
| 54 | + * @param array $slaves |
| 55 | + * @param string $driver |
| 56 | + * |
| 57 | + * @return array |
| 58 | + */ |
| 59 | + public function getSlavesConfig(array $slaves, $driver) |
| 60 | + { |
| 61 | + $handledSlaves = []; |
| 62 | + foreach ($slaves as $slave) { |
| 63 | + $handledSlaves[] = $this->getConnectionData($slave, $driver); |
| 64 | + } |
| 65 | + |
| 66 | + return $handledSlaves; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Returns single connection (slave or master) config. |
| 71 | + * |
| 72 | + * @param array $connection |
| 73 | + * @param string $driver |
| 74 | + * |
| 75 | + * @return array |
| 76 | + */ |
| 77 | + private function getConnectionData(array $connection, $driver) |
| 78 | + { |
| 79 | + $connection = $this->replaceKeyIfExists($connection, 'database', $driver === 'pdo_sqlite' ? 'path' : 'dbname'); |
| 80 | + $connection = $this->replaceKeyIfExists($connection, 'username', 'user'); |
| 81 | + |
| 82 | + return array_merge($this->getFilteredConfig(), $connection); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Returns filtered configuration to use in slaves/masters. |
| 87 | + * |
| 88 | + * @return array |
| 89 | + */ |
| 90 | + private function getFilteredConfig() |
| 91 | + { |
| 92 | + return array_diff_key($this->resolvedBaseSettings, array_flip($this->masterSlaveConfigIgnored)); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Replaces key in array if it exists. |
| 97 | + * |
| 98 | + * @param array $array |
| 99 | + * @param string $oldKey |
| 100 | + * @param string $newKey |
| 101 | + * |
| 102 | + * @return array |
| 103 | + */ |
| 104 | + private function replaceKeyIfExists(array $array, $oldKey, $newKey) |
| 105 | + { |
| 106 | + if (!isset($array[$oldKey])) { |
| 107 | + return $array; |
| 108 | + } |
| 109 | + |
| 110 | + $array[$newKey] = $array[$oldKey]; |
| 111 | + unset($array[$oldKey]); |
| 112 | + |
| 113 | + return $array; |
| 114 | + } |
| 115 | +} |
0 commit comments