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