Added logic to handle extensions before running the app
This commit is contained in:
parent
05a9bdbf4a
commit
3903f85368
@ -1,9 +1,12 @@
|
||||
from typing import Type, Optional
|
||||
import types
|
||||
from typing import Type, Optional, Callable
|
||||
|
||||
from cpl_core.application.application_abc import ApplicationABC
|
||||
from cpl_core.application.application_builder_abc import ApplicationBuilderABC
|
||||
from cpl_core.application.application_extension_abc import ApplicationExtensionABC
|
||||
from cpl_core.application.startup_abc import StartupABC
|
||||
from cpl_core.configuration.configuration import Configuration
|
||||
from cpl_core.console import Console
|
||||
from cpl_core.dependency_injection.service_collection import ServiceCollection
|
||||
|
||||
|
||||
@ -25,12 +28,25 @@ class ApplicationBuilder(ApplicationBuilderABC):
|
||||
self._environment = self._configuration.environment
|
||||
self._services = ServiceCollection(self._configuration)
|
||||
|
||||
self._extensions: list[Callable] = []
|
||||
|
||||
def use_startup(self, startup: Type[StartupABC]):
|
||||
self._startup = startup()
|
||||
|
||||
def use_extension(self, extension: Type[ApplicationExtensionABC]):
|
||||
if extension not in self._extensions:
|
||||
self._extensions.append(extension)
|
||||
|
||||
def build(self) -> ApplicationABC:
|
||||
if self._startup is not None:
|
||||
self._startup.configure_configuration(self._configuration, self._environment)
|
||||
self._startup.configure_services(self._services, self._environment)
|
||||
|
||||
return self._app(self._configuration, self._services.build_service_provider())
|
||||
config = self._configuration
|
||||
services = self._services.build_service_provider()
|
||||
|
||||
for ex in self._extensions:
|
||||
extension = ex()
|
||||
extension.run(config, services)
|
||||
|
||||
return self._app(config, services)
|
||||
|
13
src/cpl_core/application/application_extension_abc.py
Normal file
13
src/cpl_core/application/application_extension_abc.py
Normal file
@ -0,0 +1,13 @@
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
from cpl_core.configuration import ConfigurationABC
|
||||
from cpl_core.dependency_injection import ServiceProviderABC
|
||||
|
||||
|
||||
class ApplicationExtensionABC(ABC):
|
||||
|
||||
@abstractmethod
|
||||
def __init__(self): pass
|
||||
|
||||
@abstractmethod
|
||||
def run(self, config: ConfigurationABC, services: ServiceProviderABC): pass
|
@ -37,6 +37,7 @@ class Application(ApplicationABC):
|
||||
self._mailer = self._services.get_service(EMailClientABC)
|
||||
|
||||
def main(self):
|
||||
if self._configuration.environment.application_name != '':
|
||||
self._logger.header(f'{self._configuration.environment.application_name}:')
|
||||
self._logger.debug(__name__, f'Host: {self._configuration.environment.host_name}')
|
||||
self._logger.debug(__name__, f'Environment: {self._configuration.environment.environment_name}')
|
||||
|
@ -1,11 +1,13 @@
|
||||
from cpl_core.application.application_builder import ApplicationBuilder
|
||||
from application import Application
|
||||
from general.test_extension import TestExtension
|
||||
from startup import Startup
|
||||
|
||||
|
||||
def main():
|
||||
app_builder = ApplicationBuilder(Application)
|
||||
app_builder.use_startup(Startup)
|
||||
app_builder.use_extension(TestExtension)
|
||||
app_builder.build().run()
|
||||
|
||||
|
||||
|
13
src/tests/custom/general/src/general/test_extension.py
Normal file
13
src/tests/custom/general/src/general/test_extension.py
Normal file
@ -0,0 +1,13 @@
|
||||
from cpl_core.application.application_extension_abc import ApplicationExtensionABC
|
||||
from cpl_core.configuration import ConfigurationABC
|
||||
from cpl_core.console import Console
|
||||
from cpl_core.dependency_injection import ServiceProviderABC
|
||||
|
||||
|
||||
class TestExtension(ApplicationExtensionABC):
|
||||
|
||||
def __init__(self):
|
||||
ApplicationExtensionABC.__init__(self)
|
||||
|
||||
def run(self, config: ConfigurationABC, services: ServiceProviderABC):
|
||||
Console.write_line('Hello World from App Extension')
|
Loading…
Reference in New Issue
Block a user