From a4fd32581bb486989caac938e23f877cbd3c3da6 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Mon, 8 Apr 2019 19:03:35 -0400 Subject: [PATCH] Port to using angelEnv --- CHANGELOG.md | 3 +++ lib/src/core/driver.dart | 6 +++--- lib/src/core/server.dart | 19 +++++++++++++------ 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c3f2a4ca..71ccd172 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # 2.0.0-rc.0 * Log a warning when no `reflector` is provided. +* Add `AngelEnvironment` class. + * Add `Angel.environment`. + * Deprecated `app.isProduction` in favor of `app.environment.isProduction`. # 2.0.0-alpha.24 * Add `AngelEnv` class to `core`. diff --git a/lib/src/core/driver.dart b/lib/src/core/driver.dart index 5ffce8d1..e49a8ab1 100644 --- a/lib/src/core/driver.dart +++ b/lib/src/core/driver.dart @@ -113,7 +113,7 @@ abstract class Driver< } var cacheKey = req.method + path; - var tuple = app.isProduction + var tuple = app.environment.isProduction ? app.handlerCache.putIfAbsent(cacheKey, resolveTuple) : resolveTuple(); var line = tuple.item4 as MiddlewarePipeline; @@ -129,7 +129,7 @@ abstract class Driver< ..registerSingleton>(tuple.item3) ..registerSingleton(tuple.item3); - if (!app.isProduction && app.logger != null) { + if (!app.environment.isProduction && app.logger != null) { req.container .registerSingleton(new Stopwatch()..start()); } @@ -279,7 +279,7 @@ abstract class Driver< ResponseContext res, {bool ignoreFinalizers = false}) { void _cleanup(_) { - if (!app.isProduction && app.logger != null) { + if (!app.environment.isProduction && app.logger != null) { var sw = req.container.make(); app.logger.info( "${res.statusCode} ${req.method} ${req.uri} (${sw?.elapsedMilliseconds ?? 'unknown'} ms)"); diff --git a/lib/src/core/server.dart b/lib/src/core/server.dart index e71e2a09..b4d401f8 100644 --- a/lib/src/core/server.dart +++ b/lib/src/core/server.dart @@ -72,7 +72,8 @@ class Angel extends Routable { /// A set of [Controller] objects that have been loaded into the application. Map get controllers => _controllers; - /// Now *deprecated*, in favor of [AngelEnv] and [angelEnv]. + /// Now *deprecated*, in favor of [AngelEnv] and [angelEnv]. Use `app.environment.isProduction` + /// instead. /// /// Indicates whether the application is running in a production environment. /// @@ -82,7 +83,12 @@ class Angel extends Routable { /// This value is memoized the first time you call it, so do not change environment /// configuration at runtime! @deprecated - bool get isProduction => angelEnv.isProduction; + bool get isProduction => environment.isProduction; + + /// The [AngelEnvironment] in which the application is running. + /// + /// By default, it is automatically inferred. + final AngelEnvironment environment; /// Returns the parent instance of this application, if any. Angel get parent => _parent; @@ -213,14 +219,14 @@ class Angel extends Routable { String header: 'Dumping route tree:', String tab: ' ', bool showMatchers: false}) { - if (isProduction) { + if (environment.isProduction) { _flattened ??= flatten(this); _flattened.dumpTree( callback: callback, header: header?.isNotEmpty == true ? header - : (isProduction + : (environment.isProduction ? 'Dumping flattened route tree:' : 'Dumping route tree:'), tab: tab ?? ' '); @@ -229,7 +235,7 @@ class Angel extends Routable { callback: callback, header: header?.isNotEmpty == true ? header - : (isProduction + : (environment.isProduction ? 'Dumping flattened route tree:' : 'Dumping route tree:'), tab: tab ?? ' '); @@ -287,7 +293,7 @@ class Angel extends Routable { /// /// You may [force] the optimization to run, if you are not running in production. void optimizeForProduction({bool force: false}) { - if (isProduction == true || force == true) { + if (environment.isProduction || force == true) { _flattened ??= flatten(this); logger?.info('Angel is running in production mode.'); } @@ -349,6 +355,7 @@ class Angel extends Routable { Angel( {Reflector reflector: const EmptyReflector(), + this.environment: angelEnv, this.logger, this.allowMethodOverrides: true, this.serializer,