resolveInjection function

Future resolveInjection(
  1. dynamic requirement,
  2. InjectionRequest injection,
  3. RequestContext req,
  4. ResponseContext res,
  5. bool throwOnUnresolved, [
  6. Container? container,
])

Implementation

Future resolveInjection(requirement, InjectionRequest injection,
    RequestContext req, ResponseContext res, bool throwOnUnresolved,
    [Container? container]) async {
  dynamic propFromApp;
  container ??= req.container ?? res.app!.container;

  if (requirement == RequestContext) {
    return req;
  } else if (requirement == ResponseContext) {
    return res;
  } else if (requirement is String &&
      injection.parameters.containsKey(requirement)) {
    var param = injection.parameters[requirement]!;
    var value = param.getValue(req);
    if (value == null && param.required != false) throw param.error as Object;
    return value;
  } else if (requirement is String) {
    if (req.container!.hasNamed(requirement)) {
      return req.container!.findByName(requirement);
    }
    if (req.params.containsKey(requirement)) {
      return req.params[requirement];
    } else if ((propFromApp = req.app!.findProperty(requirement)) != null) {
      return propFromApp;
    } else if (injection.optional.contains(requirement)) {
      return null;
    } else if (throwOnUnresolved) {
      throw ArgumentError(
          "Cannot resolve parameter '$requirement' within handler.");
    }
  } else if (requirement is List &&
      requirement.length == 2 &&
      requirement.first is String &&
      requirement.last is Type) {
    var key = requirement.first;
    var type = requirement.last;
    if (req.params.containsKey(key) ||
        req.app!.configuration.containsKey(key) ||
        _primitiveTypes.contains(type)) {
      return await resolveInjection(
          key, injection, req, res, throwOnUnresolved, container);
    } else {
      return await resolveInjection(
          type, injection, req, res, throwOnUnresolved, container);
    }
  } else if (requirement is Type && requirement != dynamic) {
    try {
      var futureType = container.reflector.reflectFutureOf(requirement);
      if (container.has(futureType.reflectedType)) {
        return await container.make(futureType.reflectedType);
      }
    } on UnsupportedError {
      // Ignore.
    }

    return await container.make(requirement);
  } else if (throwOnUnresolved) {
    throw ArgumentError(
        '$requirement cannot be injected into a request handler.');
  }
}