Skip to content

Commit

Permalink
getCollectionClass method and config support
Browse files Browse the repository at this point in the history
  • Loading branch information
valzargaming committed Dec 24, 2024
1 parent ad0b2d7 commit d9aada4
Showing 1 changed file with 30 additions and 11 deletions.
41 changes: 30 additions & 11 deletions src/Discord/Discord.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use Discord\Factory\Factory;
use Discord\Helpers\BigInt;
use Discord\Helpers\CacheConfig;
use Discord\Helpers\Collection;
use Discord\Helpers\CollectionInterface;
use Discord\Helpers\RegisteredCommand;
use Discord\Http\Drivers\React;
use Discord\Http\Endpoint;
Expand Down Expand Up @@ -331,6 +333,13 @@ class Discord
*/
protected $cacheConfig;

/**
* The collection class.
*
* @var string
*/
protected $collectionClass;

/**
* The Client class.
*
Expand Down Expand Up @@ -375,6 +384,10 @@ public function __construct(array $options = [])
if ($cacheConfig = $this->getCacheConfig()) {
$this->logger->warning('Attached experimental CacheInterface: '.get_class($cacheConfig->interface));
}
$this->collectionClass = $options['collection'];
if ($this->collectionClass !== Collection::class) {
$this->logger->warning("Attached experimental CollectionClass: {$this->collectionClass}");
}

$connector = new SocketConnector($options['socket_options'], $this->loop);
$this->wsFactory = new Connector($this->loop, $connector);
Expand Down Expand Up @@ -1409,6 +1422,7 @@ protected function resolveOptions(array $options = []): array
'socket_options',
'dnsConfig',
'cache',
'collection',
])
->setDefaults([
'logger' => null,
Expand All @@ -1419,6 +1433,7 @@ protected function resolveOptions(array $options = []): array
'intents' => Intents::getDefaultIntents(),
'socket_options' => [],
'cache' => [AbstractRepository::class => null], // use LegacyCacheWrapper
'collection' => [Collection::class => null],
])
->setAllowedTypes('token', 'string')
->setAllowedTypes('logger', ['null', LoggerInterface::class])
Expand All @@ -1431,17 +1446,9 @@ protected function resolveOptions(array $options = []): array
->setAllowedTypes('socket_options', 'array')
->setAllowedTypes('dnsConfig', ['string', \React\Dns\Config\Config::class])
->setAllowedTypes('cache', ['array', CacheConfig::class, \React\Cache\CacheInterface::class, \Psr\SimpleCache\CacheInterface::class])
->setNormalizer('cache', function ($options, $value) {
if (! is_array($value)) {
if (! ($value instanceof CacheConfig)) {
$value = new CacheConfig($value);
}

return [AbstractRepository::class => $value];
}

return $value;
});
->setNormalizer('cache', fn($options, $value) => is_array($value) ? $value : [AbstractRepository::class => $value instanceof CacheConfig ? $value : new CacheConfig($value)])
->setAllowedTypes('collection', ['string', 'null'])
->setAllowedValues('collection', fn($value) => $value === null || (class_exists($value) && isset(class_implements($value)[CollectionInterface::class])));

$options = $resolver->resolve($options);

Expand Down Expand Up @@ -1615,6 +1622,18 @@ public function getCacheConfig($repository_class = AbstractRepository::class)
return $this->cacheConfig[$repository_class];
}

/**
* Gets the collection configuration.
*
* @param string $collection_class Collection class name.
*
* @return string The class name of the collection, which implements CollectionInterface
*/
public function getCollectionClass()
{
return $this->collectionClass;
}

/**
* Handles dynamic get calls to the client.
*
Expand Down

0 comments on commit d9aada4

Please sign in to comment.