[ Index ]

PHP Cross Reference of YOURLS

title

Body

[close]

/includes/vendor/aura/sql/src/ -> ConnectionLocator.php (source)

   1  <?php
   2  /**
   3   *
   4   * This file is part of Aura for PHP.
   5   *
   6   * @license https://opensource.org/licenses/MIT MIT
   7   *
   8   */
   9  namespace Aura\Sql;
  10  
  11  /**
  12   *
  13   * Manages ExtendedPdo instances for default, read, and write connections.
  14   *
  15   * @package Aura.Sql
  16   *
  17   */
  18  class ConnectionLocator implements ConnectionLocatorInterface
  19  {
  20      /**
  21       *
  22       * A default ExtendedPdo connection factory/instance.
  23       *
  24       * @var callable
  25       *
  26       */
  27      protected $default;
  28  
  29      /**
  30       *
  31       * A registry of ExtendedPdo "read" factories/instances.
  32       *
  33       * @var array
  34       *
  35       */
  36      protected array $read = [];
  37  
  38      /**
  39       *
  40       * A registry of ExtendedPdo "write" factories/instances.
  41       *
  42       * @var array
  43       *
  44       */
  45      protected array $write = [];
  46  
  47      /**
  48       *
  49       * Constructor.
  50       *
  51       * @param callable|null $default A callable to create a default connection.
  52       *
  53       * @param array $read An array of callables to create read connections.
  54       *
  55       * @param array $write An array of callables to create write connections.
  56       *
  57       */
  58      public function __construct(
  59          ?callable $default = null,
  60          array $read = [],
  61          array $write = []
  62      ) {
  63          if ($default) {
  64              $this->setDefault($default);
  65          }
  66          foreach ($read as $name => $callable) {
  67              $this->setRead($name, $callable);
  68          }
  69          foreach ($write as $name => $callable) {
  70              $this->setWrite($name, $callable);
  71          }
  72      }
  73  
  74      /**
  75       *
  76       * Sets the default connection factory.
  77       *
  78       * @param callable $callable The factory for the connection.
  79       *
  80       * @return void
  81       */
  82      public function setDefault(callable $callable): void
  83      {
  84          $this->default = $callable;
  85      }
  86  
  87      /**
  88       *
  89       * Returns the default connection object.
  90       *
  91       * @return ExtendedPdoInterface
  92       *
  93       * @throws Exception\ConnectionNotFound
  94       */
  95      public function getDefault(): ExtendedPdoInterface
  96      {
  97          if (! $this->default) {
  98              throw new Exception\ConnectionNotFound("default");
  99          }
 100  
 101          if (! $this->default instanceof ExtendedPdo) {
 102              $this->default = call_user_func($this->default);
 103          }
 104  
 105          return $this->default;
 106      }
 107  
 108      /**
 109       *
 110       * Sets a read connection factory by name.
 111       *
 112       * @param string $name The name of the connection.
 113       *
 114       * @param callable $callable The factory for the connection.
 115       *
 116       * @return void
 117       */
 118      public function setRead(string $name, callable $callable): void
 119      {
 120          $this->read[$name] = $callable;
 121      }
 122  
 123      /**
 124       *
 125       * Returns a read connection by name; if no name is given, picks a
 126       * random connection; if no read connections are present, returns the
 127       * default connection.
 128       *
 129       * @param string $name The read connection name to return.
 130       *
 131       * @return ExtendedPdoInterface
 132       *
 133       * @throws \Aura\Sql\Exception\ConnectionNotFound
 134       */
 135      public function getRead(string $name = ''): ExtendedPdoInterface
 136      {
 137          return $this->getConnection('read', $name);
 138      }
 139  
 140      /**
 141       *
 142       * Sets a write connection factory by name.
 143       *
 144       * @param string $name The name of the connection.
 145       *
 146       * @param callable $callable The factory for the connection.
 147       *
 148       * @return void
 149       */
 150      public function setWrite(string $name, callable $callable): void
 151      {
 152          $this->write[$name] = $callable;
 153      }
 154  
 155      /**
 156       *
 157       * Returns a write connection by name; if no name is given, picks a
 158       * random connection; if no write connections are present, returns the
 159       * default connection.
 160       *
 161       * @param string $name The write connection name to return.
 162       *
 163       * @return ExtendedPdoInterface
 164       *
 165       * @throws \Aura\Sql\Exception\ConnectionNotFound
 166       */
 167      public function getWrite(string $name = ''): ExtendedPdoInterface
 168      {
 169          return $this->getConnection('write', $name);
 170      }
 171  
 172      /**
 173       *
 174       * Returns a connection by name.
 175       *
 176       * @param string $type The connection type ('read' or 'write').
 177       *
 178       * @param string $name The name of the connection.
 179       *
 180       * @return ExtendedPdoInterface
 181       *
 182       * @throws Exception\ConnectionNotFound
 183       *
 184       */
 185      protected function getConnection(string $type, string $name): ExtendedPdoInterface
 186      {
 187          $conn = &$this->{$type};
 188  
 189          if (empty($conn)) {
 190              return $this->getDefault();
 191          }
 192  
 193          if ($name === '') {
 194              $name = array_rand($conn);
 195          }
 196  
 197          if (! isset($conn[$name])) {
 198              throw new Exception\ConnectionNotFound("$type:$name");
 199          }
 200  
 201          if (! $conn[$name] instanceof ExtendedPdo) {
 202              $conn[$name] = call_user_func($conn[$name]);
 203          }
 204  
 205          return $conn[$name];
 206      }
 207  }


Generated: Tue Apr 1 05:10:01 2025 Cross-referenced by PHPXref 0.7.1