public static function extendEventManager(
        ContainerInterface $container,
        ?EventManagerContract $eventManager = null
    ): ?EventManagerContract {
        if ($eventManager !== null) {
            $eventManager->attach(TerminableContract::TERMINATE, static function (EventContract $event): void {
                /** @var StoreContract $driver */
                $driver = $event->getTarget()->getContainer()->get(SessionManager::class)->getDriver();

                if (! $driver->getHandler() instanceof CookieSessionHandler) {
                    $driver->save();
                }
            });
        }

        return $eventManager;
    }