|
1
|
|
|
""" |
|
2
|
|
|
Module that contains all the resolvers. |
|
3
|
|
|
|
|
4
|
|
|
A resolver is a class that is able to retrieve a dependency to inject. |
|
5
|
|
|
""" |
|
6
|
1 |
|
from pyjection.reference import Reference |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
1 |
|
class BaseResolver(object): |
|
10
|
|
|
""" |
|
11
|
|
|
Base class for the resolvers |
|
12
|
|
|
""" |
|
13
|
|
|
|
|
14
|
1 |
|
def __init__(self, injector): |
|
15
|
1 |
|
self._injector = injector |
|
16
|
|
|
|
|
17
|
1 |
|
def resolve(self, method_parameter, service): |
|
18
|
|
|
pass |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
1 |
|
class ServiceResolver(BaseResolver): |
|
22
|
|
|
""" |
|
23
|
|
|
Try to resolve the dependency based on |
|
24
|
|
|
the service that has been provided to the injector. |
|
25
|
|
|
""" |
|
26
|
|
|
|
|
27
|
1 |
|
def resolve(self, method_parameter, service): |
|
28
|
1 |
|
if method_parameter.name not in service.arguments: |
|
29
|
1 |
|
return None |
|
30
|
|
|
|
|
31
|
1 |
|
value = service.arguments[method_parameter.name] |
|
32
|
1 |
|
if not isinstance(value, Reference): |
|
33
|
1 |
|
return value |
|
34
|
|
|
# The value references an other dependency service |
|
35
|
1 |
|
if value.return_class: |
|
36
|
1 |
|
return self._injector.get_uninstantiated(value.name) |
|
37
|
1 |
|
return self._injector.get(value.name) |
|
38
|
|
|
|
|
39
|
|
|
|
|
40
|
1 |
|
class NameResolver(BaseResolver): |
|
41
|
|
|
""" |
|
42
|
|
|
Try to resolve the dependency based on the parameter name. |
|
43
|
|
|
|
|
44
|
|
|
If the latter is declared in the dependency injector then we retrieve it |
|
45
|
|
|
as the instance to inject. |
|
46
|
|
|
""" |
|
47
|
|
|
|
|
48
|
1 |
|
def resolve(self, method_parameter, service): |
|
49
|
1 |
|
if self._injector.has_service(method_parameter.name): |
|
50
|
|
|
return self._injector.get(method_parameter.name) |
|
51
|
|
|
|