[ 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 $read = [];
  37  
  38      /**
  39       *
  40       * A registry of ExtendedPdo "write" factories/instances.
  41       *
  42       * @var array
  43       *
  44       */
  45      protected $write = [];
  46  
  47      /**
  48       *
  49       * Constructor.
  50       *
  51       * @param callable $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          $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 null
  81       *
  82       */
  83      public function setDefault(callable $callable)
  84      {
  85          $this->default = $callable;
  86      }
  87  
  88      /**
  89       *
  90       * Returns the default connection object.
  91       *
  92       * @return ExtendedPdoInterface
  93       *
  94       */
  95      public function getDefault()
  96      {
  97          if (! $this->default instanceof ExtendedPdo) {
  98              $this->default = call_user_func($this->default);
  99          }
 100  
 101          return $this->default;
 102      }
 103  
 104      /**
 105       *
 106       * Sets a read connection factory by name.
 107       *
 108       * @param string $name The name of the connection.
 109       *
 110       * @param callable $callable The factory for the connection.
 111       *
 112       * @return null
 113       *
 114       */
 115      public function setRead($name, callable $callable)
 116      {
 117          $this->read[$name] = $callable;
 118      }
 119  
 120      /**
 121       *
 122       * Returns a read connection by name; if no name is given, picks a
 123       * random connection; if no read connections are present, returns the
 124       * default connection.
 125       *
 126       * @param string $name The read connection name to return.
 127       *
 128       * @return ExtendedPdoInterface
 129       *
 130       */
 131      public function getRead($name = '')
 132      {
 133          return $this->getConnection('read', $name);
 134      }
 135  
 136      /**
 137       *
 138       * Sets a write connection factory by name.
 139       *
 140       * @param string $name The name of the connection.
 141       *
 142       * @param callable $callable The factory for the connection.
 143       *
 144       * @return null
 145       *
 146       */
 147      public function setWrite($name, callable $callable)
 148      {
 149          $this->write[$name] = $callable;
 150      }
 151  
 152      /**
 153       *
 154       * Returns a write connection by name; if no name is given, picks a
 155       * random connection; if no write connections are present, returns the
 156       * default connection.
 157       *
 158       * @param string $name The write connection name to return.
 159       *
 160       * @return ExtendedPdoInterface
 161       *
 162       */
 163      public function getWrite($name = '')
 164      {
 165          return $this->getConnection('write', $name);
 166      }
 167  
 168      /**
 169       *
 170       * Returns a connection by name.
 171       *
 172       * @param string $type The connection type ('read' or 'write').
 173       *
 174       * @param string $name The name of the connection.
 175       *
 176       * @return ExtendedPdoInterface
 177       *
 178       * @throws Exception\ConnectionNotFound
 179       *
 180       */
 181      protected function getConnection($type, $name)
 182      {
 183          $conn = &$this->{$type};
 184  
 185          if (empty($conn)) {
 186              return $this->getDefault();
 187          }
 188  
 189          if ($name === '') {
 190              $name = array_rand($conn);
 191          }
 192  
 193          if (! isset($conn[$name])) {
 194              throw new Exception\ConnectionNotFound("{$type}:{$name}");
 195          }
 196  
 197          if (! $conn[$name] instanceof ExtendedPdo) {
 198              $conn[$name] = call_user_func($conn[$name]);
 199          }
 200  
 201          return $conn[$name];
 202      }
 203  }


Generated: Tue Jan 21 05:10:11 2025 Cross-referenced by PHPXref 0.7.1