add: adding support capsule_manager
This commit is contained in:
parent
ab9ebde517
commit
0819c9c8cd
2 changed files with 92 additions and 0 deletions
51
packages/support/lib/src/traits/capsule_manager.dart
Normal file
51
packages/support/lib/src/traits/capsule_manager.dart
Normal file
|
@ -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<Fluent>()) {
|
||||
_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;
|
||||
}
|
41
packages/support/test/traits/capsule_manager_test.dart
Normal file
41
packages/support/test/traits/capsule_manager_test.dart
Normal file
|
@ -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<Fluent>(), isA<Fluent>());
|
||||
});
|
||||
|
||||
test('setupContainer preserves existing config if bound', () {
|
||||
var config = Fluent();
|
||||
container.registerSingleton(config, as: Fluent);
|
||||
capsule.setupContainer(container);
|
||||
expect(capsule.getContainer()!.make<Fluent>(), same(config));
|
||||
});
|
||||
|
||||
test('can set as global instance', () {
|
||||
capsule.setAsGlobal();
|
||||
expect(CapsuleManager.getInstance(), same(capsule));
|
||||
});
|
||||
});
|
||||
}
|
Loading…
Reference in a new issue