Repository: InsertKoinIO/koin
File: projects/compose/koin-compose-navigation3/src/commonMain/kotlin/org/koin/compose/navigation3/EntryProvider.kt (Kotlin)

Code before:
import androidx.compose.runtime.Composable
import androidx.navigation3.runtime.NavEntry
import androidx.navigation3.runtime.entryProvider
import org.koin.compose.LocalKoinScopeContext
import org.koin.core.annotation.KoinExperimentalAPI
import org.koin.core.annotation.KoinInternalApi
import org.koin.core.scope.Scope

/**
 * Type alias for a function that provides navigation entries based on a route/destination.
 *
 * An [EntryProvider] takes any route object and returns the corresponding [NavEntry] that
 * contains the composable content for that destination. This enables type-safe navigation
 * with Koin dependency injection.
 *
 * @see EntryProviderInstaller for defining navigation entries in Koin modules
 */
@KoinExperimentalAPI
typealias EntryProvider = (Any) -> NavEntry<Any>

@KoinExperimentalAPI
internal fun Scope.getEntryProvider() : EntryProvider {
    val entries = getAll<EntryProviderInstaller>()
    val entryProvider: (Any) -> NavEntry<Any> = entryProvider {
        entries.forEach { builder -> this.builder() }
    }
    return entryProvider
}

/**
 * Composable function that retrieves an [EntryProvider] from the current or specified Koin scope.
 *
 * This function collects all registered [EntryProviderInstaller] instances from the Koin scope
 * and aggregates them into a single [EntryProvider] that can be used with Navigation 3.
 * By default, it uses the scope from [LocalKoinScopeContext], but a custom scope can be provided.
 *
 * Example usage:
 * ```kotlin
 * @Composable
 * fun MyApp() {
 *     val entryProvider = koinEntryProvider()
 *     NavigationHost(entryProvider) {
 *         // Your navigation setup
 *     }
 * }
 * ```
 *
 * @param scope The Koin scope to retrieve navigation entries from. Defaults to [LocalKoinScopeContext.current].
 * @return An [EntryProvider] that combines all registered navigation entries from the scope
 *
 * @see EntryProvider for the navigation entry provider type

Review comment (suggestion):
The unsafe cast to `EntryProviderScope<Any>` is necessary due to the design where `EntryProviderInstaller` is hardcoded to `EntryProviderScope<Any>`. While this works because the navigation3 library's type-safe `entry<T>` calls handle type matching at registration time, it means that the generic type parameter `T` on `koinEntryProvider<T>()` doesn't actually provide compile-time type safety - it only affects the return type signature.

This could be misleading to API consumers who might expect that calling `koinEntryProvider<MyNavKey>()` would restrict entries to only those registered for `MyNavKey`. Consider adding a KDoc note explaining that `T` is a convenience type parameter for the caller's type system and doesn't enforce which entries are included at runtime.

Code after:
import androidx.compose.runtime.Composable
import androidx.navigation3.runtime.EntryProviderScope
import androidx.navigation3.runtime.NavEntry
import androidx.navigation3.runtime.entryProvider
import org.koin.compose.LocalKoinScopeContext
import org.koin.core.annotation.KoinExperimentalAPI
import org.koin.core.annotation.KoinInternalApi
import org.koin.core.scope.Scope

/**
 * Type alias for a function that provides navigation entries based on a route/destination.
 *
 * An [EntryProvider] takes any route object and returns the corresponding [NavEntry] that
 * contains the composable content for that destination. This enables type-safe navigation
 * with Koin dependency injection.
 *
 * @see EntryProviderInstaller for defining navigation entries in Koin modules
 */
@KoinExperimentalAPI
typealias EntryProvider<T> = (T) -> NavEntry<T>

@KoinExperimentalAPI
internal fun <T : Any> Scope.getEntryProvider() : EntryProvider<T> {
    val entries = getAll<EntryProviderInstaller>()
    val entryProvider: EntryProvider<T> = entryProvider {
        entries.forEach { builder -> (this as EntryProviderScope<Any>).builder() }
    }
    return entryProvider
}

/**
 * Composable function that retrieves an [EntryProvider] from the current or specified Koin scope.
 *
 * This function collects all registered [EntryProviderInstaller] instances from the Koin scope
 * and aggregates them into a single [EntryProvider] that can be used with Navigation 3.
 * By default, it uses the scope from [LocalKoinScopeContext], but a custom scope can be provided.
 *
 * Example usage:
 * ```kotlin
 * @Composable
 * fun MyApp() {
 *     val entryProvider = koinEntryProvider()
 *     NavigationHost(entryProvider) {
 *         // Your navigation setup
 *     }
 * }
 * ```
 *
 * @param scope The Koin scope to retrieve navigation entries from. Defaults to [LocalKoinScopeContext.current].
 * @return An [EntryProvider] that combines all registered navigation entries from the scope
 *