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 entry in providers.entries) {
    final providerName = entry.key.toString();
    final 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(
          '[$providerName] setup in ${stopwatch.elapsedMilliseconds}ms',
        );
      }
    } catch (e, stackTrace) {
      if (shouldLog) {
        NyLogger.error('[$providerName] failed to setup: $e');
        NyLogger.error(stackTrace.toString());
      }
      rethrow;
    }
  }
  return nylo;
}