Skip to content

Commit

Permalink
use container 2
Browse files Browse the repository at this point in the history
  • Loading branch information
sergix44 committed Jul 2, 2023
1 parent cb28429 commit 7e0c813
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 17 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"guzzlehttp/guzzle": "^7.2",
"illuminate/macroable": "^8.0|^9.0|^10.0",
"laravel/serializable-closure": "^1.2",
"sergix44/container": "^1.0",
"sergix44/container": "^2.0",
"nutgram/hydrator": "^5.0",
"psr/log": "^1.0|^2.0|^3.0",
"psr/simple-cache": "^1.0|^2.0|^3.0"
Expand Down
27 changes: 12 additions & 15 deletions src/Nutgram.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,8 @@ private function bootstrap(string $token, Configuration $config): void
$this->config = $config;
$this->container = new Container();
if ($config->container !== null) {
$this->container->delegateTo($config->container);
$this->container->delegate($config->container);
}
$this->container->set(ContainerInterface::class, $this->container);

SerializableClosure::setSecretKey($this->token);

Expand All @@ -116,29 +115,29 @@ private function bootstrap(string $token, Configuration $config): void
]);
$botId = $config->botId ?? (int)explode(':', $this->token)[0];
$this->container->set(ClientInterface::class, $this->http);
$this->container->register(Hydrator::class, $config->hydrator)->singleton();
$this->container->register(CacheInterface::class, $config->cache)->singleton();
$this->container->register(LoggerInterface::class, $config->logger)->singleton();
$this->container->register(
$this->container->singleton(Hydrator::class, $config->hydrator);
$this->container->singleton(CacheInterface::class, $config->cache);
$this->container->singleton(LoggerInterface::class, $config->logger);
$this->container->singleton(
ConversationCache::class,
fn (ContainerInterface $c) => new ConversationCache($c->get(CacheInterface::class), $botId)
)->singleton();
$this->container->register(
);
$this->container->singleton(
GlobalCache::class,
fn (ContainerInterface $c) => new GlobalCache($c->get(CacheInterface::class), $botId)
)->singleton();
$this->container->register(
);
$this->container->singleton(
UserCache::class,
fn (ContainerInterface $c) => new UserCache($c->get(CacheInterface::class), $botId)
)->singleton();
);

$this->hydrator = $this->container->get(Hydrator::class);
$this->conversationCache = $this->container->get(ConversationCache::class);
$this->globalCache = $this->container->get(GlobalCache::class);
$this->userCache = $this->container->get(UserCache::class);
$this->logger = $this->container->get(LoggerInterface::class);

$this->container->register(RunningMode::class, Polling::class);
$this->container->singleton(RunningMode::class, Polling::class);
$this->container->set(__CLASS__, $this);
}

Expand Down Expand Up @@ -176,12 +175,10 @@ public static function fake(mixed $update = null, array $responses = [], ?Config

/**
* @param string|RunningMode $classOrInstance
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function setRunningMode(string|RunningMode $classOrInstance): void
{
$this->container->register(RunningMode::class, $classOrInstance);
$this->container->bind(RunningMode::class, $classOrInstance);
}

protected function preflight(): void
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/BotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@

it('uses a different container', function () {
$differentContainer = new Container();
$differentContainer->register(MyService::class, fn () => new MyService('hello'))->singleton();
$differentContainer->singleton(MyService::class, fn () => new MyService('hello'));

$bot = Nutgram::fake(config: new Configuration(
container: $differentContainer
Expand Down

0 comments on commit 7e0c813

Please sign in to comment.