From 2e3a9cbc7cfaab745d69168978fecad26d13fe05 Mon Sep 17 00:00:00 2001 From: Patrick Stewart Date: Mon, 23 Dec 2024 08:26:38 -0700 Subject: [PATCH] update: updating 39 pass 9 fail --- .../service_container/lib/src/container.dart | 59 +++++++++---------- 1 file changed, 27 insertions(+), 32 deletions(-) diff --git a/packages/service_container/lib/src/container.dart b/packages/service_container/lib/src/container.dart index 6167a58..0ec0c79 100644 --- a/packages/service_container/lib/src/container.dart +++ b/packages/service_container/lib/src/container.dart @@ -415,12 +415,6 @@ class Container implements ContainerContract, Map { return instance; } - @override - T make(String abstract, [List? parameters]) { - var result = resolve(abstract, parameters); - return _applyExtenders(abstract, result, this) as T; - } - @override void singleton(String abstract, [dynamic concrete]) { bind(abstract, concrete ?? abstract, shared: true); @@ -442,22 +436,8 @@ class Container implements ContainerContract, Map { _instances[abstract] = closure(_instances[abstract], this); } - if (_bindings.containsKey(abstract)) { - var originalConcrete = _bindings[abstract]!['concrete']; - _bindings[abstract]!['concrete'] = (Container c) { - var result = originalConcrete is Function - ? originalConcrete(c) - : (originalConcrete ?? abstract); - return _applyExtenders(abstract, result, c); - }; - } else { - bind(abstract, (Container c) { - dynamic result = abstract; - if (c.bound(abstract) && abstract != c._getConcrete(abstract)) { - result = c.resolve(abstract); - } - return _applyExtenders(abstract, result, c); - }); + if (!_bindings.containsKey(abstract)) { + bind(abstract, (Container c) => abstract); } // Handle aliases @@ -471,20 +451,35 @@ class Container implements ContainerContract, Map { }); } + @override + T make(String abstract, [List? parameters]) { + var result = resolve(abstract, parameters); + return _applyExtenders(abstract, result, this) as T; + } + dynamic _applyExtenders(String abstract, dynamic result, Container c) { - if (_extenders.containsKey(abstract)) { - for (var extender in _extenders[abstract]!) { - result = extender(result, c); - } - } - // Apply extenders for aliases as well - _aliases.forEach((alias, target) { - if (target == abstract && _extenders.containsKey(alias)) { - for (var extender in _extenders[alias]!) { - result = extender(result, c); + var appliedExtenders = {}; + + void applyExtendersForAbstract(String key) { + if (_extenders.containsKey(key)) { + for (var extender in _extenders[key]!) { + if (!appliedExtenders.contains(extender)) { + result = extender(result, c); + appliedExtenders.add(extender); + } } } + } + + applyExtendersForAbstract(abstract); + + // Apply extenders for aliases + _aliases.forEach((alias, target) { + if (target == abstract) { + applyExtendersForAbstract(alias); + } }); + return result; }