Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | namespace LdapRecord; |
| 4 | |
| 5 | use BadMethodCallException; |
| 6 | use LdapRecord\Events\Dispatcher; |
| 7 | use LdapRecord\Events\DispatcherInterface; |
| 8 | use LdapRecord\Events\Logger; |
| 9 | use Psr\Log\LoggerInterface; |
| 10 | |
| 11 | class ConnectionManager |
| 12 | { |
| 13 | /** |
| 14 | * The logger instance. |
| 15 | * |
| 16 | * @var LoggerInterface|null |
| 17 | */ |
| 18 | protected $logger; |
| 19 | |
| 20 | /** |
| 21 | * The event dispatcher instance. |
| 22 | * |
| 23 | * @var DispatcherInterface|null |
| 24 | */ |
| 25 | protected $dispatcher; |
| 26 | |
| 27 | /** |
| 28 | * The added LDAP connections. |
| 29 | * |
| 30 | * @var Connection[] |
| 31 | */ |
| 32 | protected $connections = []; |
| 33 | |
| 34 | /** |
| 35 | * The name of the default connection. |
| 36 | * |
| 37 | * @var string |
| 38 | */ |
| 39 | protected $default = 'default'; |
| 40 | |
| 41 | /** |
| 42 | * The events to register listeners for during initialization. |
| 43 | * |
| 44 | * @var array |
| 45 | */ |
| 46 | protected $listen = [ |
| 47 | 'LdapRecord\Auth\Events\*', |
| 48 | 'LdapRecord\Query\Events\*', |
| 49 | 'LdapRecord\Models\Events\*', |
| 50 | ]; |
| 51 | |
| 52 | /** |
| 53 | * The method calls to proxy for compatibility. |
| 54 | * |
| 55 | * To be removed in the next major version. |
| 56 | * |
| 57 | * @var array |
| 58 | */ |
| 59 | protected $proxy = [ |
| 60 | 'reset' => 'flush', |
| 61 | 'addConnection' => 'add', |
| 62 | 'getConnection' => 'get', |
| 63 | 'allConnections' => 'all', |
| 64 | 'removeConnection' => 'remove', |
| 65 | 'getDefaultConnection' => 'getDefault', |
| 66 | 'setDefaultConnection' => 'setDefault', |
| 67 | 'getEventDispatcher' => 'dispatcher', |
| 68 | 'setEventDispatcher' => 'setDispatcher', |
| 69 | ]; |
| 70 | |
| 71 | /** |
| 72 | * Constructor. |
| 73 | * |
| 74 | * @return void |
| 75 | */ |
| 76 | public function __construct() |
| 77 | { |
| 78 | $this->dispatcher = new Dispatcher(); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Forward missing method calls onto the instance. |
| 83 | * |
| 84 | * @param string $method |
| 85 | * @param mixed $args |
| 86 | * |
| 87 | * @return mixed |
| 88 | */ |
| 89 | public function __call($method, $args) |
| 90 | { |
| 91 | $method = $this->proxy[$method] ?? $method; |
| 92 | |
| 93 | if (! method_exists($this, $method)) { |
| 94 | throw new BadMethodCallException(sprintf( |
| 95 | 'Call to undefined method %s::%s()', |
| 96 | static::class, |
| 97 | $method |
| 98 | )); |
| 99 | } |
| 100 | |
| 101 | return $this->{$method}(...$args); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Add a new connection. |
| 106 | * |
| 107 | * @param Connection $connection |
| 108 | * @param string|null $name |
| 109 | * |
| 110 | * @return $this |
| 111 | */ |
| 112 | public function add(Connection $connection, $name = null) |
| 113 | { |
| 114 | $this->connections[$name ?? $this->default] = $connection; |
| 115 | |
| 116 | if ($this->dispatcher) { |
| 117 | $connection->setDispatcher($this->dispatcher); |
| 118 | } |
| 119 | |
| 120 | return $this; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Remove a connection. |
| 125 | * |
| 126 | * @param $name |
| 127 | * |
| 128 | * @return $this |
| 129 | */ |
| 130 | public function remove($name) |
| 131 | { |
| 132 | unset($this->connections[$name]); |
| 133 | |
| 134 | return $this; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Get all of the connections. |
| 139 | * |
| 140 | * @return Connection[] |
| 141 | */ |
| 142 | public function all() |
| 143 | { |
| 144 | return $this->connections; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Get a connection by name or return the default. |
| 149 | * |
| 150 | * @param string|null $name |
| 151 | * |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 152 | * @return Connection |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 153 | * |
| 154 | * @throws ContainerException If the given connection does not exist. |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 155 | */ |
| 156 | public function get($name = null) |
| 157 | { |
| 158 | if ($this->exists($name = $name ?? $this->default)) { |
| 159 | return $this->connections[$name]; |
| 160 | } |
| 161 | |
| 162 | throw new ContainerException("The LDAP connection [$name] does not exist."); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Return the default connection. |
| 167 | * |
| 168 | * @return Connection |
| 169 | */ |
| 170 | public function getDefault() |
| 171 | { |
| 172 | return $this->get($this->default); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Get the default connection name. |
| 177 | * |
| 178 | * @return string |
| 179 | */ |
| 180 | public function getDefaultConnectionName() |
| 181 | { |
| 182 | return $this->default; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Checks if the connection exists. |
| 187 | * |
| 188 | * @param string $name |
| 189 | * |
| 190 | * @return bool |
| 191 | */ |
| 192 | public function exists($name) |
| 193 | { |
| 194 | return array_key_exists($name, $this->connections); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Set the default connection name. |
| 199 | * |
| 200 | * @param string $name |
| 201 | * |
| 202 | * @return $this |
| 203 | */ |
| 204 | public function setDefault($name = null) |
| 205 | { |
| 206 | $this->default = $name; |
| 207 | |
| 208 | return $this; |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Flush the manager of all instances and connections. |
| 213 | * |
| 214 | * @return $this |
| 215 | */ |
| 216 | public function flush() |
| 217 | { |
| 218 | $this->logger = null; |
| 219 | |
| 220 | $this->connections = []; |
| 221 | |
| 222 | $this->dispatcher = new Dispatcher(); |
| 223 | |
| 224 | return $this; |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Get the logger instance. |
| 229 | * |
| 230 | * @return LoggerInterface|null |
| 231 | */ |
| 232 | public function getLogger() |
| 233 | { |
| 234 | return $this->logger; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Set the event logger to use. |
| 239 | * |
| 240 | * @param LoggerInterface $logger |
| 241 | * |
| 242 | * @return void |
| 243 | */ |
| 244 | public function setLogger(LoggerInterface $logger) |
| 245 | { |
| 246 | $this->logger = $logger; |
| 247 | |
| 248 | $this->initEventLogger(); |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Initialize the event logger. |
| 253 | * |
| 254 | * @return void |
| 255 | */ |
| 256 | public function initEventLogger() |
| 257 | { |
| 258 | $logger = $this->newEventLogger(); |
| 259 | |
| 260 | foreach ($this->listen as $event) { |
| 261 | $this->dispatcher->listen($event, function ($eventName, $events) use ($logger) { |
| 262 | foreach ($events as $event) { |
| 263 | $logger->log($event); |
| 264 | } |
| 265 | }); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Make a new event logger instance. |
| 271 | * |
| 272 | * @return Logger |
| 273 | */ |
| 274 | protected function newEventLogger() |
| 275 | { |
| 276 | return new Logger($this->logger); |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Unset the logger instance. |
| 281 | * |
| 282 | * @return void |
| 283 | */ |
| 284 | public function unsetLogger() |
| 285 | { |
| 286 | $this->logger = null; |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * Get the event dispatcher. |
| 291 | * |
| 292 | * @return DispatcherInterface|null |
| 293 | */ |
| 294 | public function dispatcher() |
| 295 | { |
| 296 | return $this->dispatcher; |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * Set the event dispatcher. |
| 301 | * |
| 302 | * @param DispatcherInterface $dispatcher |
| 303 | * |
| 304 | * @return void |
| 305 | */ |
| 306 | public function setDispatcher(DispatcherInterface $dispatcher) |
| 307 | { |
| 308 | $this->dispatcher = $dispatcher; |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * Unset the event dispatcher. |
| 313 | * |
| 314 | * @return void |
| 315 | */ |
| 316 | public function unsetEventDispatcher() |
| 317 | { |
| 318 | $this->dispatcher = null; |
| 319 | } |
| 320 | } |