diff --git a/src/Discord/Discord.php b/src/Discord/Discord.php index 30782df04..7a6360cbc 100644 --- a/src/Discord/Discord.php +++ b/src/Discord/Discord.php @@ -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; @@ -331,6 +333,13 @@ class Discord */ protected $cacheConfig; + /** + * The collection class. + * + * @var string + */ + protected $collectionClass; + /** * The Client class. * @@ -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); @@ -1409,6 +1422,7 @@ protected function resolveOptions(array $options = []): array 'socket_options', 'dnsConfig', 'cache', + 'collection', ]) ->setDefaults([ 'logger' => null, @@ -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]) @@ -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); @@ -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. *