Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 1 | <?php |
| 2 | |
| 3 | namespace Adldap; |
| 4 | |
| 5 | use Adldap\Log\EventLogger; |
| 6 | use Adldap\Connections\Ldap; |
| 7 | use InvalidArgumentException; |
| 8 | use Adldap\Log\LogsInformation; |
| 9 | use Adldap\Connections\Provider; |
| 10 | use Adldap\Events\DispatchesEvents; |
| 11 | use Adldap\Connections\ProviderInterface; |
| 12 | use Adldap\Connections\ConnectionInterface; |
| 13 | use Adldap\Configuration\DomainConfiguration; |
| 14 | |
| 15 | class Adldap implements AdldapInterface |
| 16 | { |
| 17 | use DispatchesEvents; |
| 18 | use LogsInformation; |
| 19 | /** |
| 20 | * The default provider name. |
| 21 | * |
| 22 | * @var string |
| 23 | */ |
| 24 | protected $default = 'default'; |
| 25 | |
| 26 | /** |
| 27 | * The connection providers. |
| 28 | * |
| 29 | * @var array |
| 30 | */ |
| 31 | protected $providers = []; |
| 32 | |
| 33 | /** |
| 34 | * The events to register listeners for during initialization. |
| 35 | * |
| 36 | * @var array |
| 37 | */ |
| 38 | protected $listen = [ |
| 39 | 'Adldap\Auth\Events\*', |
| 40 | 'Adldap\Query\Events\*', |
| 41 | 'Adldap\Models\Events\*', |
| 42 | ]; |
| 43 | |
| 44 | /** |
| 45 | * {@inheritdoc} |
| 46 | */ |
| 47 | public function __construct(array $providers = []) |
| 48 | { |
| 49 | foreach ($providers as $name => $config) { |
| 50 | $this->addProvider($config, $name); |
| 51 | } |
| 52 | |
| 53 | if ($default = key($providers)) { |
| 54 | $this->setDefaultProvider($default); |
| 55 | } |
| 56 | |
| 57 | $this->initEventLogger(); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * {@inheritdoc} |
| 62 | */ |
| 63 | public function addProvider($config, $name = 'default', ConnectionInterface $connection = null) |
| 64 | { |
| 65 | if ($this->isValidConfig($config)) { |
| 66 | $config = new Provider($config, $connection ?? new Ldap($name)); |
| 67 | } |
| 68 | |
| 69 | if ($config instanceof ProviderInterface) { |
| 70 | $this->providers[$name] = $config; |
| 71 | |
| 72 | return $this; |
| 73 | } |
| 74 | |
| 75 | throw new InvalidArgumentException( |
| 76 | "You must provide a configuration array or an instance of Adldap\Connections\ProviderInterface." |
| 77 | ); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Determines if the given config is valid. |
| 82 | * |
| 83 | * @param mixed $config |
| 84 | * |
| 85 | * @return bool |
| 86 | */ |
| 87 | protected function isValidConfig($config) |
| 88 | { |
| 89 | return is_array($config) || $config instanceof DomainConfiguration; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * {@inheritdoc} |
| 94 | */ |
| 95 | public function getProviders() |
| 96 | { |
| 97 | return $this->providers; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * {@inheritdoc} |
| 102 | */ |
| 103 | public function getProvider($name) |
| 104 | { |
| 105 | if (array_key_exists($name, $this->providers)) { |
| 106 | return $this->providers[$name]; |
| 107 | } |
| 108 | |
| 109 | throw new AdldapException("The connection provider '$name' does not exist."); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * {@inheritdoc} |
| 114 | */ |
| 115 | public function setDefaultProvider($name = 'default') |
| 116 | { |
| 117 | if ($this->getProvider($name) instanceof ProviderInterface) { |
| 118 | $this->default = $name; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * {@inheritdoc} |
| 124 | */ |
| 125 | public function getDefaultProvider() |
| 126 | { |
| 127 | return $this->getProvider($this->default); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * {@inheritdoc} |
| 132 | */ |
| 133 | public function removeProvider($name) |
| 134 | { |
| 135 | unset($this->providers[$name]); |
| 136 | |
| 137 | return $this; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * {@inheritdoc} |
| 142 | */ |
| 143 | public function connect($name = null, $username = null, $password = null) |
| 144 | { |
| 145 | $provider = $name ? $this->getProvider($name) : $this->getDefaultProvider(); |
| 146 | |
| 147 | return $provider->connect($username, $password); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * {@inheritdoc} |
| 152 | */ |
| 153 | public function __call($method, $parameters) |
| 154 | { |
| 155 | $provider = $this->getDefaultProvider(); |
| 156 | |
| 157 | if (! $provider->getConnection()->isBound()) { |
| 158 | $provider->connect(); |
| 159 | } |
| 160 | |
| 161 | return call_user_func_array([$provider, $method], $parameters); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Initializes the event logger. |
| 166 | * |
| 167 | * @return void |
| 168 | */ |
| 169 | public function initEventLogger() |
| 170 | { |
| 171 | $dispatcher = static::getEventDispatcher(); |
| 172 | |
| 173 | $logger = $this->newEventLogger(); |
| 174 | |
| 175 | // We will go through each of our event wildcards and register their listener. |
| 176 | foreach ($this->listen as $event) { |
| 177 | $dispatcher->listen($event, function ($eventName, $events) use ($logger) { |
| 178 | foreach ($events as $event) { |
| 179 | $logger->log($event); |
| 180 | } |
| 181 | }); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Returns a new event logger instance. |
| 187 | * |
| 188 | * @return EventLogger |
| 189 | */ |
| 190 | protected function newEventLogger() |
| 191 | { |
| 192 | return new EventLogger(static::getLogger()); |
| 193 | } |
| 194 | } |