From 28411ad0ba956f16c33ab0a3902fa211c0ff2713 Mon Sep 17 00:00:00 2001 From: Chima Precious Date: Mon, 4 Nov 2024 12:08:05 +0300 Subject: [PATCH] chore: Make AppInstance available as mixing (#134) * _ * _ * _ --- packages/pharaoh/lib/src/_next/core.dart | 16 ++++++++-------- packages/pharaoh/lib/src/_next/http.dart | 6 +++--- packages/pharaoh/lib/src/core_impl.dart | 5 +++-- packages/pharaoh/pubspec.yaml | 7 ++----- 4 files changed, 16 insertions(+), 18 deletions(-) diff --git a/packages/pharaoh/lib/src/_next/core.dart b/packages/pharaoh/lib/src/_next/core.dart index 6abc4241..c857ae3a 100644 --- a/packages/pharaoh/lib/src/_next/core.dart +++ b/packages/pharaoh/lib/src/_next/core.dart @@ -24,10 +24,10 @@ part '_core/reflector.dart'; typedef RoutesResolver = List Function(); -/// This should really be a mixin but due to a bug in reflectable.dart#324 -/// TODO:(codekeyz) make this a mixin when reflectable.dart#324 is fixed -abstract class AppInstance { - Application get app => Application.instance; +mixin class AppInstance { + Application get app => Application._instance; + + AppConfig get config => app.config; } /// Use this to override the application exceptiosn handler @@ -39,7 +39,7 @@ typedef ApplicationExceptionsHandler = FutureOr Function( abstract interface class Application { Application(AppConfig config); - static late final Application instance; + static late final Application _instance; String get name; @@ -101,7 +101,7 @@ abstract class ApplicationFactory { } Future startServer() async { - final app = Application.instance as _PharaohNextImpl; + final app = Application._instance as _PharaohNextImpl; await app ._createPharaohInstance(onException: onApplicationException) @@ -110,7 +110,7 @@ abstract class ApplicationFactory { Future _bootstrapComponents(AppConfig config) async { final spanner = Spanner()..addMiddleware('/', bodyParser); - Application.instance = _PharaohNextImpl(config, spanner); + Application._instance = _PharaohNextImpl(config, spanner); final providerInstances = providers.map(createNewInstance); @@ -185,7 +185,7 @@ abstract class ApplicationFactory { @visibleForTesting Future get tester { - final app = (Application.instance as _PharaohNextImpl); + final app = (Application._instance as _PharaohNextImpl); return spookie.request( app._createPharaohInstance(onException: onApplicationException)); } diff --git a/packages/pharaoh/lib/src/_next/http.dart b/packages/pharaoh/lib/src/_next/http.dart index ccc32dd6..3387c7ab 100644 --- a/packages/pharaoh/lib/src/_next/http.dart +++ b/packages/pharaoh/lib/src/_next/http.dart @@ -9,7 +9,7 @@ import '../router/router_handler.dart'; import 'core.dart'; @inject -abstract class ClassMiddleware extends AppInstance { +abstract class ClassMiddleware with AppInstance { handle(Request req, Response res, NextFunction next) { next(); } @@ -18,7 +18,7 @@ abstract class ClassMiddleware extends AppInstance { } @inject -abstract class ServiceProvider extends AppInstance { +abstract class ServiceProvider with AppInstance { static List get defaultProviders => []; void boot() {} @@ -27,7 +27,7 @@ abstract class ServiceProvider extends AppInstance { } @inject -abstract class HTTPController extends AppInstance { +abstract class HTTPController with AppInstance { late final Request request; late final Response response; diff --git a/packages/pharaoh/lib/src/core_impl.dart b/packages/pharaoh/lib/src/core_impl.dart index 95fe7b94..5770b61c 100644 --- a/packages/pharaoh/lib/src/core_impl.dart +++ b/packages/pharaoh/lib/src/core_impl.dart @@ -57,8 +57,8 @@ class $PharaohImpl extends RouterContract ..autoCompress = true; _server.listen(handleRequest); - print( - 'Server start on PORT: ${_server.port} -> ${uri.scheme}://localhost:${_server.port}'); + stdout.writeln( + 'Server started on PORT: ${_server.port} -> ${uri.scheme}://localhost:${_server.port}'); return this; } @@ -162,6 +162,7 @@ class $PharaohImpl extends RouterContract if (!responseHeaders.containsKey(_XPoweredByHeader)) { request.response.headers.add(_XPoweredByHeader, 'Pharaoh'); } + if (!responseHeaders.containsKey(HttpHeaders.dateHeader)) { request.response.headers.add( HttpHeaders.dateHeader, diff --git a/packages/pharaoh/pubspec.yaml b/packages/pharaoh/pubspec.yaml index b78bbf7d..891efc04 100644 --- a/packages/pharaoh/pubspec.yaml +++ b/packages/pharaoh/pubspec.yaml @@ -19,8 +19,8 @@ dependencies: shelf: ^1.4.1 # framework - reflectable: 4.0.6 - get_it: ^7.6.7 + reflectable: ^4.0.12 + get_it: ^8.0.2 grammer: ^1.0.3 dotenv: ^4.2.0 ez_validator_dart: ^0.3.1 @@ -28,6 +28,3 @@ dependencies: dev_dependencies: lints: ^3.0.0 - -dependency_overrides: - analyzer: 6.4.0