|
| 1 | +package dev.programadorthi.routing.kodein.ext |
| 2 | + |
| 3 | +import dev.programadorthi.routing.core.application.ApplicationCall |
| 4 | +import dev.programadorthi.routing.kodein.closestDI |
| 5 | +import io.ktor.util.pipeline.PipelineContext |
| 6 | +import org.kodein.di.DI |
| 7 | +import org.kodein.di.DITrigger |
| 8 | +import org.kodein.di.LazyDelegate |
| 9 | +import org.kodein.di.On |
| 10 | +import org.kodein.di.Typed |
| 11 | +import org.kodein.di.constant |
| 12 | +import org.kodein.di.diContext |
| 13 | +import org.kodein.di.factory |
| 14 | +import org.kodein.di.factoryOrNull |
| 15 | +import org.kodein.di.instance |
| 16 | +import org.kodein.di.instanceOrNull |
| 17 | +import org.kodein.di.provider |
| 18 | +import org.kodein.di.providerOrNull |
| 19 | + |
| 20 | +//region Standard retrieving |
| 21 | + |
| 22 | +/** |
| 23 | + * Gets a factory of `T` for the given argument type, return type and tag. |
| 24 | + * |
| 25 | + * A & T generics will be preserved! |
| 26 | + * |
| 27 | + * @param A The type of argument the factory takes. |
| 28 | + * @param T The type of object the factory returns. |
| 29 | + * @param tag The bound tag, if any. |
| 30 | + * @return A factory. |
| 31 | + * @throws DI.NotFoundException if no factory was found. |
| 32 | + * @throws DI.DependencyLoopException When calling the factory function, if the instance construction triggered a dependency loop. |
| 33 | + */ |
| 34 | +public inline fun <reified A : Any, reified T : Any> PipelineContext<*, ApplicationCall>.factory(tag: Any? = null): LazyDelegate<(A) -> T> = |
| 35 | + closestDI().factory(tag = tag) |
| 36 | + |
| 37 | +/** |
| 38 | + * Gets a factory of `T` for the given argument type, return type and tag, or nul if none is found. |
| 39 | + * |
| 40 | + * A & T generics will be preserved! |
| 41 | + * |
| 42 | + * @param A The type of argument the factory takes. |
| 43 | + * @param T The type of object the factory returns. |
| 44 | + * @param tag The bound tag, if any. |
| 45 | + * @return A factory, or null if no factory was found. |
| 46 | + * @throws DI.DependencyLoopException When calling the factory function, if the instance construction triggered a dependency loop. |
| 47 | + */ |
| 48 | +public inline fun <reified A : Any, reified T : Any> PipelineContext<*, ApplicationCall>.factoryOrNull( |
| 49 | + tag: Any? = null, |
| 50 | +): LazyDelegate<((A) -> T)?> = closestDI().factoryOrNull(tag = tag) |
| 51 | + |
| 52 | +/** |
| 53 | + * Gets a provider of `T` for the given type and tag. |
| 54 | + * |
| 55 | + * T generics will be preserved! |
| 56 | + * |
| 57 | + * @param T The type of object the provider returns. |
| 58 | + * @param tag The bound tag, if any. |
| 59 | + * @return A provider. |
| 60 | + * @throws DI.NotFoundException if no provider was found. |
| 61 | + * @throws DI.DependencyLoopException When calling the provider function, if the instance construction triggered a dependency loop. |
| 62 | + */ |
| 63 | +public inline fun <reified T : Any> PipelineContext<*, ApplicationCall>.provider(tag: Any? = null): LazyDelegate<() -> T> = |
| 64 | + closestDI().provider(tag = tag) |
| 65 | + |
| 66 | +/** |
| 67 | + * Gets a provider of [T] for the given type and tag, curried from a factory that takes an argument [A]. |
| 68 | + * |
| 69 | + * A & T generics will be preserved! |
| 70 | + * |
| 71 | + * @param A The type of argument the curried factory takes. |
| 72 | + * @param T The type of object to retrieve with the returned provider. |
| 73 | + * @param tag The bound tag, if any. |
| 74 | + * @param arg The argument that will be given to the factory when curried. |
| 75 | + * @return A provider of [T]. |
| 76 | + * @throws DI.NotFoundException If no provider was found. |
| 77 | + * @throws DI.DependencyLoopException When calling the provider, if the value construction triggered a dependency loop. |
| 78 | + */ |
| 79 | +public inline fun <reified A : Any, reified T : Any> PipelineContext<*, ApplicationCall>.provider( |
| 80 | + tag: Any? = null, |
| 81 | + arg: A, |
| 82 | +): LazyDelegate<() -> T> = closestDI().provider(tag = tag, arg = arg) |
| 83 | + |
| 84 | +/** |
| 85 | + * Gets a provider of [T] for the given type and tag, curried from a factory that takes an argument [A]. |
| 86 | + * |
| 87 | + * A & T generics will be preserved! |
| 88 | + * |
| 89 | + * @param A The type of argument the curried factory takes. |
| 90 | + * @param T The type of object to retrieve with the returned provider. |
| 91 | + * @param tag The bound tag, if any. |
| 92 | + * @param arg The argument that will be given to the factory when curried. |
| 93 | + * @return A provider of [T]. |
| 94 | + * @throws DI.NotFoundException If no provider was found. |
| 95 | + * @throws DI.DependencyLoopException When calling the provider, if the value construction triggered a dependency loop. |
| 96 | + */ |
| 97 | +public inline fun <A, reified T : Any> PipelineContext<*, ApplicationCall>.provider( |
| 98 | + tag: Any? = null, |
| 99 | + arg: Typed<A>, |
| 100 | +): LazyDelegate<() -> T> = closestDI().provider(tag = tag, arg = arg) |
| 101 | + |
| 102 | +/** |
| 103 | + * Gets a provider of [T] for the given type and tag, curried from a factory that takes an argument [A]. |
| 104 | + * |
| 105 | + * A & T generics will be preserved! |
| 106 | + * |
| 107 | + * @param A The type of argument the curried factory takes. |
| 108 | + * @param T The type of object to retrieve with the returned provider. |
| 109 | + * @param tag The bound tag, if any. |
| 110 | + * @param fArg A function that returns the argument that will be given to the factory when curried. |
| 111 | + * @return A provider of [T]. |
| 112 | + * @throws DI.NotFoundException If no provider was found. |
| 113 | + * @throws DI.DependencyLoopException When calling the provider, if the value construction triggered a dependency loop. |
| 114 | + */ |
| 115 | +public inline fun <reified A : Any, reified T : Any> PipelineContext<*, ApplicationCall>.provider( |
| 116 | + tag: Any? = null, |
| 117 | + noinline fArg: () -> A, |
| 118 | +): LazyDelegate<() -> T> = closestDI().provider(tag = tag, fArg = fArg) |
| 119 | + |
| 120 | +/** |
| 121 | + * Gets a provider of `T` for the given type and tag, or null if none is found. |
| 122 | + * |
| 123 | + * T generics will be preserved! |
| 124 | + * |
| 125 | + * @param T The type of object the provider returns. |
| 126 | + * @param tag The bound tag, if any. |
| 127 | + * @return A provider, or null if no provider was found. |
| 128 | + * @throws DI.DependencyLoopException When calling the provider function, if the instance construction triggered a dependency loop. |
| 129 | + */ |
| 130 | +public inline fun <reified T : Any> PipelineContext<*, ApplicationCall>.providerOrNull(tag: Any? = null): LazyDelegate<(() -> T)?> = |
| 131 | + closestDI().providerOrNull(tag = tag) |
| 132 | + |
| 133 | +/** |
| 134 | + * Gets a provider of [T] for the given type and tag, curried from a factory that takes an argument [A], or null if none is found. |
| 135 | + * |
| 136 | + * A & T generics will be preserved! |
| 137 | + * |
| 138 | + * @param A The type of argument the curried factory takes. |
| 139 | + * @param T The type of object to retrieve with the returned provider. |
| 140 | + * @param tag The bound tag, if any. |
| 141 | + * @param arg The argument that will be given to the factory when curried. |
| 142 | + * @return A provider of [T], or null if no factory was found. |
| 143 | + * @throws DI.DependencyLoopException When calling the provider, if the value construction triggered a dependency loop. |
| 144 | + */ |
| 145 | +public inline fun <reified A : Any, reified T : Any> PipelineContext<*, ApplicationCall>.providerOrNull( |
| 146 | + tag: Any? = null, |
| 147 | + arg: A, |
| 148 | +): LazyDelegate<(() -> T)?> = closestDI().providerOrNull(tag = tag, arg = arg) |
| 149 | + |
| 150 | +/** |
| 151 | + * Gets a provider of [T] for the given type and tag, curried from a factory that takes an argument [A], or null if none is found. |
| 152 | + * |
| 153 | + * The argument type is extracted from the `Typed.type` of the argument. |
| 154 | + * |
| 155 | + * A & T generics will be preserved! |
| 156 | + * |
| 157 | + * @param A The type of argument the curried factory takes. |
| 158 | + * @param T The type of object to retrieve with the returned provider. |
| 159 | + * @param tag The bound tag, if any. |
| 160 | + * @param arg The argument that will be given to the factory when curried. |
| 161 | + * @return A provider of [T], or null if no factory was found. |
| 162 | + * @throws DI.DependencyLoopException When calling the provider, if the value construction triggered a dependency loop. |
| 163 | + */ |
| 164 | +public inline fun <A, reified T : Any> PipelineContext<*, ApplicationCall>.providerOrNull( |
| 165 | + tag: Any? = null, |
| 166 | + arg: Typed<A>, |
| 167 | +): LazyDelegate<(() -> T)?> = closestDI().providerOrNull(tag = tag, arg = arg) |
| 168 | + |
| 169 | +/** |
| 170 | + * Gets a provider of [T] for the given type and tag, curried from a factory that takes an argument [A], or null if none is found. |
| 171 | + * |
| 172 | + * A & T generics will be preserved! |
| 173 | + * |
| 174 | + * @param A The type of argument the curried factory takes. |
| 175 | + * @param T The type of object to retrieve with the returned provider. |
| 176 | + * @param tag The bound tag, if any. |
| 177 | + * @param fArg A function that returns the argument that will be given to the factory when curried. |
| 178 | + * @return A provider of [T], or null if no factory was found. |
| 179 | + * @throws DI.DependencyLoopException When calling the provider, if the value construction triggered a dependency loop. |
| 180 | + */ |
| 181 | +public inline fun <reified A : Any, reified T : Any> PipelineContext<*, ApplicationCall>.providerOrNull( |
| 182 | + tag: Any? = null, |
| 183 | + noinline fArg: () -> A, |
| 184 | +): LazyDelegate<(() -> T)?> = closestDI().providerOrNull(tag = tag, fArg = fArg) |
| 185 | + |
| 186 | +/** |
| 187 | + * Gets an instance of `T` for the given type and tag. |
| 188 | + * |
| 189 | + * T generics will be preserved! |
| 190 | + * |
| 191 | + * @param T The type of object to retrieve. |
| 192 | + * @param tag The bound tag, if any. |
| 193 | + * @return An instance. |
| 194 | + * @throws DI.NotFoundException if no provider was found. |
| 195 | + * @throws DI.DependencyLoopException If the instance construction triggered a dependency loop. |
| 196 | + */ |
| 197 | +public inline fun <reified T : Any> PipelineContext<*, ApplicationCall>.instance(tag: Any? = null): LazyDelegate<T> = |
| 198 | + closestDI().instance(tag = tag) |
| 199 | + |
| 200 | +/** |
| 201 | + * Gets an instance of [T] for the given type and tag, curried from a factory that takes an argument [A]. |
| 202 | + * |
| 203 | + * A & T generics will be preserved! |
| 204 | + * |
| 205 | + * @param A The type of argument the curried factory takes. |
| 206 | + * @param T The type of object to retrieve. |
| 207 | + * @param tag The bound tag, if any. |
| 208 | + * @param arg The argument that will be given to the factory when curried. |
| 209 | + * @return An instance of [T]. |
| 210 | + * @throws DI.NotFoundException If no provider was found. |
| 211 | + * @throws DI.DependencyLoopException If the value construction triggered a dependency loop. |
| 212 | + */ |
| 213 | +public inline fun <reified A : Any, reified T : Any> PipelineContext<*, ApplicationCall>.instance( |
| 214 | + tag: Any? = null, |
| 215 | + arg: A, |
| 216 | +): LazyDelegate<T> = closestDI().instance(tag = tag, arg = arg) |
| 217 | + |
| 218 | +/** |
| 219 | + * Gets an instance of [T] for the given type and tag, curried from a factory that takes an argument [A]. |
| 220 | + * |
| 221 | + * The argument type is extracted from the `Typed.type` of the argument. |
| 222 | + * |
| 223 | + * A & T generics will be erased! |
| 224 | + * |
| 225 | + * @param A The type of argument the curried factory takes. |
| 226 | + * @param T The type of object to retrieve. |
| 227 | + * @param tag The bound tag, if any. |
| 228 | + * @param arg The argument that will be given to the factory when curried. |
| 229 | + * @return An instance of [T]. |
| 230 | + * @throws DI.NotFoundException If no provider was found. |
| 231 | + * @throws DI.DependencyLoopException If the value construction triggered a dependency loop. |
| 232 | + */ |
| 233 | +public inline fun <A, reified T : Any> PipelineContext<*, ApplicationCall>.instance( |
| 234 | + tag: Any? = null, |
| 235 | + arg: Typed<A>, |
| 236 | +): LazyDelegate<T> = closestDI().instance(tag = tag, arg = arg) |
| 237 | + |
| 238 | +/** |
| 239 | + * Gets an instance of [T] for the given type and tag, curried from a factory that takes an argument [A]. |
| 240 | + * |
| 241 | + * A & T generics will be erased! |
| 242 | + * |
| 243 | + * @param A The type of argument the curried factory takes. |
| 244 | + * @param T The type of object to retrieve. |
| 245 | + * @param tag The bound tag, if any. |
| 246 | + * @param fArg A function that returns the argument that will be given to the factory when curried. |
| 247 | + * @return An instance of [T]. |
| 248 | + * @throws DI.NotFoundException If no provider was found. |
| 249 | + * @throws DI.DependencyLoopException If the value construction triggered a dependency loop. |
| 250 | + */ |
| 251 | +public inline fun <reified A : Any, reified T : Any> PipelineContext<*, ApplicationCall>.instance( |
| 252 | + tag: Any? = null, |
| 253 | + noinline fArg: () -> A, |
| 254 | +): LazyDelegate<T> = closestDI().instance(tag = tag, fArg = fArg) |
| 255 | + |
| 256 | +/** |
| 257 | + * Gets an instance of `T` for the given type and tag, or null if none is found. |
| 258 | + * |
| 259 | + * T generics will be erased! |
| 260 | + * |
| 261 | + * @param T The type of object to retrieve. |
| 262 | + * @param tag The bound tag, if any. |
| 263 | + * @return An instance, or null if no provider was found. |
| 264 | + * @throws DI.DependencyLoopException If the instance construction triggered a dependency loop. |
| 265 | + */ |
| 266 | +public inline fun <reified T : Any> PipelineContext<*, ApplicationCall>.instanceOrNull(tag: Any? = null): LazyDelegate<T?> = |
| 267 | + closestDI().instanceOrNull(tag = tag) |
| 268 | + |
| 269 | +/** |
| 270 | + * Gets an instance of [T] for the given type and tag, curried from a factory that takes an argument [A], or null if none is found. |
| 271 | + * |
| 272 | + * A & T generics will be erased! |
| 273 | + * |
| 274 | + * @param A The type of argument the curried factory takes. |
| 275 | + * @param T The type of object to retrieve. |
| 276 | + * @param tag The bound tag, if any. |
| 277 | + * @param arg The argument that will be given to the factory when curried. |
| 278 | + * @return An instance of [T], or null if no factory was found. |
| 279 | + * @throws DI.DependencyLoopException If the value construction triggered a dependency loop. |
| 280 | + */ |
| 281 | +public inline fun <reified A : Any, reified T : Any> PipelineContext<*, ApplicationCall>.instanceOrNull( |
| 282 | + tag: Any? = null, |
| 283 | + arg: A, |
| 284 | +): LazyDelegate<T?> = closestDI().instanceOrNull(tag = tag, arg = arg) |
| 285 | + |
| 286 | +/** |
| 287 | + * Gets an instance of [T] for the given type and tag, curried from a factory that takes an argument [A], or null if none is found. |
| 288 | + * |
| 289 | + * The argument type is extracted from the `Typed.type` of the argument. |
| 290 | + * |
| 291 | + * A & T generics will be erased! |
| 292 | + * |
| 293 | + * @param A The type of argument the curried factory takes. |
| 294 | + * @param T The type of object to retrieve. |
| 295 | + * @param tag The bound tag, if any. |
| 296 | + * @param arg The argument that will be given to the factory when curried. |
| 297 | + * @return An instance of [T], or null if no factory was found. |
| 298 | + * @throws DI.DependencyLoopException If the value construction triggered a dependency loop. |
| 299 | + */ |
| 300 | +public inline fun <A, reified T : Any> PipelineContext<*, ApplicationCall>.instanceOrNull( |
| 301 | + tag: Any? = null, |
| 302 | + arg: Typed<A>, |
| 303 | +): LazyDelegate<T?> = closestDI().instanceOrNull(tag = tag, arg = arg) |
| 304 | + |
| 305 | +/** |
| 306 | + * Gets an instance of [T] for the given type and tag, curried from a factory that takes an argument [A], or null if none is found. |
| 307 | + * |
| 308 | + * A & T generics will be erased! |
| 309 | + * |
| 310 | + * @param A The type of argument the curried factory takes. |
| 311 | + * @param T The type of object to retrieve. |
| 312 | + * @param tag The bound tag, if any. |
| 313 | + * @param fArg A function that returns the argument that will be given to the factory when curried. |
| 314 | + * @return An instance of [T], or null if no factory was found. |
| 315 | + * @throws DI.DependencyLoopException If the value construction triggered a dependency loop. |
| 316 | + */ |
| 317 | +public inline fun <reified A : Any, reified T : Any> PipelineContext<*, ApplicationCall>.instanceOrNull( |
| 318 | + tag: Any? = null, |
| 319 | + noinline fArg: () -> A, |
| 320 | +): LazyDelegate<T?> = closestDI().instanceOrNull(tag = tag, fArg = fArg) |
| 321 | + |
| 322 | +/** |
| 323 | + * Allows to create a new DI object with a context and/or a trigger set. |
| 324 | + * |
| 325 | + * @param context The new context of the new DI. |
| 326 | + * @param trigger The new trigger of the new DI. |
| 327 | + * @return A DI object that uses the same container as this one, but with its context and/or trigger changed. |
| 328 | + */ |
| 329 | +public inline fun <reified C : Any> PipelineContext<*, ApplicationCall>.on( |
| 330 | + context: C, |
| 331 | + trigger: DITrigger? = closestDI().diTrigger, |
| 332 | +): DI = closestDI().On(diContext(context), trigger) |
| 333 | + |
| 334 | +/** |
| 335 | + * Allows to create a new DI object with a context and/or a trigger set. |
| 336 | + * |
| 337 | + * @param getContext A function that gets the new context of the new DI. |
| 338 | + * @param trigger The new trigger of the new DI. |
| 339 | + * @return A DI object that uses the same container as this one, but with its context and/or trigger changed. |
| 340 | + */ |
| 341 | +public inline fun <reified C : Any> PipelineContext<*, ApplicationCall>.on( |
| 342 | + trigger: DITrigger? = closestDI().diTrigger, |
| 343 | + crossinline getContext: () -> C, |
| 344 | +): DI = closestDI().On(diContext(getContext), trigger) |
| 345 | + |
| 346 | +/** |
| 347 | + * Allows to create a new DI object with a trigger set. |
| 348 | + * |
| 349 | + * @param trigger The new trigger of the new DI. |
| 350 | + * @return A DI object that uses the same container as this one, but with its context and/or trigger changed. |
| 351 | + */ |
| 352 | +public fun PipelineContext<*, ApplicationCall>.on(trigger: DITrigger?): DI { |
| 353 | + val di = closestDI() |
| 354 | + return di.On(di.diContext, trigger) |
| 355 | +} |
| 356 | +//endregion |
| 357 | + |
| 358 | +/** |
| 359 | + * Gets a constant of type [T] and tag whose tag is the name of the receiving property. |
| 360 | + * |
| 361 | + * T generics will be erased! |
| 362 | + * |
| 363 | + * @param T The type of object to retrieve. |
| 364 | + * @return An instance of [T]. |
| 365 | + * @throws DI.NotFoundException If no provider was found. |
| 366 | + * @throws DI.DependencyLoopException If the value construction triggered a dependency loop. |
| 367 | + */ |
| 368 | +public inline fun <reified T : Any> PipelineContext<*, ApplicationCall>.constant(): LazyDelegate<T> = closestDI().constant() |
| 369 | +//endregion |
0 commit comments