5 use InvalidArgumentException
;
6 use Adldap\Connections\Provider
;
7 use Adldap\Schemas\SchemaInterface
;
8 use Adldap\Connections\ProviderInterface
;
9 use Adldap\Connections\ConnectionInterface
;
10 use Adldap\Configuration\DomainConfiguration
;
12 class Adldap
implements AdldapInterface
15 * The default provider name.
19 protected $default = 'default';
22 * The connection providers.
26 protected $providers = [];
31 public function __construct(array $providers = [])
33 foreach ($providers as $name => $config) {
34 $this->addProvider($config, $name);
37 if ($default = key($providers)) {
38 $this->setDefaultProvider($default);
45 public function addProvider($config = [], $name = 'default', ConnectionInterface
$connection = null, SchemaInterface
$schema = null)
47 if ($this->isValidConfig($config)) {
48 $config = new Provider($config, $connection, $schema);
51 if ($config instanceof ProviderInterface
) {
52 $this->providers
[$name] = $config;
57 throw new InvalidArgumentException(
58 "You must provide a configuration array or an instance of Adldap\Connections\ProviderInterface."
63 * Determines if the given config is valid.
65 * @param mixed $config
69 protected function isValidConfig($config)
71 return is_array($config) ||
$config instanceof DomainConfiguration
;
77 public function getProviders()
79 return $this->providers
;
85 public function getProvider($name)
87 if (array_key_exists($name, $this->providers
)) {
88 return $this->providers
[$name];
91 throw new AdldapException("The connection provider '$name' does not exist.");
97 public function setDefaultProvider($name = 'default')
99 if ($this->getProvider($name) instanceof ProviderInterface
) {
100 $this->default = $name;
107 public function getDefaultProvider()
109 return $this->getProvider($this->default);
115 public function removeProvider($name)
117 unset($this->providers
[$name]);
125 public function connect($name = null, $username = null, $password = null)
127 $provider = $name ?
$this->getProvider($name) : $this->getDefaultProvider();
129 return $provider->connect($username, $password);
135 public function __call($method, $parameters)
137 $provider = $this->getDefaultProvider();
139 if (!$provider->getConnection()->isBound()) {
140 // We'll make sure we have a bound connection before
141 // allowing dynamic calls on the default provider.
142 $provider->connect();
145 return call_user_func_array([$provider, $method], $parameters);