123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- <?php
- declare(strict_types=1);
- /**
- * This file is part of Hyperf.
- *
- * @link https://www.hyperf.io
- * @document https://hyperf.wiki
- * @contact group@hyperf.io
- * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
- */
- namespace Hyperf\Database;
- class ConnectionResolver implements ConnectionResolverInterface
- {
- /**
- * All the registered connections.
- */
- protected array $connections = [];
- /**
- * The default connection name.
- */
- protected string $default = 'default';
- /**
- * Create a new connection resolver instance.
- */
- public function __construct(array $connections = [])
- {
- foreach ($connections as $name => $connection) {
- $this->addConnection($name, $connection);
- }
- }
- /**
- * Get a database connection instance.
- */
- public function connection(?string $name = null): ConnectionInterface
- {
- if (is_null($name)) {
- $name = $this->getDefaultConnection();
- }
- return $this->connections[$name];
- }
- /**
- * Add a connection to the resolver.
- *
- * @param string $name
- */
- public function addConnection($name, ConnectionInterface $connection)
- {
- $this->connections[$name] = $connection;
- }
- /**
- * Check if a connection has been registered.
- *
- * @param string $name
- */
- public function hasConnection($name): bool
- {
- return isset($this->connections[$name]);
- }
- /**
- * Get the default connection name.
- */
- public function getDefaultConnection(): string
- {
- return $this->default;
- }
- /**
- * Set the default connection name.
- */
- public function setDefaultConnection(string $name): void
- {
- $this->default = $name;
- }
- }
|