Final Class Yiisoft\Yii\AuthClient\Collection
| Inheritance | Yiisoft\ |
|---|
Collection is a storage for all auth clients in the application.
Example application configuration:
'yiisoft/yii-auth-client' => [
'clients' => [
'google' => [
'class' => Yiisoft\Yii\AuthClient\Client\Google::class,
'clientId' => $_ENV['GOOGLE_CLIENT_ID'],
'clientSecret' => $_ENV['GOOGLE_CLIENT_SECRET'],
'oauth2ReturnUrl' => 'https://example.com/auth/google',
],
'facebook' => [
'class' => Yiisoft\Yii\AuthClient\Client\Facebook::class,
'clientId' => $_ENV['FACEBOOK_CLIENT_ID'],
'clientSecret' => $_ENV['FACEBOOK_CLIENT_SECRET'],
],
],
]
The array key ('google', 'facebook') is used as the client's name.
Remaining keys map to setters on the client class, e.g. clientId → setClientId().
See also \
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\ |
|
| getClient() | Yiisoft\ |
|
| getClients() | Yiisoft\ |
|
| hasClient() | Checks if client exists in the hub. | Yiisoft\ |
| setClients() | Yiisoft\ |
Method Details
| public mixed __construct ( array $clients ) | ||
| $clients | array | |
public function __construct(
/**
* @var array|OAuth2Interface[] list of OAuth2 clients with their configuration in format: 'clientName' => [...]
*/
private array $clients,
) {}
| public Yiisoft\ | ||
| $name | string | |
public function getClient(string $name): OAuth2Interface
{
if (!$this->hasClient($name)) {
throw new InvalidArgumentException("Unknown auth client '{$name}'.");
}
$client = $this->clients[$name];
if (!($client instanceof OAuth2Interface)) {
throw new RuntimeException(
'The Client should be an OAuth2 Interface.',
);
}
return $client;
}
| public array getClients ( ) |
public function getClients(): array
{
$clients = [];
/**
* @var OAuth2Interface $client
* @var string $name
* @var array $this->clients
*/
foreach ($this->clients as $name => $client) {
$clients[$name] = $this->getClient($name);
}
return $clients;
}
Checks if client exists in the hub.
| public boolean hasClient ( string $name ) | ||
| $name | string |
Client id. |
| return | boolean |
Whether client exist. |
|---|---|---|
public function hasClient(string $name): bool
{
return array_key_exists($name, $this->clients);
}
| public void setClients ( array $clients ) | ||
| $clients | array |
List of auth clients indexed by their names |
public function setClients(array $clients): void
{
$this->clients = $clients;
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.