From 0819c9c8cda693f717fd257b4d41a1502eb4973d Mon Sep 17 00:00:00 2001 From: Patrick Stewart Date: Mon, 30 Dec 2024 02:25:27 -0700 Subject: [PATCH] add: adding support capsule_manager --- .../lib/src/traits/capsule_manager.dart | 51 +++++++++++++++++++ .../test/traits/capsule_manager_test.dart | 41 +++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 packages/support/lib/src/traits/capsule_manager.dart create mode 100644 packages/support/test/traits/capsule_manager_test.dart diff --git a/packages/support/lib/src/traits/capsule_manager.dart b/packages/support/lib/src/traits/capsule_manager.dart new file mode 100644 index 0000000..de4be94 --- /dev/null +++ b/packages/support/lib/src/traits/capsule_manager.dart @@ -0,0 +1,51 @@ +import 'package:platform_container/container.dart'; +import 'package:platform_support/src/fluent.dart'; + +/// A mixin that provides capsule management functionality. +/// +/// This mixin allows classes to manage a global instance and a container instance, +/// similar to Laravel's CapsuleManagerTrait. +mixin CapsuleManager { + /// The current globally used instance. + static dynamic _instance; + + /// The container instance. + Container? _container; + + /// Setup the IoC container instance. + /// + /// This method initializes the container and ensures it has a config binding. + /// If no config binding exists, it creates one with an empty [Fluent] instance. + void setupContainer(Container container) { + _container = container; + + if (!_container!.has()) { + _container!.registerSingleton(Fluent(), as: Fluent); + } + } + + /// Make this capsule instance available globally. + /// + /// This method sets the current instance as the global instance that can be + /// accessed throughout the application. + void setAsGlobal() { + _instance = this; + } + + /// Get the IoC container instance. + /// + /// Returns the current container instance used by this capsule. + Container? getContainer() => _container; + + /// Set the IoC container instance. + /// + /// This method allows changing the container instance used by this capsule. + void setContainer(Container container) { + _container = container; + } + + /// Get the current globally used instance. + /// + /// Returns the current global instance of this capsule. + static dynamic getInstance() => _instance; +} diff --git a/packages/support/test/traits/capsule_manager_test.dart b/packages/support/test/traits/capsule_manager_test.dart new file mode 100644 index 0000000..85070bf --- /dev/null +++ b/packages/support/test/traits/capsule_manager_test.dart @@ -0,0 +1,41 @@ +import 'package:platform_container/container.dart'; +import 'package:platform_container/mirrors.dart'; +import 'package:platform_support/src/fluent.dart'; +import 'package:platform_support/src/traits/capsule_manager.dart'; +import 'package:test/test.dart'; + +class TestCapsule with CapsuleManager {} + +void main() { + group('CapsuleManager', () { + late Container container; + late TestCapsule capsule; + + setUp(() { + container = Container(MirrorsReflector()); + capsule = TestCapsule(); + }); + + test('can set and get container', () { + capsule.setContainer(container); + expect(capsule.getContainer(), equals(container)); + }); + + test('setupContainer initializes config if not bound', () { + capsule.setupContainer(container); + expect(capsule.getContainer()!.make(), isA()); + }); + + test('setupContainer preserves existing config if bound', () { + var config = Fluent(); + container.registerSingleton(config, as: Fluent); + capsule.setupContainer(container); + expect(capsule.getContainer()!.make(), same(config)); + }); + + test('can set as global instance', () { + capsule.setAsGlobal(); + expect(CapsuleManager.getInstance(), same(capsule)); + }); + }); +}