2020-11-22 14:15:39 +01:00
|
|
|
from collections import Callable
|
2020-11-25 21:41:45 +01:00
|
|
|
from inspect import signature, Parameter
|
2021-03-03 10:47:52 +01:00
|
|
|
from typing import Type, Optional, Union
|
2020-11-22 14:15:39 +01:00
|
|
|
|
2021-03-03 10:47:52 +01:00
|
|
|
from cpl.application.application_runtime_abc import ApplicationRuntimeABC
|
2021-03-09 14:35:45 +01:00
|
|
|
from cpl.configuration.configuration_abc import ConfigurationABC
|
2021-03-03 10:47:52 +01:00
|
|
|
from cpl.configuration.configuration_model_abc import ConfigurationModelABC
|
|
|
|
from cpl.database.context.database_context_abc import DatabaseContextABC
|
|
|
|
from cpl.dependency_injection.service_abc import ServiceABC
|
2021-03-08 22:02:16 +01:00
|
|
|
from cpl.dependency_injection.service_provider_abc import ServiceProviderABC
|
2021-03-14 16:20:29 +01:00
|
|
|
from cpl.environment.environment_abc import ApplicationEnvironmentABC
|
2020-11-22 14:15:39 +01:00
|
|
|
|
|
|
|
|
2021-03-03 10:47:52 +01:00
|
|
|
class ServiceProvider(ServiceProviderABC):
|
2020-11-22 14:15:39 +01:00
|
|
|
|
2021-03-03 10:47:52 +01:00
|
|
|
def __init__(self, app_runtime: ApplicationRuntimeABC):
|
|
|
|
ServiceProviderABC.__init__(self)
|
|
|
|
self._app_runtime: ApplicationRuntimeABC = app_runtime
|
|
|
|
self._database_context: Optional[DatabaseContextABC] = None
|
2020-11-22 14:15:39 +01:00
|
|
|
|
2021-03-04 17:29:04 +01:00
|
|
|
self._transient_services: dict[Type[ServiceABC], Callable[ServiceABC]] = {}
|
|
|
|
self._scoped_services: dict[Type[ServiceABC], Callable[ServiceABC]] = {}
|
2021-03-03 10:47:52 +01:00
|
|
|
self._singleton_services: dict[Type[ServiceABC], Callable[ServiceABC], ServiceABC] = {}
|
2020-11-26 11:55:55 +01:00
|
|
|
|
2021-03-03 10:47:52 +01:00
|
|
|
def _create_instance(self, service: Union[Callable[ServiceABC], ServiceABC]) -> Callable[ServiceABC]:
|
2020-11-25 21:41:45 +01:00
|
|
|
sig = signature(service.__init__)
|
|
|
|
params = []
|
|
|
|
for param in sig.parameters.items():
|
|
|
|
parameter = param[1]
|
|
|
|
if parameter.name != 'self' and parameter.annotation != Parameter.empty:
|
2021-03-03 10:47:52 +01:00
|
|
|
if issubclass(parameter.annotation, ApplicationRuntimeABC):
|
2020-11-26 18:58:08 +01:00
|
|
|
params.append(self._app_runtime)
|
2020-11-26 11:20:21 +01:00
|
|
|
|
2021-03-14 16:20:29 +01:00
|
|
|
elif issubclass(parameter.annotation, ApplicationEnvironmentABC):
|
2020-12-20 14:49:28 +01:00
|
|
|
params.append(self._app_runtime.configuration.environment)
|
|
|
|
|
2021-03-03 10:47:52 +01:00
|
|
|
elif issubclass(parameter.annotation, DatabaseContextABC):
|
2020-12-11 21:48:46 +01:00
|
|
|
params.append(self._database_context)
|
|
|
|
|
2021-03-03 10:47:52 +01:00
|
|
|
elif issubclass(parameter.annotation, ConfigurationModelABC):
|
2020-11-28 15:13:54 +01:00
|
|
|
params.append(self._app_runtime.configuration.get_configuration(parameter.annotation))
|
2020-11-22 14:15:39 +01:00
|
|
|
|
2021-03-09 14:35:45 +01:00
|
|
|
elif issubclass(parameter.annotation, ConfigurationABC):
|
|
|
|
params.append(self._app_runtime.configuration)
|
|
|
|
|
2021-03-04 19:06:16 +01:00
|
|
|
elif issubclass(parameter.annotation, ServiceProviderABC):
|
|
|
|
params.append(self)
|
|
|
|
|
2021-03-03 10:47:52 +01:00
|
|
|
else:
|
|
|
|
params.append(self.get_service(parameter.annotation))
|
|
|
|
|
2020-11-25 21:41:45 +01:00
|
|
|
return service(*params)
|
2020-11-22 14:15:39 +01:00
|
|
|
|
2021-03-03 10:47:52 +01:00
|
|
|
def add_db_context(self, db_context: Type[DatabaseContextABC]):
|
2020-12-11 21:48:46 +01:00
|
|
|
self._database_context = self._create_instance(db_context)
|
|
|
|
|
2021-03-03 10:47:52 +01:00
|
|
|
def get_db_context(self) -> Callable[DatabaseContextABC]:
|
2020-12-11 21:48:46 +01:00
|
|
|
return self._database_context
|
|
|
|
|
2021-03-04 17:55:15 +01:00
|
|
|
def add_transient(self, service_type: Type[ServiceABC], service: Callable[ServiceABC] = None):
|
|
|
|
if service is None:
|
|
|
|
self._transient_services[service_type] = service_type
|
|
|
|
else:
|
|
|
|
self._transient_services[service_type] = service
|
|
|
|
|
|
|
|
def add_scoped(self, service_type: Type[ServiceABC], service: Callable[ServiceABC] = None):
|
|
|
|
if service is None:
|
|
|
|
self._scoped_services[service_type] = service_type
|
|
|
|
else:
|
|
|
|
self._scoped_services[service_type] = service
|
|
|
|
|
|
|
|
def add_singleton(self, service_type: Type[ServiceABC], service: Callable[ServiceABC] = None):
|
2020-11-22 14:15:39 +01:00
|
|
|
for known_service in self._singleton_services:
|
2020-11-29 21:36:16 +01:00
|
|
|
if type(known_service) == service_type:
|
|
|
|
raise Exception(f'Service with type {service_type} already exists')
|
2020-11-22 14:15:39 +01:00
|
|
|
|
2021-03-04 17:55:15 +01:00
|
|
|
if service is None:
|
|
|
|
self._singleton_services[service_type] = self._create_instance(service_type)
|
|
|
|
else:
|
|
|
|
self._singleton_services[service_type] = self._create_instance(service)
|
2020-11-22 14:15:39 +01:00
|
|
|
|
2021-03-03 10:47:52 +01:00
|
|
|
def get_service(self, instance_type: Type) -> Callable[ServiceABC]:
|
2021-03-04 19:06:16 +01:00
|
|
|
if issubclass(instance_type, ServiceProviderABC):
|
|
|
|
return self
|
|
|
|
|
2020-11-25 21:41:45 +01:00
|
|
|
for service in self._transient_services:
|
|
|
|
if service == instance_type and isinstance(self._transient_services[service], type(instance_type)):
|
|
|
|
return self._create_instance(self._transient_services[service])
|
2020-11-22 14:15:39 +01:00
|
|
|
|
2020-11-25 21:41:45 +01:00
|
|
|
for service in self._scoped_services:
|
|
|
|
if service == instance_type and isinstance(self._scoped_services[service], type(instance_type)):
|
|
|
|
return self._create_instance(self._scoped_services[service])
|
2020-11-22 14:15:39 +01:00
|
|
|
|
|
|
|
for service in self._singleton_services:
|
2020-11-25 21:41:45 +01:00
|
|
|
if service == instance_type and isinstance(self._singleton_services[service], instance_type):
|
|
|
|
return self._singleton_services[service]
|
2020-11-22 14:15:39 +01:00
|
|
|
|
2021-03-03 10:47:52 +01:00
|
|
|
def remove_service(self, instance_type: Type[ServiceABC]):
|
2020-11-25 21:41:45 +01:00
|
|
|
for service in self._transient_services:
|
2020-12-25 14:53:50 +01:00
|
|
|
if service == instance_type and isinstance(self._transient_services[service], type(instance_type)):
|
2020-11-25 21:41:45 +01:00
|
|
|
del self._transient_services[service]
|
2020-11-22 14:15:39 +01:00
|
|
|
return
|
|
|
|
|
2020-11-25 21:41:45 +01:00
|
|
|
for service in self._scoped_services:
|
2020-12-25 14:53:50 +01:00
|
|
|
if service == instance_type and isinstance(self._scoped_services[service], type(instance_type)):
|
2020-11-25 21:41:45 +01:00
|
|
|
del self._scoped_services[service]
|
2020-11-22 14:15:39 +01:00
|
|
|
return
|
|
|
|
|
|
|
|
for service in self._singleton_services:
|
2020-12-25 14:53:50 +01:00
|
|
|
if service == instance_type and isinstance(self._singleton_services[service], instance_type):
|
2020-11-25 21:41:45 +01:00
|
|
|
del self._singleton_services[service]
|
2020-11-22 14:15:39 +01:00
|
|
|
return
|