fluent-community
/
vendor
/
wpfluent
/
framework
/
src
/
WPFluent
/
Database
/
ConnectionResolver.php
ConnectionResolver.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 1.0.92, at vendor/wpfluent/framework/src/WPFluent/Database/ConnectionResolver.php
| 1 | <?php |
| 2 | |
| 3 | namespace FluentCommunity\Framework\Database; |
| 4 | |
| 5 | use FluentCommunity\Framework\Foundation\App; |
| 6 | use FluentCommunity\Framework\Database\ConnectionResolverInterface; |
| 7 | |
| 8 | class ConnectionResolver implements ConnectionResolverInterface |
| 9 | { |
| 10 | /** |
| 11 | * All of the registered connections. |
| 12 | * |
| 13 | * @var array |
| 14 | */ |
| 15 | protected $connections = []; |
| 16 | |
| 17 | /** |
| 18 | * The default connection name. |
| 19 | * |
| 20 | * @var string |
| 21 | */ |
| 22 | protected $default; |
| 23 | |
| 24 | /** |
| 25 | * Create a new connection resolver instance. |
| 26 | * |
| 27 | * @param array $connections |
| 28 | * @return void |
| 29 | */ |
| 30 | public function __construct(array $connections = []) |
| 31 | { |
| 32 | foreach ($connections as $name => $connection) { |
| 33 | $this->addConnection($name, $connection); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Get a database connection instance. |
| 39 | * |
| 40 | * @param string|null $name |
| 41 | * @return \FluentCommunity\Framework\Database\ConnectionInterface |
| 42 | */ |
| 43 | public function connection($name = null) |
| 44 | { |
| 45 | if ($name instanceof ConnectionInterface) { |
| 46 | return $name; |
| 47 | } |
| 48 | |
| 49 | if (is_null($name)) { |
| 50 | return $this->getDefaultConnection(); |
| 51 | } |
| 52 | |
| 53 | if (isset($this->connections[$name])) { |
| 54 | return $this->connections[$name]; |
| 55 | } |
| 56 | |
| 57 | return $this->getDefaultConnection(); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Add a connection to the resolver. |
| 62 | * |
| 63 | * @param string $name |
| 64 | * @param \FluentCommunity\Framework\Database\ConnectionInterface $connection |
| 65 | * @return void |
| 66 | */ |
| 67 | public function addConnection($name, ConnectionInterface $connection) |
| 68 | { |
| 69 | $this->connections[$name] = $connection; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Check if a connection has been registered. |
| 74 | * |
| 75 | * @param string $name |
| 76 | * @return bool |
| 77 | */ |
| 78 | public function hasConnection($name) |
| 79 | { |
| 80 | return isset($this->connections[$name]); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Get the default connection name. |
| 85 | * |
| 86 | * @return string |
| 87 | */ |
| 88 | public function getDefaultConnection() |
| 89 | { |
| 90 | return $this->default ?: App::getInstance('db'); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Set the default connection name. |
| 95 | * |
| 96 | * @param string $name |
| 97 | * @return void |
| 98 | */ |
| 99 | public function setDefaultConnection($name) |
| 100 | { |
| 101 | $this->default = $name; |
| 102 | } |
| 103 | } |
| 104 |