setupApplication function

Future<Nylo> setupApplication(
  1. Map<Type, NyProvider> providers, {
  2. bool? enableDebugLogging,
})

Sets up application providers.

See "bootstrap/providers" to add/modify providers.

providers - Map of provider types to provider instances. enableDebugLogging - When true, logs setup timing for each provider. Defaults to the value of APP_DEBUG environment variable.

Implementation

Future<Nylo> setupApplication(
  Map<Type, NyProvider> providers, {
  bool? enableDebugLogging,
}) async {
  Nylo nylo = Nylo();
  final bool shouldLog =
      enableDebugLogging ?? getEnv('APP_DEBUG', defaultValue: false);

  for (final MapEntry<Type, NyProvider> entry in providers.entries) {
    final providerName = entry.key.toString();
    final NyProvider provider = entry.value;

    final Stopwatch? stopwatch = shouldLog ? (Stopwatch()..start()) : null;

    try {
      Nylo? nyloObject = await provider.setup(nylo);
      if (nyloObject != null) {
        nylo = nyloObject;
      }

      if (shouldLog && stopwatch != null) {
        stopwatch.stop();
        NyLogger.debug(
          'Provider setup completed',
          context: {
            'provider': providerName,
            'duration_ms': stopwatch.elapsedMilliseconds,
          },
        );
      }
    } catch (e, stackTrace) {
      if (shouldLog) {
        NyLogger.error(
          'Provider setup failed',
          context: {'provider': providerName, 'error': e.toString()},
        );
        NyLogger.error(stackTrace.toString());
      }
      rethrow;
    }
  }
  return nylo;
}