refactor: adding arrayaccess support
This commit is contained in:
parent
81bb7bdd5e
commit
f4e567c046
2 changed files with 99 additions and 0 deletions
|
@ -1011,6 +1011,28 @@ class Container {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Operator overload for array-style access to container bindings.
|
||||||
|
///
|
||||||
|
/// This allows you to get instances from the container using array syntax:
|
||||||
|
/// ```dart
|
||||||
|
/// var logger = container[Logger];
|
||||||
|
/// ```
|
||||||
|
dynamic operator [](Type type) => make(type);
|
||||||
|
|
||||||
|
/// Operator overload for array-style binding registration.
|
||||||
|
///
|
||||||
|
/// This allows you to register bindings using array syntax:
|
||||||
|
/// ```dart
|
||||||
|
/// container[Logger] = ConsoleLogger();
|
||||||
|
/// ```
|
||||||
|
void operator []=(Type type, dynamic value) {
|
||||||
|
if (value is Function) {
|
||||||
|
registerFactory(value as dynamic Function(Container), as: type);
|
||||||
|
} else {
|
||||||
|
registerSingleton(value, as: type);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Check if we're in danger of a circular dependency.
|
/// Check if we're in danger of a circular dependency.
|
||||||
void _checkCircularDependency(Type type) {
|
void _checkCircularDependency(Type type) {
|
||||||
if (_buildStack.contains(type)) {
|
if (_buildStack.contains(type)) {
|
||||||
|
|
77
incubation/container/container/test/array_access_test.dart
Normal file
77
incubation/container/container/test/array_access_test.dart
Normal file
|
@ -0,0 +1,77 @@
|
||||||
|
import 'package:platformed_container/container.dart';
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
|
class MockReflector extends Reflector {
|
||||||
|
@override
|
||||||
|
String? getName(Symbol symbol) => null;
|
||||||
|
|
||||||
|
@override
|
||||||
|
ReflectedClass? reflectClass(Type clazz) => null;
|
||||||
|
|
||||||
|
@override
|
||||||
|
ReflectedType? reflectType(Type type) => null;
|
||||||
|
|
||||||
|
@override
|
||||||
|
ReflectedInstance? reflectInstance(Object? instance) => null;
|
||||||
|
|
||||||
|
@override
|
||||||
|
ReflectedFunction? reflectFunction(Function function) => null;
|
||||||
|
|
||||||
|
@override
|
||||||
|
ReflectedType reflectFutureOf(Type type) => throw UnimplementedError();
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract class Logger {
|
||||||
|
void log(String message);
|
||||||
|
}
|
||||||
|
|
||||||
|
class ConsoleLogger implements Logger {
|
||||||
|
@override
|
||||||
|
void log(String message) => print(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
late Container container;
|
||||||
|
|
||||||
|
setUp(() {
|
||||||
|
container = Container(MockReflector());
|
||||||
|
});
|
||||||
|
|
||||||
|
group('Array Access Tests', () {
|
||||||
|
test('can get instance using array syntax', () {
|
||||||
|
container.registerSingleton<Logger>(ConsoleLogger());
|
||||||
|
var logger = container[Logger];
|
||||||
|
expect(logger, isA<ConsoleLogger>());
|
||||||
|
});
|
||||||
|
|
||||||
|
test('can register singleton using array syntax', () {
|
||||||
|
container[Logger] = ConsoleLogger();
|
||||||
|
var logger = container.make<Logger>();
|
||||||
|
expect(logger, isA<ConsoleLogger>());
|
||||||
|
});
|
||||||
|
|
||||||
|
test('can register factory using array syntax', () {
|
||||||
|
container[Logger] = (Container c) => ConsoleLogger();
|
||||||
|
var logger = container.make<Logger>();
|
||||||
|
expect(logger, isA<ConsoleLogger>());
|
||||||
|
});
|
||||||
|
|
||||||
|
test('array access works with parameter overrides', () {
|
||||||
|
container[Logger] = (Container c) {
|
||||||
|
var level = c.getParameterOverride('level') as String? ?? 'info';
|
||||||
|
return ConsoleLogger();
|
||||||
|
};
|
||||||
|
|
||||||
|
var logger =
|
||||||
|
container.withParameters({'level': 'debug'}, () => container[Logger]);
|
||||||
|
expect(logger, isA<ConsoleLogger>());
|
||||||
|
});
|
||||||
|
|
||||||
|
test('array access works with child containers', () {
|
||||||
|
container[Logger] = ConsoleLogger();
|
||||||
|
var child = container.createChild();
|
||||||
|
var logger = child[Logger];
|
||||||
|
expect(logger, isA<ConsoleLogger>());
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
Loading…
Reference in a new issue