From bca8b9d3d4b5213ad8a80795fa9919be9218bbb6 Mon Sep 17 00:00:00 2001 From: Jonathan Renard <1273438+fox-john@users.noreply.github.com> Date: Thu, 5 Dec 2024 18:22:52 +0100 Subject: [PATCH] fix: service container v9 (#397) --- .zip-contents | 1 + Makefile | 17 +- config.php | 27 ++ config/admin/services.yml | 14 - config/common/common.yml | 55 --- config/common/repository.yml | 152 -------- config/front/api.yml | 24 -- config/front/services.yml | 273 -------------- config/parameters.yml | 7 - controllers/front/apiHealthCheck.php | 2 +- controllers/front/apiShopContent.php | 2 +- ps_eventbus.php | 51 +-- src/DependencyInjection/ContainerProvider.php | 131 ------- src/DependencyInjection/ServiceContainer.php | 130 ------- src/Log/Logger.php | 153 ++++++++ src/Service/ApiShopContentService.php | 8 +- src/Service/PresenterService.php | 3 +- src/Service/SynchronizationService.php | 8 +- .../Contract/IServiceProvider.php | 33 ++ .../Contract/ISingletonService.php | 33 ++ .../Exception/ParameterNotFoundException.php | 25 ++ .../Exception/ProviderNotFoundException.php | 25 ++ .../Exception/ServiceNotFoundException.php | 25 ++ src/ServiceContainer/Provider/ApiProvider.php | 61 ++++ .../Provider/CommonProvider.php | 83 +++++ .../Provider/RepositoryProvider.php | 183 ++++++++++ .../Provider/ServiceProvider.php | 339 ++++++++++++++++++ src/ServiceContainer/ServiceContainer.php | 282 +++++++++++++++ 28 files changed, 1306 insertions(+), 841 deletions(-) create mode 100644 config.php delete mode 100644 config/common/common.yml delete mode 100644 config/common/repository.yml delete mode 100644 config/front/api.yml delete mode 100644 config/parameters.yml delete mode 100644 src/DependencyInjection/ContainerProvider.php delete mode 100644 src/DependencyInjection/ServiceContainer.php create mode 100644 src/Log/Logger.php create mode 100644 src/ServiceContainer/Contract/IServiceProvider.php create mode 100644 src/ServiceContainer/Contract/ISingletonService.php create mode 100644 src/ServiceContainer/Exception/ParameterNotFoundException.php create mode 100644 src/ServiceContainer/Exception/ProviderNotFoundException.php create mode 100644 src/ServiceContainer/Exception/ServiceNotFoundException.php create mode 100644 src/ServiceContainer/Provider/ApiProvider.php create mode 100644 src/ServiceContainer/Provider/CommonProvider.php create mode 100644 src/ServiceContainer/Provider/RepositoryProvider.php create mode 100644 src/ServiceContainer/Provider/ServiceProvider.php create mode 100644 src/ServiceContainer/ServiceContainer.php diff --git a/.zip-contents b/.zip-contents index 49275582..7db0f1e9 100644 --- a/.zip-contents +++ b/.zip-contents @@ -5,6 +5,7 @@ src upgrade vendor ps_eventbus.php +config.php logo.png config.xml LICENSE diff --git a/Makefile b/Makefile index dff473e2..381ec533 100644 --- a/Makefile +++ b/Makefile @@ -38,22 +38,22 @@ zip: zip-prod zip-inte zip-e2e # target: zip-e2e - Bundle a local E2E integrable zip .PHONY: zip-e2e zip-e2e: vendor tools/vendor dist - @$(call zip_it,./config/parameters.yml,${PACKAGE}_e2e.zip) + @$(call zip_it,${PACKAGE}_e2e.zip) # target: zip-inte - Bundle an integration zip .PHONY: zip-inte zip-inte: vendor tools/vendor dist - @$(call zip_it,.config.inte.yml,${PACKAGE}_integration.zip) + @$(call zip_it,${PACKAGE}_integration.zip) # target: zip-prod - Bundle a production zip .PHONY: zip-prod zip-prod: vendor tools/vendor dist - @$(call zip_it,.config.prod.yml,${PACKAGE}.zip) + @$(call zip_it,${PACKAGE}.zip) # target: zip-unzipped - Bundle a production module, but without zip step (only to check sources) .PHONY: zip-unzipped zip-unzipped: vendor tools/vendor dist - @$(call no_zip_it,.config.prod.yml) + @$(call no_zip_it) dist: @mkdir -p ./dist @@ -184,20 +184,19 @@ define create_module $(call replace_version,${TMP_DIR}/${MODULE_NAME},${SEM_VERSION}) ./tools/vendor/bin/autoindex prestashop:add:index ${TMP_DIR} tools/vendor/bin/header-stamp --target=${TMP_DIR}/${MODULE_NAME} --license=tools/vendor/prestashop/header-stamp/assets/osl3.txt --exclude=vendor,e2e,e2e-env,tests,composer.json,scoper.inc.php - cp $1 ${TMP_DIR}/${MODULE_NAME}/config/parameters.yml cd ${TMP_DIR}/${MODULE_NAME} && composer dump-autoload endef define zip_it - TMP_DIR=$(call create_module,$1) - cd ${TMP_DIR} && zip -9 -r $2 ./${MODULE_NAME}; - mv ${TMP_DIR}/$2 ./dist; + TMP_DIR=$(call create_module) + cd ${TMP_DIR} && zip -9 -r $1 ./${MODULE_NAME}; + mv ${TMP_DIR}/$1 ./dist; rm -rf ${TMP_DIR}; endef define no_zip_it rm -rf ./dist/${MODULE_NAME} - TMP_DIR=$(call create_module,$1) + TMP_DIR=$(call create_module) mv ${TMP_DIR}/${MODULE_NAME} ./dist; rm -rf ${TMP_DIR:-/dev/null}; endef diff --git a/config.php b/config.php new file mode 100644 index 00000000..27c8398e --- /dev/null +++ b/config.php @@ -0,0 +1,27 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 + */ + +return [ + 'ps_eventbus.proxy_api_url' => 'http://reverse-proxy/collector', + 'ps_eventbus.sync_api_url' => 'http://reverse-proxy/sync-api', + 'ps_eventbus.live_sync_api_url' => 'http://reverse-proxy/live-sync-api/v1', + 'ps_eventbus.sentry_dsn' => 'https://sentry-id@stuff.ingest.sentry.io/stuff', + 'ps_eventbus.sentry_env' => 'development' +]; diff --git a/config/admin/services.yml b/config/admin/services.yml index 521fb78a..0baad476 100644 --- a/config/admin/services.yml +++ b/config/admin/services.yml @@ -1,15 +1 @@ -imports: - - { resource: ../common/common.yml } - - { resource: ../common/repository.yml } - - { resource: ../front/services.yml } - services: - ps_eventbus.service.presenter: - class: 'PrestaShop\Module\PsEventbus\Service\PresenterService' - public: true - - ps_eventbus.module.upgrade: - class: 'PrestaShop\Module\PsEventbus\Module\Upgrade' - arguments: - - "@ps_eventbus" - public: true diff --git a/config/common/common.yml b/config/common/common.yml deleted file mode 100644 index 431885cc..00000000 --- a/config/common/common.yml +++ /dev/null @@ -1,55 +0,0 @@ -imports: - - { resource: ../parameters.yml } - -services: - ps_eventbus.db: - class: Db - public: true - factory: ['Db', 'getInstance'] - - ps_eventbus: - class: 'Ps_eventbus' - public: true - factory: ['Module', 'getInstanceByName'] - arguments: - - 'ps_eventbus' - - ps_eventbus.context: - class: 'Context' - public: true - factory: ['PrestaShop\Module\PsEventbus\Factory\ContextFactory', 'getContext'] - - ps_eventbus.helper.module: - class: 'PrestaShop\Module\PsEventbus\Helper\ModuleHelper' - public: true - - PrestaShop\Module\PsEventbus\Service\PsAccountsAdapterService: - class: PrestaShop\Module\PsEventbus\Service\PsAccountsAdapterService - public: true - arguments: - - '@ps_eventbus.helper.module' - - PrestaShop\Module\PsEventbus\Formatter\JsonFormatter: - class: PrestaShop\Module\PsEventbus\Formatter\JsonFormatter - public: true - - PrestaShop\Module\PsEventbus\Formatter\ArrayFormatter: - class: PrestaShop\Module\PsEventbus\Formatter\ArrayFormatter - public: true - - PrestaShop\Module\PsEventbus\Service\ProxyService: - class: PrestaShop\Module\PsEventbus\Service\ProxyService - public: true - arguments: - - '@PrestaShop\Module\PsEventbus\Api\CollectorApiClient' - - '@PrestaShop\Module\PsEventbus\Formatter\JsonFormatter' - - '@PrestaShop\Module\PsEventbus\Handler\ErrorHandler\ErrorHandler' - - PrestaShop\Module\PsEventbus\Handler\ErrorHandler\ErrorHandler: - class: PrestaShop\Module\PsEventbus\Handler\ErrorHandler\ErrorHandler - public: true - arguments: - - '@ps_eventbus' - - '@PrestaShop\Module\PsEventbus\Service\PsAccountsAdapterService' - - '%ps_eventbus.sentry_dsn%' - - '%ps_eventbus.sentry_env%' diff --git a/config/common/repository.yml b/config/common/repository.yml deleted file mode 100644 index b349eefd..00000000 --- a/config/common/repository.yml +++ /dev/null @@ -1,152 +0,0 @@ -services: - PrestaShop\Module\PsEventbus\Repository\SyncRepository: - class: PrestaShop\Module\PsEventbus\Repository\SyncRepository - public: true - arguments: - - '@=service("ps_eventbus").getContext()' - - PrestaShop\Module\PsEventbus\Repository\IncrementalSyncRepository: - class: PrestaShop\Module\PsEventbus\Repository\IncrementalSyncRepository - public: true - arguments: - - '@PrestaShop\Module\PsEventbus\Handler\ErrorHandler\ErrorHandler' - - PrestaShop\Module\PsEventbus\Repository\LiveSyncRepository: - class: PrestaShop\Module\PsEventbus\Repository\LiveSyncRepository - public: true - -############################################################################################################ -###################################### SHOP CONTENT REPOSITORIES ########################################### -############################################################################################################ - - PrestaShop\Module\PsEventbus\Repository\BundleRepository: - class: PrestaShop\Module\PsEventbus\Repository\BundleRepository - public: true - - PrestaShop\Module\PsEventbus\Repository\CarrierRepository: - class: PrestaShop\Module\PsEventbus\Repository\CarrierRepository - public: true - - PrestaShop\Module\PsEventbus\Repository\CarrierDetailRepository: - class: PrestaShop\Module\PsEventbus\Repository\CarrierDetailRepository - public: true - - PrestaShop\Module\PsEventbus\Repository\CarrierTaxeRepository: - class: PrestaShop\Module\PsEventbus\Repository\CarrierTaxeRepository - public: true - - PrestaShop\Module\PsEventbus\Repository\CartRepository: - class: PrestaShop\Module\PsEventbus\Repository\CartRepository - public: true - - PrestaShop\Module\PsEventbus\Repository\CartProductRepository: - class: PrestaShop\Module\PsEventbus\Repository\CartProductRepository - public: true - - PrestaShop\Module\PsEventbus\Repository\CartRuleRepository: - class: PrestaShop\Module\PsEventbus\Repository\CartRuleRepository - public: true - - PrestaShop\Module\PsEventbus\Repository\CustomProductCarrierRepository: - class: PrestaShop\Module\PsEventbus\Repository\CustomProductCarrierRepository - public: true - - PrestaShop\Module\PsEventbus\Repository\CategoryRepository: - class: PrestaShop\Module\PsEventbus\Repository\CategoryRepository - public: true - - PrestaShop\Module\PsEventbus\Repository\CustomerRepository: - class: PrestaShop\Module\PsEventbus\Repository\CustomerRepository - public: true - - PrestaShop\Module\PsEventbus\Repository\CurrencyRepository: - class: PrestaShop\Module\PsEventbus\Repository\CurrencyRepository - public: true - - PrestaShop\Module\PsEventbus\Repository\EmployeeRepository: - class: PrestaShop\Module\PsEventbus\Repository\EmployeeRepository - public: true - - PrestaShop\Module\PsEventbus\Repository\ImageRepository: - class: PrestaShop\Module\PsEventbus\Repository\ImageRepository - public: true - - PrestaShop\Module\PsEventbus\Repository\ImageTypeRepository: - class: PrestaShop\Module\PsEventbus\Repository\ImageTypeRepository - public: true - - PrestaShop\Module\PsEventbus\Repository\ModuleRepository: - class: PrestaShop\Module\PsEventbus\Repository\ModuleRepository - public: true - - PrestaShop\Module\PsEventbus\Repository\LanguageRepository: - class: PrestaShop\Module\PsEventbus\Repository\LanguageRepository - public: true - - PrestaShop\Module\PsEventbus\Repository\ManufacturerRepository: - class: PrestaShop\Module\PsEventbus\Repository\ManufacturerRepository - public: true - - PrestaShop\Module\PsEventbus\Repository\OrderRepository: - class: PrestaShop\Module\PsEventbus\Repository\OrderRepository - public: true - - PrestaShop\Module\PsEventbus\Repository\OrderCartRuleRepository: - class: PrestaShop\Module\PsEventbus\Repository\OrderCartRuleRepository - public: true - - PrestaShop\Module\PsEventbus\Repository\OrderstatusHistoryRepository: - class: PrestaShop\Module\PsEventbus\Repository\OrderStatusHistoryRepository - public: true - - PrestaShop\Module\PsEventbus\Repository\OrderDetailRepository: - class: PrestaShop\Module\PsEventbus\Repository\OrderDetailRepository - public: true - - PrestaShop\Module\PsEventbus\Repository\ProductRepository: - class: PrestaShop\Module\PsEventbus\Repository\ProductRepository - public: true - - PrestaShop\Module\PsEventbus\Repository\ProductSupplierRepository: - class: PrestaShop\Module\PsEventbus\Repository\ProductSupplierRepository - public: true - - PrestaShop\Module\PsEventbus\Repository\InfoRepository: - class: PrestaShop\Module\PsEventbus\Repository\InfoRepository - public: true - - PrestaShop\Module\PsEventbus\Repository\StockRepository: - class: PrestaShop\Module\PsEventbus\Repository\StockRepository - public: true - - PrestaShop\Module\PsEventbus\Repository\StockMovementRepository: - class: PrestaShop\Module\PsEventbus\Repository\StockMovementRepository - public: true - - PrestaShop\Module\PsEventbus\Repository\SpecificPriceRepository: - class: PrestaShop\Module\PsEventbus\Repository\SpecificPriceRepository - public: true - - PrestaShop\Module\PsEventbus\Repository\SupplierRepository: - class: PrestaShop\Module\PsEventbus\Repository\SupplierRepository - public: true - - PrestaShop\Module\PsEventbus\Repository\StoreRepository: - class: PrestaShop\Module\PsEventbus\Repository\StoreRepository - public: true - - PrestaShop\Module\PsEventbus\Repository\TaxonomyRepository: - class: PrestaShop\Module\PsEventbus\Repository\TaxonomyRepository - public: true - - PrestaShop\Module\PsEventbus\Repository\TranslationRepository: - class: PrestaShop\Module\PsEventbus\Repository\TranslationRepository - public: true - - PrestaShop\Module\PsEventbus\Repository\WishlistRepository: - class: PrestaShop\Module\PsEventbus\Repository\WishlistRepository - public: true - - PrestaShop\Module\PsEventbus\Repository\WishlistProductRepository: - class: PrestaShop\Module\PsEventbus\Repository\WishlistProductRepository - public: true diff --git a/config/front/api.yml b/config/front/api.yml deleted file mode 100644 index 91cbbd65..00000000 --- a/config/front/api.yml +++ /dev/null @@ -1,24 +0,0 @@ -services: - PrestaShop\Module\PsEventbus\Api\SyncApiClient: - class: PrestaShop\Module\PsEventbus\Api\SyncApiClient - public: true - arguments: - - '%ps_eventbus.sync_api_url%' - - '@ps_eventbus' - - '@PrestaShop\Module\PsEventbus\Service\PsAccountsAdapterService' - - PrestaShop\Module\PsEventbus\Api\LiveSyncApiClient: - class: PrestaShop\Module\PsEventbus\Api\LiveSyncApiClient - public: true - arguments: - - '%ps_eventbus.live_sync_api_url%' - - '@ps_eventbus' - - '@PrestaShop\Module\PsEventbus\Service\PsAccountsAdapterService' - - PrestaShop\Module\PsEventbus\Api\CollectorApiClient: - class: PrestaShop\Module\PsEventbus\Api\CollectorApiClient - public: true - arguments: - - '%ps_eventbus.proxy_api_url%' - - '@ps_eventbus' - - '@PrestaShop\Module\PsEventbus\Service\PsAccountsAdapterService' diff --git a/config/front/services.yml b/config/front/services.yml index f5874a39..0baad476 100644 --- a/config/front/services.yml +++ b/config/front/services.yml @@ -1,274 +1 @@ -imports: - - { resource: api.yml } - - { resource: ../common/common.yml } - - { resource: ../common/repository.yml } - services: - PrestaShop\Module\PsEventbus\Service\ApiAuthorizationService: - class: PrestaShop\Module\PsEventbus\Service\ApiAuthorizationService - public: true - arguments: - - '@PrestaShop\Module\PsEventbus\Repository\SyncRepository' - - '@PrestaShop\Module\PsEventbus\Api\SyncApiClient' - - '@PrestaShop\Module\PsEventbus\Service\PsAccountsAdapterService' - - '@PrestaShop\Module\PsEventbus\Handler\ErrorHandler\ErrorHandler' - - PrestaShop\Module\PsEventbus\Service\SynchronizationService: - class: PrestaShop\Module\PsEventbus\Service\SynchronizationService - public: true - arguments: - - '@PrestaShop\Module\PsEventbus\Api\LiveSyncApiClient' - - '@PrestaShop\Module\PsEventbus\Repository\SyncRepository' - - '@PrestaShop\Module\PsEventbus\Repository\IncrementalSyncRepository' - - '@PrestaShop\Module\PsEventbus\Repository\LiveSyncRepository' - - '@PrestaShop\Module\PsEventbus\Service\ShopContent\LanguagesService' - - '@PrestaShop\Module\PsEventbus\Service\ProxyService' - - '@PrestaShop\Module\PsEventbus\Handler\ErrorHandler\ErrorHandler' - - PrestaShop\Module\PsEventbus\Service\PresenterService: - class: PrestaShop\Module\PsEventbus\Service\PresenterService - public: true - - PrestaShop\Module\PsEventbus\Service\ApiShopContentService: - class: PrestaShop\Module\PsEventbus\Service\ApiShopContentService - public: true - arguments: - - '@ps_eventbus' - - '@PrestaShop\Module\PsEventbus\Handler\ErrorHandler\ErrorHandler' - - '@PrestaShop\Module\PsEventbus\Service\ApiAuthorizationService' - - '@PrestaShop\Module\PsEventbus\Service\SynchronizationService' - - '@PrestaShop\Module\PsEventbus\Repository\SyncRepository' - - '@PrestaShop\Module\PsEventbus\Handler\ErrorHandler\ErrorHandler' - - PrestaShop\Module\PsEventbus\Service\ApiHealthCheckService: - class: PrestaShop\Module\PsEventbus\Service\ApiHealthCheckService - public: true - arguments: - - '@PrestaShop\Module\PsEventbus\Service\PsAccountsAdapterService' - - '@PrestaShop\Module\PsEventbus\Service\ApiAuthorizationService' - - '@PrestaShop\Module\PsEventbus\Handler\ErrorHandler\ErrorHandler' - - '%ps_eventbus.sync_api_url%' - - '%ps_eventbus.live_sync_api_url%' - - '%ps_eventbus.proxy_api_url%' - -############################################################################################################ -###################################### SHOP CONTENT SERVICES ############################################### -############################################################################################################ - PrestaShop\Module\PsEventbus\Service\ShopContent\AttributesService: - class: PrestaShop\Module\PsEventbus\Service\ShopContent\AttributesService - public: true - - PrestaShop\Module\PsEventbus\Service\ShopContent\BundlesService: - class: PrestaShop\Module\PsEventbus\Service\ShopContent\BundlesService - public: true - arguments: - - '@PrestaShop\Module\PsEventbus\Repository\BundleRepository' - - PrestaShop\Module\PsEventbus\Service\ShopContent\CarriersService: - class: PrestaShop\Module\PsEventbus\Service\ShopContent\CarriersService - public: true - arguments: - - '@PrestaShop\Module\PsEventbus\Repository\CarrierRepository' - - PrestaShop\Module\PsEventbus\Service\ShopContent\CarrierDetailsService: - class: PrestaShop\Module\PsEventbus\Service\ShopContent\CarrierDetailsService - public: true - arguments: - - '@PrestaShop\Module\PsEventbus\Repository\CarrierDetailRepository' - - PrestaShop\Module\PsEventbus\Service\ShopContent\CarrierTaxesService: - class: PrestaShop\Module\PsEventbus\Service\ShopContent\CarrierTaxesService - public: true - arguments: - - '@PrestaShop\Module\PsEventbus\Repository\CarrierTaxeRepository' - - PrestaShop\Module\PsEventbus\Service\ShopContent\CartsService: - class: PrestaShop\Module\PsEventbus\Service\ShopContent\CartsService - public: true - arguments: - - '@PrestaShop\Module\PsEventbus\Repository\CartRepository' - - PrestaShop\Module\PsEventbus\Service\ShopContent\CartProductsService: - class: PrestaShop\Module\PsEventbus\Service\ShopContent\CartProductsService - public: true - arguments: - - '@PrestaShop\Module\PsEventbus\Repository\CartProductRepository' - - PrestaShop\Module\PsEventbus\Service\ShopContent\CartRulesService: - class: PrestaShop\Module\PsEventbus\Service\ShopContent\CartRulesService - public: true - arguments: - - '@PrestaShop\Module\PsEventbus\Repository\CartRuleRepository' - - PrestaShop\Module\PsEventbus\Service\ShopContent\CustomProductCarriersService: - class: PrestaShop\Module\PsEventbus\Service\ShopContent\CustomProductCarriersService - public: true - arguments: - - '@PrestaShop\Module\PsEventbus\Repository\CustomProductCarrierRepository' - - PrestaShop\Module\PsEventbus\Service\ShopContent\CustomersService: - class: PrestaShop\Module\PsEventbus\Service\ShopContent\CustomersService - public: true - arguments: - - '@PrestaShop\Module\PsEventbus\Repository\CustomerRepository' - - PrestaShop\Module\PsEventbus\Service\ShopContent\CategoriesService: - class: PrestaShop\Module\PsEventbus\Service\ShopContent\CategoriesService - public: true - arguments: - - '@PrestaShop\Module\PsEventbus\Repository\CategoryRepository' - - PrestaShop\Module\PsEventbus\Service\ShopContent\CurrenciesService: - class: PrestaShop\Module\PsEventbus\Service\ShopContent\CurrenciesService - public: true - arguments: - - '@PrestaShop\Module\PsEventbus\Repository\CurrencyRepository' - - PrestaShop\Module\PsEventbus\Service\ShopContent\EmployeesService: - class: PrestaShop\Module\PsEventbus\Service\ShopContent\EmployeesService - public: true - arguments: - - '@PrestaShop\Module\PsEventbus\Repository\EmployeeRepository' - - PrestaShop\Module\PsEventbus\Service\ShopContent\ImagesService: - class: PrestaShop\Module\PsEventbus\Service\ShopContent\ImagesService - public: true - arguments: - - '@PrestaShop\Module\PsEventbus\Repository\ImageRepository' - - PrestaShop\Module\PsEventbus\Service\ShopContent\ImageTypesService: - class: PrestaShop\Module\PsEventbus\Service\ShopContent\ImageTypesService - public: true - arguments: - - '@PrestaShop\Module\PsEventbus\Repository\ImageTypeRepository' - - - PrestaShop\Module\PsEventbus\Service\ShopContent\LanguagesService: - class: PrestaShop\Module\PsEventbus\Service\ShopContent\LanguagesService - public: true - arguments: - - '@PrestaShop\Module\PsEventbus\Repository\LanguageRepository' - - PrestaShop\Module\PsEventbus\Service\ShopContent\ManufacturersService: - class: PrestaShop\Module\PsEventbus\Service\ShopContent\ManufacturersService - public: true - arguments: - - '@PrestaShop\Module\PsEventbus\Repository\ManufacturerRepository' - - PrestaShop\Module\PsEventbus\Service\ShopContent\ModulesService: - class: PrestaShop\Module\PsEventbus\Service\ShopContent\ModulesService - public: true - arguments: - - '@PrestaShop\Module\PsEventbus\Repository\ModuleRepository' - - '@PrestaShop\Module\PsEventbus\Repository\InfoRepository' - - PrestaShop\Module\PsEventbus\Service\ShopContent\OrdersService: - class: PrestaShop\Module\PsEventbus\Service\ShopContent\OrdersService - public: true - arguments: - - '@PrestaShop\Module\PsEventbus\Repository\OrderRepository' - - '@PrestaShop\Module\PsEventbus\Repository\OrderstatusHistoryRepository' - - '@PrestaShop\Module\PsEventbus\Formatter\ArrayFormatter' - - PrestaShop\Module\PsEventbus\Service\ShopContent\OrderCartRulesService: - class: PrestaShop\Module\PsEventbus\Service\ShopContent\OrderCartRulesService - public: true - arguments: - - '@PrestaShop\Module\PsEventbus\Repository\OrderCartRuleRepository' - - PrestaShop\Module\PsEventbus\Service\ShopContent\OrderDetailsService: - class: PrestaShop\Module\PsEventbus\Service\ShopContent\OrderDetailsService - public: true - arguments: - - '@PrestaShop\Module\PsEventbus\Repository\OrderDetailRepository' - - PrestaShop\Module\PsEventbus\Service\ShopContent\OrderStatusHistoryService: - class: PrestaShop\Module\PsEventbus\Service\ShopContent\OrderStatusHistoryService - public: true - arguments: - - '@PrestaShop\Module\PsEventbus\Repository\OrderstatusHistoryRepository' - - PrestaShop\Module\PsEventbus\Service\ShopContent\ProductsService: - class: PrestaShop\Module\PsEventbus\Service\ShopContent\ProductsService - public: true - arguments: - - '@PrestaShop\Module\PsEventbus\Repository\ProductRepository' - - '@PrestaShop\Module\PsEventbus\Service\ShopContent\LanguagesService' - - '@PrestaShop\Module\PsEventbus\Service\ShopContent\CategoriesService' - - '@PrestaShop\Module\PsEventbus\Formatter\ArrayFormatter' - - PrestaShop\Module\PsEventbus\Service\ShopContent\ProductSuppliersService: - class: PrestaShop\Module\PsEventbus\Service\ShopContent\ProductSuppliersService - public: true - arguments: - - '@PrestaShop\Module\PsEventbus\Repository\ProductSupplierRepository' - - PrestaShop\Module\PsEventbus\Service\ShopContent\InfoService: - class: PrestaShop\Module\PsEventbus\Service\ShopContent\InfoService - public: true - arguments: - - '@=service("ps_eventbus").getContext()' - - '@PrestaShop\Module\PsEventbus\Repository\InfoRepository' - - '@PrestaShop\Module\PsEventbus\Service\ShopContent\LanguagesService' - - '@PrestaShop\Module\PsEventbus\Service\ShopContent\CurrenciesService' - - PrestaShop\Module\PsEventbus\Service\ShopContent\SpecificPricesService: - class: PrestaShop\Module\PsEventbus\Service\ShopContent\SpecificPricesService - public: true - arguments: - - '@PrestaShop\Module\PsEventbus\Repository\SpecificPriceRepository' - - '@PrestaShop\Module\PsEventbus\Repository\ProductRepository' - - PrestaShop\Module\PsEventbus\Service\ShopContent\StocksService: - class: PrestaShop\Module\PsEventbus\Service\ShopContent\StocksService - public: true - arguments: - - '@PrestaShop\Module\PsEventbus\Repository\StockRepository' - - PrestaShop\Module\PsEventbus\Service\ShopContent\StockMovementsService: - class: PrestaShop\Module\PsEventbus\Service\ShopContent\StockMovementsService - public: true - arguments: - - '@PrestaShop\Module\PsEventbus\Repository\StockMovementRepository' - - PrestaShop\Module\PsEventbus\Service\ShopContent\StoresService: - class: PrestaShop\Module\PsEventbus\Service\ShopContent\StoresService - public: true - arguments: - - '@PrestaShop\Module\PsEventbus\Repository\StoreRepository' - - PrestaShop\Module\PsEventbus\Service\ShopContent\SuppliersService: - class: PrestaShop\Module\PsEventbus\Service\ShopContent\SuppliersService - public: true - arguments: - - '@PrestaShop\Module\PsEventbus\Repository\SupplierRepository' - - PrestaShop\Module\PsEventbus\Service\ShopContent\TaxonomiesService: - class: PrestaShop\Module\PsEventbus\Service\ShopContent\TaxonomiesService - public: true - arguments: - - '@PrestaShop\Module\PsEventbus\Repository\TaxonomyRepository' - - PrestaShop\Module\PsEventbus\Service\ShopContent\ThemesService: - class: PrestaShop\Module\PsEventbus\Service\ShopContent\ThemesService - public: true - arguments: - - '@=service("ps_eventbus").getContext()' - - PrestaShop\Module\PsEventbus\Service\ShopContent\TranslationsService: - class: PrestaShop\Module\PsEventbus\Service\ShopContent\TranslationsService - public: true - arguments: - - '@PrestaShop\Module\PsEventbus\Repository\TranslationRepository' - - PrestaShop\Module\PsEventbus\Service\ShopContent\WishlistsService: - class: PrestaShop\Module\PsEventbus\Service\ShopContent\WishlistsService - public: true - arguments: - - '@PrestaShop\Module\PsEventbus\Repository\WishlistRepository' - - PrestaShop\Module\PsEventbus\Service\ShopContent\WishlistProductsService: - class: PrestaShop\Module\PsEventbus\Service\ShopContent\WishlistProductsService - public: true - arguments: - - '@PrestaShop\Module\PsEventbus\Repository\WishlistProductRepository' diff --git a/config/parameters.yml b/config/parameters.yml deleted file mode 100644 index a4873285..00000000 --- a/config/parameters.yml +++ /dev/null @@ -1,7 +0,0 @@ -# Note: this file defaults to the E2E env, it will be overridden on zip build -parameters: - ps_eventbus.proxy_api_url: "http://reverse-proxy/collector" - ps_eventbus.sync_api_url: "http://reverse-proxy/sync-api" - ps_eventbus.live_sync_api_url: "http://reverse-proxy/live-sync-api/v1" - ps_eventbus.sentry_dsn: "https://sentry-id@stuff.ingest.sentry.io/stuff" - ps_eventbus.sentry_env: "development" diff --git a/controllers/front/apiHealthCheck.php b/controllers/front/apiHealthCheck.php index 867364b8..1d4a5216 100644 --- a/controllers/front/apiHealthCheck.php +++ b/controllers/front/apiHealthCheck.php @@ -47,7 +47,7 @@ public function postProcess() $module = Module::getInstanceByName('ps_eventbus'); /** @var ApiHealthCheckService $apiHealthCheckService */ - $apiHealthCheckService = $module->getService('PrestaShop\Module\PsEventbus\Service\ApiHealthCheckService'); + $apiHealthCheckService = $module->getService(ApiHealthCheckService::class); $response = $apiHealthCheckService->getHealthCheck($jobId); CommonService::exitWithResponse($response); diff --git a/controllers/front/apiShopContent.php b/controllers/front/apiShopContent.php index b44e5dbb..abb65320 100644 --- a/controllers/front/apiShopContent.php +++ b/controllers/front/apiShopContent.php @@ -67,7 +67,7 @@ public function postProcess() $module = Module::getInstanceByName('ps_eventbus'); /** @var ApiShopContentService $apiShopContentService */ - $apiShopContentService = $module->getService('PrestaShop\Module\PsEventbus\Service\ApiShopContentService'); + $apiShopContentService = $module->getService(ApiShopContentService::class); // Define our 3 constants here to be retrieved later in the application (errorHandler, repositories) if (!defined('PS_EVENTBUS_EXPLAIN_SQL_ENABLED')) { diff --git a/ps_eventbus.php b/ps_eventbus.php index 236d0fbc..63d13280 100644 --- a/ps_eventbus.php +++ b/ps_eventbus.php @@ -58,7 +58,7 @@ class Ps_eventbus extends Module /** * @var PrestaShop\Module\PsEventbus\DependencyInjection\ServiceContainer */ - private $serviceContainer; + private $container; /** * @var int the unique shop identifier (uuid v4) @@ -169,40 +169,19 @@ public function uninstall() } /** - * @return string - */ - public function getModuleEnvVar() - { - return strtoupper((string) $this->name) . '_ENV'; - } - - /** - * @param string $default - * - * @return string - */ - public function getModuleEnv($default = null) - { - return getenv($this->getModuleEnvVar()) ?: $default ?: self::DEFAULT_ENV; - } - - /** - * @return PrestaShop\Module\PsEventbus\DependencyInjection\ServiceContainer + * @return ServiceContainer * * @throws Exception */ public function getServiceContainer() { - if (null === $this->serviceContainer) { - // append version number to force cache generation (1.6 Core won't clear it) - $this->serviceContainer = new PrestaShop\Module\PsEventbus\DependencyInjection\ServiceContainer( - $this->name . str_replace(['.', '-', '+'], '', $this->version), - $this->getLocalPath(), - $this->getModuleEnv() + if (null === $this->container) { + $this->container = ServiceContainer::createInstance( + __DIR__ . '/config.php' ); } - return $this->serviceContainer; + return $this->container; } /** @@ -215,15 +194,15 @@ public function getServiceContainer() */ public function getService($serviceName) { - try { - return $this->getServiceContainer()->getService($serviceName); - } catch (Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException $exception) { - if (method_exists($this, 'get')) { - return $this->get($serviceName); - } - - throw new Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException($serviceName); - } + return $this->getServiceContainer()->getService($serviceName); + } + + /** + * @return Monolog\Logger + */ + public function getLogger() + { + return $this->getService('ps_eventbus.logger'); } /** diff --git a/src/DependencyInjection/ContainerProvider.php b/src/DependencyInjection/ContainerProvider.php deleted file mode 100644 index ebad03ef..00000000 --- a/src/DependencyInjection/ContainerProvider.php +++ /dev/null @@ -1,131 +0,0 @@ - - * @copyright Since 2007 PrestaShop SA and Contributors - * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) - */ - -namespace PrestaShop\Module\PsEventbus\DependencyInjection; - -use PrestaShop\ModuleLibCacheDirectoryProvider\Cache\CacheDirectoryProvider; -use Symfony\Component\Config\ConfigCache; -use Symfony\Component\Config\FileLocator; -use Symfony\Component\DependencyInjection\ContainerBuilder; -use Symfony\Component\DependencyInjection\ContainerInterface; -use Symfony\Component\DependencyInjection\Dumper\PhpDumper; -use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; - -if (!defined('_PS_VERSION_')) { - exit; -} - -class ContainerProvider -{ - /** - * @var string Module Name - */ - private $moduleName; - - /** - * @var string Module Local Path - */ - private $moduleLocalPath; - - /** - * @var string - */ - private $moduleEnv; - - /** - * @var CacheDirectoryProvider - */ - private $cacheDirectory; - - /** - * @param string $moduleName - * @param string $moduleLocalPath - * @param string $moduleEnv - * @param CacheDirectoryProvider $cacheDirectory - */ - public function __construct( - $moduleName, - $moduleLocalPath, - $moduleEnv, - CacheDirectoryProvider $cacheDirectory - ) { - $this->moduleName = $moduleName; - $this->moduleLocalPath = $moduleLocalPath; - $this->moduleEnv = $moduleEnv; - $this->cacheDirectory = $cacheDirectory; - } - - /** - * @param string $containerName - * - * @return ContainerInterface - * - * @throws \Exception - */ - public function get($containerName) - { - $containerClassName = ucfirst($this->moduleName) - . ucfirst($containerName) - . 'Container' - ; - $containerFilePath = $this->cacheDirectory->getPath() . '/' . $containerClassName . '.php'; - $containerConfigCache = new ConfigCache($containerFilePath, _PS_MODE_DEV_); - - if ($containerConfigCache->isFresh()) { - require_once $containerFilePath; - - /** @var ContainerInterface $containerClass */ - $containerClass = new $containerClassName(); - - return $containerClass; - } - - $containerBuilder = new ContainerBuilder(); - $containerBuilder->set( - $this->moduleName . '.cache.directory', - $this->cacheDirectory - ); - $moduleConfigPath = $this->moduleLocalPath - . 'config/' - . $containerName - ; - - $loader = new YamlFileLoader($containerBuilder, new FileLocator($moduleConfigPath)); - - $loader->load('services' . ($this->moduleEnv ? '_' . $this->moduleEnv : '') . '.yml'); - - $containerBuilder->compile(); - $dumper = new PhpDumper($containerBuilder); - $serviceContainerClass = $dumper->dump(['class' => $containerClassName]); - - $containerConfigCache->write( - is_array($serviceContainerClass) ? implode(' ', $serviceContainerClass) : $serviceContainerClass, - $containerBuilder->getResources() - ); - - return $containerBuilder; - } -} diff --git a/src/DependencyInjection/ServiceContainer.php b/src/DependencyInjection/ServiceContainer.php deleted file mode 100644 index 63d063d1..00000000 --- a/src/DependencyInjection/ServiceContainer.php +++ /dev/null @@ -1,130 +0,0 @@ - - * @copyright Since 2007 PrestaShop SA and Contributors - * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) - */ - -namespace PrestaShop\Module\PsEventbus\DependencyInjection; - -use PrestaShop\ModuleLibCacheDirectoryProvider\Cache\CacheDirectoryProvider; -use Symfony\Component\DependencyInjection\ContainerInterface; - -if (!defined('_PS_VERSION_')) { - exit; -} - -class ServiceContainer -{ - /** - * @var string Module Name - */ - private $moduleName; - - /** - * @var string Module Local Path - */ - private $moduleLocalPath; - - /** - * @var string - */ - private $moduleEnv; - - /** - * @var ContainerInterface - */ - private $container; - - /** - * @param string $moduleName - * @param string $moduleLocalPath - * @param string $moduleEnv - * - * @throws \Exception - */ - public function __construct($moduleName, $moduleLocalPath, $moduleEnv) - { - $this->moduleName = $moduleName; - $this->moduleLocalPath = $moduleLocalPath; - $this->moduleEnv = $moduleEnv; - - $this->initContainer(); - } - - /** - * @param string $serviceName - * - * @return object|null - * - * @throws \Exception - */ - public function getService($serviceName) - { - return $this->getContainer()->get($serviceName); - } - - /** - * @param string $serviceName - * - * @return bool - * - * @throws \Exception - */ - public function hasService($serviceName) - { - return $this->getContainer()->has($serviceName); - } - - /** - * @return ContainerInterface - * - * @throws \Exception - */ - public function getContainer() - { - if (null === $this->container) { - $this->initContainer(); - } - - return $this->container; - } - - /** - * Instantiate a new ContainerProvider - * - * @return void - * - * @throws \Exception - */ - private function initContainer() - { - $cacheDirectory = new CacheDirectoryProvider( - _PS_VERSION_, - _PS_ROOT_DIR_, - _PS_MODE_DEV_ - ); - $containerProvider = new ContainerProvider($this->moduleName, $this->moduleLocalPath, $this->moduleEnv, $cacheDirectory); - - $this->container = $containerProvider->get(defined('_PS_ADMIN_DIR_') || defined('PS_INSTALLATION_IN_PROGRESS') ? 'admin' : 'front'); - } -} diff --git a/src/Log/Logger.php b/src/Log/Logger.php new file mode 100644 index 00000000..8fa5d5ad --- /dev/null +++ b/src/Log/Logger.php @@ -0,0 +1,153 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 + */ + +namespace PrestaShop\Module\PsEventbus\Log; + +use Monolog\Handler\RotatingFileHandler; +use Monolog\Logger as MonologLogger; + +class Logger +{ + /** + * Detailed debug information + * + * @var string + */ + const DEBUG = 'DEBUG'; + + /** + * Interesting events + * + * @var string + */ + const INFO = 'INFO'; + + /** + * Uncommon events + * + * @var string + */ + const NOTICE = 'NOTICE'; + + /** + * Exceptional occurrences that are not errors + * + * @var string + */ + const WARNING = 'WARNING'; + + /** + * Runtime errors + * + * @var string + */ + const ERROR = 'ERROR'; + + /** + * Critical conditions + * + * @var string + */ + const CRITICAL = 'CRITICAL'; + + /** + * Action must be taken immediately + * + * @var string + */ + const ALERT = 'ALERT'; + + /** + * Urgent alert. + * + * @var string + */ + const EMERGENCY = 'EMERGENCY'; + + /** + * Number of files to rotate + * + * @var int + */ + const MAX_FILES = 15; + + /** + * @var int + */ + const DEFAULT_MONOLOG_LEVEL = MonologLogger::ERROR; + + /** + * @param string $level + * + * @return MonologLogger + */ + public static function create($level = '') + { + return (new MonologLogger('ps_eventbus'))->pushHandler( + new RotatingFileHandler( + self::getPath(), + static::MAX_FILES, + self::getMonologLevel($level) + ) + ); + } + + /** + * FIXME: misnamed method + * + * @return MonologLogger + */ + public static function getInstance() + { + /** @var \Ps_eventbus $psEventbus */ + $psEventbus = \Module::getInstanceByName('ps_eventbus'); + + return $psEventbus->getLogger(); + } + + /** + * @return string + */ + protected static function getPath() + { + $path = _PS_ROOT_DIR_ . '/var/logs/ps_eventbus'; + if (version_compare(_PS_VERSION_, '1.7', '<')) { + $path = _PS_ROOT_DIR_ . '/log/ps_eventbus'; + } elseif (version_compare(_PS_VERSION_, '1.7.4', '<')) { + $path = _PS_ROOT_DIR_ . '/app/logs/ps_eventbus'; + } + + return $path; + } + + /** + * @param string $level + * @param int $default + * + * @return int + */ + protected static function getMonologLevel($level, $default = self::DEFAULT_MONOLOG_LEVEL) + { + $logLevel = MonologLogger::toMonologLevel($level); + + /* @phpstan-ignore-next-line */ + return is_int($logLevel) ? $logLevel : $default; + } +} diff --git a/src/Service/ApiShopContentService.php b/src/Service/ApiShopContentService.php index a1a1a28f..60f858fa 100644 --- a/src/Service/ApiShopContentService.php +++ b/src/Service/ApiShopContentService.php @@ -59,10 +59,10 @@ class ApiShopContentService public function __construct( \Ps_eventbus $module, - ErrorHandler $errorHandler, ApiAuthorizationService $apiAuthorizationService, SynchronizationService $synchronizationService, - SyncRepository $syncRepository + SyncRepository $syncRepository, + ErrorHandler $errorHandler ) { $this->startTime = time(); @@ -98,7 +98,7 @@ public function handleDataSync($shopContent, $jobId, $langIso, $limit, $fullSync $response = []; /** @var LanguagesService $languagesService */ - $languagesService = $this->module->getService('PrestaShop\Module\PsEventbus\Service\ShopContent\LanguagesService'); + $languagesService = $this->module->getService(LanguagesService::class); $timezone = (string) \Configuration::get('PS_TIMEZONE'); $dateNow = (new \DateTime('now', new \DateTimeZone($timezone)))->format(Config::MYSQL_DATE_FORMAT); @@ -114,7 +114,7 @@ public function handleDataSync($shopContent, $jobId, $langIso, $limit, $fullSync if ($typeSync) { /** @var IncrementalSyncRepository $incrementalSyncRepository */ - $incrementalSyncRepository = $this->module->getService('PrestaShop\Module\PsEventbus\Repository\IncrementalSyncRepository'); + $incrementalSyncRepository = $this->module->getService(IncrementalSyncRepository::class); $incrementalSyncRepository->removeIncrementaSyncObjectByType($shopContent); } diff --git a/src/Service/PresenterService.php b/src/Service/PresenterService.php index 832d8c51..63983ab9 100644 --- a/src/Service/PresenterService.php +++ b/src/Service/PresenterService.php @@ -39,8 +39,9 @@ class PresenterService public function __construct() { + /** @var \Ps_eventbus */ $psEventbus = \Module::getInstanceByName('ps_eventbus'); - $psAccountsAdapterService = $psEventbus->getService('PrestaShop\Module\PsEventbus\Service\PsAccountsAdapterService'); + $psAccountsAdapterService = $psEventbus->getService(PsAccountsAdapterService::class); $this->psAccountsAdapterService = $psAccountsAdapterService; } diff --git a/src/Service/SynchronizationService.php b/src/Service/SynchronizationService.php index f238a72d..49374d51 100644 --- a/src/Service/SynchronizationService.php +++ b/src/Service/SynchronizationService.php @@ -34,7 +34,7 @@ use PrestaShop\Module\PsEventbus\Repository\SyncRepository; use PrestaShop\Module\PsEventbus\Service\ShopContent\LanguagesService; use PrestaShop\Module\PsEventbus\Service\ShopContent\ShopContentServiceInterface; -use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; +use PrestaShop\Module\PsEventbus\ServiceContainer\Exception\ServiceNotFoundException; if (!defined('_PS_VERSION_')) { exit; @@ -93,6 +93,8 @@ public function __construct( $this->languagesService = $languagesService; $this->proxyService = $proxyService; $this->errorHandler = $errorHandler; + + } /** @@ -125,9 +127,9 @@ public function sendFullSync( /** @var \Ps_eventbus */ $module = \Module::getInstanceByName('ps_eventbus'); - if (!$module->hasService($serviceId)) { + /* if (!$module->hasService($serviceId)) { throw new ServiceNotFoundException($serviceId); - } + } */ /** @var ShopContentServiceInterface $shopContentApiService */ $shopContentApiService = $module->getService($serviceId); diff --git a/src/ServiceContainer/Contract/IServiceProvider.php b/src/ServiceContainer/Contract/IServiceProvider.php new file mode 100644 index 00000000..cceee0a1 --- /dev/null +++ b/src/ServiceContainer/Contract/IServiceProvider.php @@ -0,0 +1,33 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 + */ + +namespace PrestaShop\Module\PsEventbus\ServiceContainer\Contract; + +use PrestaShop\Module\PsEventbus\ServiceContainer\ServiceContainer; + +interface IServiceProvider +{ + /** + * @param ServiceContainer $container + * + * @return void + */ + public function provide(ServiceContainer $container); +} diff --git a/src/ServiceContainer/Contract/ISingletonService.php b/src/ServiceContainer/Contract/ISingletonService.php new file mode 100644 index 00000000..62c2d629 --- /dev/null +++ b/src/ServiceContainer/Contract/ISingletonService.php @@ -0,0 +1,33 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 + */ + +namespace PrestaShop\Module\PsEventbus\ServiceContainer\Contract; + +use PrestaShop\Module\PsEventbus\ServiceContainer\ServiceContainer; + +interface ISingletonService +{ + /** + * @param ServiceContainer $serviceContainer + * + * @return mixed + */ + public static function getInstance(ServiceContainer $serviceContainer); +} diff --git a/src/ServiceContainer/Exception/ParameterNotFoundException.php b/src/ServiceContainer/Exception/ParameterNotFoundException.php new file mode 100644 index 00000000..76e4bdbc --- /dev/null +++ b/src/ServiceContainer/Exception/ParameterNotFoundException.php @@ -0,0 +1,25 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 + */ + +namespace PrestaShop\Module\PsEventbus\ServiceContainer\Exception; + +class ParameterNotFoundException extends \Exception +{ +} diff --git a/src/ServiceContainer/Exception/ProviderNotFoundException.php b/src/ServiceContainer/Exception/ProviderNotFoundException.php new file mode 100644 index 00000000..52c4e039 --- /dev/null +++ b/src/ServiceContainer/Exception/ProviderNotFoundException.php @@ -0,0 +1,25 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 + */ + +namespace PrestaShop\Module\PsEventbus\ServiceContainer\Exception; + +class ProviderNotFoundException extends \Exception +{ +} diff --git a/src/ServiceContainer/Exception/ServiceNotFoundException.php b/src/ServiceContainer/Exception/ServiceNotFoundException.php new file mode 100644 index 00000000..52c42283 --- /dev/null +++ b/src/ServiceContainer/Exception/ServiceNotFoundException.php @@ -0,0 +1,25 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 + */ + +namespace PrestaShop\Module\PsEventbus\ServiceContainer\Exception; + +class ServiceNotFoundException extends \Exception +{ +} diff --git a/src/ServiceContainer/Provider/ApiProvider.php b/src/ServiceContainer/Provider/ApiProvider.php new file mode 100644 index 00000000..451624ef --- /dev/null +++ b/src/ServiceContainer/Provider/ApiProvider.php @@ -0,0 +1,61 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 + */ + +namespace PrestaShop\Module\PsEventbus\ServiceContainer\Provider; + +use PrestaShop\Module\PsEventbus\Api\CollectorApiClient; +use PrestaShop\Module\PsEventbus\Api\LiveSyncApiClient; +use PrestaShop\Module\PsEventbus\Api\SyncApiClient; +use PrestaShop\Module\PsEventbus\Service\PsAccountsAdapterService; +use PrestaShop\Module\PsEventbus\ServiceContainer\Contract\IServiceProvider; +use PrestaShop\Module\PsEventbus\ServiceContainer\ServiceContainer; + +class ApiProvider implements IServiceProvider +{ + /** + * @param ServiceContainer $container + * + * @return void + */ + public function provide(ServiceContainer $container) + { + $container->registerProvider(SyncApiClient::class, static function () use ($container) { + return new SyncApiClient( + $container->getParameter('ps_eventbus.sync_api_url'), + $container->get('ps_eventbus.module'), + $container->get(PsAccountsAdapterService::class) + ); + }); + $container->registerProvider(LiveSyncApiClient::class, static function () use ($container) { + return new LiveSyncApiClient( + $container->getParameter('ps_eventbus.live_sync_api_url'), + $container->get('ps_eventbus.module'), + $container->get(PsAccountsAdapterService::class) + ); + }); + $container->registerProvider(CollectorApiClient::class, static function () use ($container) { + return new CollectorApiClient( + $container->getParameter('ps_eventbus.proxy_api_url'), + $container->get('ps_eventbus.module'), + $container->get(PsAccountsAdapterService::class) + ); + }); + } +} diff --git a/src/ServiceContainer/Provider/CommonProvider.php b/src/ServiceContainer/Provider/CommonProvider.php new file mode 100644 index 00000000..fc013681 --- /dev/null +++ b/src/ServiceContainer/Provider/CommonProvider.php @@ -0,0 +1,83 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 + */ + +namespace PrestaShop\Module\PsEventbus\ServiceContainer\Provider; + +use PrestaShop\Module\PsEventbus\Api\CollectorApiClient; +use PrestaShop\Module\PsEventbus\Formatter\ArrayFormatter; +use PrestaShop\Module\PsEventbus\Formatter\JsonFormatter; +use PrestaShop\Module\PsEventbus\Handler\ErrorHandler\ErrorHandler; +use PrestaShop\Module\PsEventbus\Helper\ModuleHelper; +use PrestaShop\Module\PsEventbus\Service\PresenterService; +use PrestaShop\Module\PsEventbus\Service\ProxyService; +use PrestaShop\Module\PsEventbus\Service\PsAccountsAdapterService; +use PrestaShop\Module\PsEventbus\ServiceContainer\Contract\IServiceProvider; +use PrestaShop\Module\PsEventbus\ServiceContainer\ServiceContainer; + +class CommonProvider implements IServiceProvider +{ + /** + * @param ServiceContainer $container + * + * @return void + */ + public function provide(ServiceContainer $container) + { + $container->registerProvider('ps_eventbus.context', static function () { + return \Context::getContext(); + }); + $container->registerProvider('ps_eventbus.module', static function () { + return \Module::getInstanceByName('ps_eventbus'); + }); + $container->registerProvider(ModuleHelper::class, static function () { + return new ModuleHelper(); + }); + $container->registerProvider(PsAccountsAdapterService::class, static function () use ($container) { + return new PsAccountsAdapterService( + $container->get(ModuleHelper::class) + ); + }); + $container->registerProvider(JsonFormatter::class, static function () { + return new JsonFormatter(); + }); + $container->registerProvider(ArrayFormatter::class, static function () { + return new ArrayFormatter(); + }); + $container->registerProvider(ProxyService::class, static function () use ($container) { + return new ProxyService( + $container->get(CollectorApiClient::class), + $container->get(JsonFormatter::class), + $container->get(ErrorHandler::class) + ); + }); + $container->registerProvider(ErrorHandler::class, static function () use ($container) { + return new ErrorHandler( + $container->get('ps_eventbus.module'), + $container->get(PsAccountsAdapterService::class), + $container->getParameter('ps_eventbus.sentry_dsn'), + $container->getParameter('ps_eventbus.sentry_env') + ); + }); + + $container->registerProvider(PresenterService::class, static function () { + return new PresenterService(); + }); + } +} diff --git a/src/ServiceContainer/Provider/RepositoryProvider.php b/src/ServiceContainer/Provider/RepositoryProvider.php new file mode 100644 index 00000000..6ddd9d93 --- /dev/null +++ b/src/ServiceContainer/Provider/RepositoryProvider.php @@ -0,0 +1,183 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 + */ + +namespace PrestaShop\Module\PsEventbus\ServiceContainer\Provider; + +use PrestaShop\Module\PsEventbus\Handler\ErrorHandler\ErrorHandler; +use PrestaShop\Module\PsEventbus\Repository\BundleRepository; +use PrestaShop\Module\PsEventbus\Repository\CarrierDetailRepository; +use PrestaShop\Module\PsEventbus\Repository\CarrierRepository; +use PrestaShop\Module\PsEventbus\Repository\CarrierTaxeRepository; +use PrestaShop\Module\PsEventbus\Repository\CartProductRepository; +use PrestaShop\Module\PsEventbus\Repository\CartRepository; +use PrestaShop\Module\PsEventbus\Repository\CartRuleRepository; +use PrestaShop\Module\PsEventbus\Repository\CategoryRepository; +use PrestaShop\Module\PsEventbus\Repository\CurrencyRepository; +use PrestaShop\Module\PsEventbus\Repository\CustomerRepository; +use PrestaShop\Module\PsEventbus\Repository\CustomProductCarrierRepository; +use PrestaShop\Module\PsEventbus\Repository\EmployeeRepository; +use PrestaShop\Module\PsEventbus\Repository\ImageRepository; +use PrestaShop\Module\PsEventbus\Repository\ImageTypeRepository; +use PrestaShop\Module\PsEventbus\Repository\IncrementalSyncRepository; +use PrestaShop\Module\PsEventbus\Repository\LanguageRepository; +use PrestaShop\Module\PsEventbus\Repository\LiveSyncRepository; +use PrestaShop\Module\PsEventbus\Repository\ManufacturerRepository; +use PrestaShop\Module\PsEventbus\Repository\ModuleRepository; +use PrestaShop\Module\PsEventbus\Repository\OrderCartRuleRepository; +use PrestaShop\Module\PsEventbus\Repository\OrderDetailRepository; +use PrestaShop\Module\PsEventbus\Repository\OrderRepository; +use PrestaShop\Module\PsEventbus\Repository\OrderStatusHistoryRepository; +use PrestaShop\Module\PsEventbus\Repository\ProductRepository; +use PrestaShop\Module\PsEventbus\Repository\ProductSupplierRepository; +use PrestaShop\Module\PsEventbus\Repository\InfoRepository; +use PrestaShop\Module\PsEventbus\Repository\SpecificPriceRepository; +use PrestaShop\Module\PsEventbus\Repository\StockMovementRepository; +use PrestaShop\Module\PsEventbus\Repository\StockRepository; +use PrestaShop\Module\PsEventbus\Repository\StoreRepository; +use PrestaShop\Module\PsEventbus\Repository\SupplierRepository; +use PrestaShop\Module\PsEventbus\Repository\SyncRepository; +use PrestaShop\Module\PsEventbus\Repository\TaxonomyRepository; +use PrestaShop\Module\PsEventbus\Repository\TranslationRepository; +use PrestaShop\Module\PsEventbus\Repository\WishlistProductRepository; +use PrestaShop\Module\PsEventbus\Repository\WishlistRepository; +use PrestaShop\Module\PsEventbus\ServiceContainer\Contract\IServiceProvider; +use PrestaShop\Module\PsEventbus\ServiceContainer\ServiceContainer; + +class RepositoryProvider implements IServiceProvider +{ + /** + * @param ServiceContainer $container + * + * @return void + */ + public function provide(ServiceContainer $container) + { + $container->registerProvider(IncrementalSyncRepository::class, static function () use ($container) { + return new IncrementalSyncRepository( + $container->get(ErrorHandler::class) + ); + }); + $container->registerProvider(SyncRepository::class, static function () { + return new SyncRepository(); + }); + $container->registerProvider(LiveSyncRepository::class, static function () { + return new LiveSyncRepository(); + }); + $container->registerProvider(BundleRepository::class, static function () { + return new BundleRepository(); + }); + $container->registerProvider(CarrierRepository::class, static function () { + return new CarrierRepository(); + }); + $container->registerProvider(CarrierDetailRepository::class, static function () { + return new CarrierDetailRepository(); + }); + $container->registerProvider(CarrierTaxeRepository::class, static function () { + return new CarrierTaxeRepository(); + }); + $container->registerProvider(CartRepository::class, static function () { + return new CartRepository(); + }); + $container->registerProvider(CartProductRepository::class, static function () { + return new CartProductRepository(); + }); + $container->registerProvider(CartRuleRepository::class, static function () { + return new CartRuleRepository(); + }); + $container->registerProvider(CustomProductCarrierRepository::class, static function () { + return new CustomProductCarrierRepository(); + }); + $container->registerProvider(CategoryRepository::class, static function () { + return new CategoryRepository(); + }); + $container->registerProvider(CustomerRepository::class, static function () { + return new CustomerRepository(); + }); + $container->registerProvider(CurrencyRepository::class, static function () { + return new CurrencyRepository(); + }); + $container->registerProvider(EmployeeRepository::class, static function () { + return new EmployeeRepository(); + }); + $container->registerProvider(ImageRepository::class, static function () { + return new ImageRepository(); + }); + $container->registerProvider(ImageTypeRepository::class, static function () { + return new ImageTypeRepository(); + }); + $container->registerProvider(ModuleRepository::class, static function () { + return new ModuleRepository(); + }); + $container->registerProvider(LanguageRepository::class, static function () { + return new LanguageRepository(); + }); + $container->registerProvider(ManufacturerRepository::class, static function () { + return new ManufacturerRepository(); + }); + $container->registerProvider(OrderRepository::class, static function () { + return new OrderRepository(); + }); + $container->registerProvider(OrderCartRuleRepository::class, static function () { + return new OrderCartRuleRepository(); + }); + $container->registerProvider(OrderStatusHistoryRepository::class, static function () { + return new OrderStatusHistoryRepository(); + }); + $container->registerProvider(OrderDetailRepository::class, static function () { + return new OrderDetailRepository(); + }); + $container->registerProvider(ProductRepository::class, static function () { + return new ProductRepository(); + }); + $container->registerProvider(ProductSupplierRepository::class, static function () { + return new ProductSupplierRepository(); + }); + $container->registerProvider(InfoRepository::class, static function () { + return new InfoRepository(); + }); + $container->registerProvider(StockRepository::class, static function () { + return new StockRepository(); + }); + $container->registerProvider(StockMovementRepository::class, static function () { + return new StockMovementRepository(); + }); + $container->registerProvider(SpecificPriceRepository::class, static function () { + return new SpecificPriceRepository(); + }); + $container->registerProvider(SupplierRepository::class, static function () { + return new SupplierRepository(); + }); + $container->registerProvider(StoreRepository::class, static function () { + return new StoreRepository(); + }); + $container->registerProvider(TaxonomyRepository::class, static function () { + return new TaxonomyRepository(); + }); + $container->registerProvider(TranslationRepository::class, static function () { + return new TranslationRepository(); + }); + $container->registerProvider(WishlistRepository::class, static function () { + return new WishlistRepository(); + }); + $container->registerProvider(WishlistProductRepository::class, static function () { + return new WishlistProductRepository(); + }); + } +} diff --git a/src/ServiceContainer/Provider/ServiceProvider.php b/src/ServiceContainer/Provider/ServiceProvider.php new file mode 100644 index 00000000..16101986 --- /dev/null +++ b/src/ServiceContainer/Provider/ServiceProvider.php @@ -0,0 +1,339 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 + */ + +namespace PrestaShop\Module\PsEventbus\ServiceContainer\Provider; + +use PrestaShop\Module\PsEventbus\Api\LiveSyncApiClient; +use PrestaShop\Module\PsEventbus\Api\SyncApiClient; +use PrestaShop\Module\PsEventbus\Formatter\ArrayFormatter; +use PrestaShop\Module\PsEventbus\Handler\ErrorHandler\ErrorHandler; +use PrestaShop\Module\PsEventbus\Repository\BundleRepository; +use PrestaShop\Module\PsEventbus\Repository\CarrierDetailRepository; +use PrestaShop\Module\PsEventbus\Repository\CarrierRepository; +use PrestaShop\Module\PsEventbus\Repository\CarrierTaxeRepository; +use PrestaShop\Module\PsEventbus\Repository\CartProductRepository; +use PrestaShop\Module\PsEventbus\Repository\CartRepository; +use PrestaShop\Module\PsEventbus\Repository\CartRuleRepository; +use PrestaShop\Module\PsEventbus\Repository\CategoryRepository; +use PrestaShop\Module\PsEventbus\Repository\CurrencyRepository; +use PrestaShop\Module\PsEventbus\Repository\CustomerRepository; +use PrestaShop\Module\PsEventbus\Repository\CustomProductCarrierRepository; +use PrestaShop\Module\PsEventbus\Repository\EmployeeRepository; +use PrestaShop\Module\PsEventbus\Repository\ImageRepository; +use PrestaShop\Module\PsEventbus\Repository\ImageTypeRepository; +use PrestaShop\Module\PsEventbus\Repository\IncrementalSyncRepository; +use PrestaShop\Module\PsEventbus\Repository\LanguageRepository; +use PrestaShop\Module\PsEventbus\Repository\LiveSyncRepository; +use PrestaShop\Module\PsEventbus\Repository\ManufacturerRepository; +use PrestaShop\Module\PsEventbus\Repository\ModuleRepository; +use PrestaShop\Module\PsEventbus\Repository\OrderCartRuleRepository; +use PrestaShop\Module\PsEventbus\Repository\OrderDetailRepository; +use PrestaShop\Module\PsEventbus\Repository\OrderRepository; +use PrestaShop\Module\PsEventbus\Repository\OrderStatusHistoryRepository; +use PrestaShop\Module\PsEventbus\Repository\ProductRepository; +use PrestaShop\Module\PsEventbus\Repository\ProductSupplierRepository; +use PrestaShop\Module\PsEventbus\Repository\InfoRepository; +use PrestaShop\Module\PsEventbus\Repository\SpecificPriceRepository; +use PrestaShop\Module\PsEventbus\Repository\StockMovementRepository; +use PrestaShop\Module\PsEventbus\Repository\StockRepository; +use PrestaShop\Module\PsEventbus\Repository\StoreRepository; +use PrestaShop\Module\PsEventbus\Repository\SupplierRepository; +use PrestaShop\Module\PsEventbus\Repository\SyncRepository; +use PrestaShop\Module\PsEventbus\Repository\TaxonomyRepository; +use PrestaShop\Module\PsEventbus\Repository\TranslationRepository; +use PrestaShop\Module\PsEventbus\Repository\WishlistProductRepository; +use PrestaShop\Module\PsEventbus\Repository\WishlistRepository; +use PrestaShop\Module\PsEventbus\Service\ApiAuthorizationService; +use PrestaShop\Module\PsEventbus\Service\ApiHealthCheckService; +use PrestaShop\Module\PsEventbus\Service\ApiShopContentService; +use PrestaShop\Module\PsEventbus\Service\PresenterService; +use PrestaShop\Module\PsEventbus\Service\ProxyService; +use PrestaShop\Module\PsEventbus\Service\PsAccountsAdapterService; +use PrestaShop\Module\PsEventbus\Service\ShopContent\BundlesService; +use PrestaShop\Module\PsEventbus\Service\ShopContent\CarrierDetailsService; +use PrestaShop\Module\PsEventbus\Service\ShopContent\CarriersService; +use PrestaShop\Module\PsEventbus\Service\ShopContent\CarrierTaxesService; +use PrestaShop\Module\PsEventbus\Service\ShopContent\CartProductsService; +use PrestaShop\Module\PsEventbus\Service\ShopContent\CartRulesService; +use PrestaShop\Module\PsEventbus\Service\ShopContent\CartsService; +use PrestaShop\Module\PsEventbus\Service\ShopContent\CategoriesService; +use PrestaShop\Module\PsEventbus\Service\ShopContent\CurrenciesService; +use PrestaShop\Module\PsEventbus\Service\ShopContent\CustomersService; +use PrestaShop\Module\PsEventbus\Service\ShopContent\CustomProductCarriersService; +use PrestaShop\Module\PsEventbus\Service\ShopContent\EmployeesService; +use PrestaShop\Module\PsEventbus\Service\ShopContent\ImagesService; +use PrestaShop\Module\PsEventbus\Service\ShopContent\ImageTypesService; +use PrestaShop\Module\PsEventbus\Service\ShopContent\InfoService; +use PrestaShop\Module\PsEventbus\Service\ShopContent\LanguagesService; +use PrestaShop\Module\PsEventbus\Service\ShopContent\ManufacturersService; +use PrestaShop\Module\PsEventbus\Service\ShopContent\ModulesService; +use PrestaShop\Module\PsEventbus\Service\ShopContent\OrderCartRulesService; +use PrestaShop\Module\PsEventbus\Service\ShopContent\OrderDetailsService; +use PrestaShop\Module\PsEventbus\Service\ShopContent\OrdersService; +use PrestaShop\Module\PsEventbus\Service\ShopContent\OrderStatusHistoryService; +use PrestaShop\Module\PsEventbus\Service\ShopContent\ProductsService; +use PrestaShop\Module\PsEventbus\Service\ShopContent\ProductSuppliersService; +use PrestaShop\Module\PsEventbus\Service\ShopContent\SpecificPricesService; +use PrestaShop\Module\PsEventbus\Service\ShopContent\StockMovementsService; +use PrestaShop\Module\PsEventbus\Service\ShopContent\StocksService; +use PrestaShop\Module\PsEventbus\Service\ShopContent\StoresService; +use PrestaShop\Module\PsEventbus\Service\ShopContent\SuppliersService; +use PrestaShop\Module\PsEventbus\Service\ShopContent\TaxonomiesService; +use PrestaShop\Module\PsEventbus\Service\ShopContent\ThemesService; +use PrestaShop\Module\PsEventbus\Service\ShopContent\TranslationsService; +use PrestaShop\Module\PsEventbus\Service\ShopContent\WishlistProductsService; +use PrestaShop\Module\PsEventbus\Service\ShopContent\WishlistsService; +use PrestaShop\Module\PsEventbus\Service\SynchronizationService; +use PrestaShop\Module\PsEventbus\ServiceContainer\Contract\IServiceProvider; +use PrestaShop\Module\PsEventbus\ServiceContainer\ServiceContainer; + +class ServiceProvider implements IServiceProvider +{ + /** + * @param ServiceContainer $container + * + * @return void + */ + public function provide(ServiceContainer $container) + { + $container->registerProvider(ApiAuthorizationService::class, static function () use ($container) { + return new ApiAuthorizationService( + $container->get(SyncRepository::class), + $container->get(SyncApiClient::class), + $container->get(PsAccountsAdapterService::class), + $container->get(ErrorHandler::class) + ); + }); + $container->registerProvider(SynchronizationService::class, static function () use ($container) { + return new SynchronizationService( + $container->get(LiveSyncApiClient::class), + $container->get(SyncRepository::class), + $container->get(IncrementalSyncRepository::class), + $container->get(LiveSyncRepository::class), + $container->get(LanguagesService::class), + $container->get(ProxyService::class), + $container->get(ErrorHandler::class) + ); + }); + $container->registerProvider(PresenterService::class, static function () { + return new PresenterService(); + }); + $container->registerProvider(ApiShopContentService::class, static function () use ($container) { + return new ApiShopContentService( + $container->get('ps_eventbus.module'), + $container->get(ApiAuthorizationService::class), + $container->get(SynchronizationService::class), + $container->get(SyncRepository::class), + $container->get(ErrorHandler::class) + ); + }); + $container->registerProvider(ApiHealthCheckService::class, static function () use ($container) { + return new ApiHealthCheckService( + $container->get(PsAccountsAdapterService::class), + $container->get(ApiAuthorizationService::class), + $container->get(ErrorHandler::class), + $container->getParameter('ps_eventbus.sync_api_url'), + $container->getParameter('ps_eventbus.live_sync_api_url'), + $container->getParameter('ps_eventbus.proxy_api_url') + ); + }); + + $container->registerProvider(BundlesService::class, static function () use ($container) { + return new BundlesService( + $container->get(BundleRepository::class) + ); + }); + $container->registerProvider(CarriersService::class, static function () use ($container) { + return new CarriersService( + $container->get(CarrierRepository::class) + ); + }); + $container->registerProvider(CarrierDetailsService::class, static function () use ($container) { + return new CarrierDetailsService( + $container->get(CarrierDetailRepository::class) + ); + }); + $container->registerProvider(CarrierTaxesService::class, static function () use ($container) { + return new CarrierTaxesService( + $container->get(CarrierTaxeRepository::class) + ); + }); + $container->registerProvider(CartsService::class, static function () use ($container) { + return new CartsService( + $container->get(CartRepository::class) + ); + }); + $container->registerProvider(CartProductsService::class, static function () use ($container) { + return new CartProductsService( + $container->get(CartProductRepository::class) + ); + }); + $container->registerProvider(CartRulesService::class, static function () use ($container) { + return new CartRulesService( + $container->get(CartRuleRepository::class) + ); + }); + $container->registerProvider(CustomProductCarriersService::class, static function () use ($container) { + return new CustomProductCarriersService( + $container->get(CustomProductCarrierRepository::class) + ); + }); + $container->registerProvider(CustomersService::class, static function () use ($container) { + return new CustomersService( + $container->get(CustomerRepository::class) + ); + }); + $container->registerProvider(CategoriesService::class, static function () use ($container) { + return new CategoriesService( + $container->get(CategoryRepository::class) + ); + }); + $container->registerProvider(CurrenciesService::class, static function () use ($container) { + return new CurrenciesService( + $container->get(CurrencyRepository::class) + ); + }); + $container->registerProvider(EmployeesService::class, static function () use ($container) { + return new EmployeesService( + $container->get(EmployeeRepository::class) + ); + }); + $container->registerProvider(ImagesService::class, static function () use ($container) { + return new ImagesService( + $container->get(ImageRepository::class) + ); + }); + $container->registerProvider(ImageTypesService::class, static function () use ($container) { + return new ImageTypesService( + $container->get(ImageTypeRepository::class) + ); + }); + $container->registerProvider(LanguagesService::class, static function () use ($container) { + return new LanguagesService( + $container->get(LanguageRepository::class) + ); + }); + $container->registerProvider(ManufacturersService::class, static function () use ($container) { + return new ManufacturersService( + $container->get(ManufacturerRepository::class) + ); + }); + $container->registerProvider(ModulesService::class, static function () use ($container) { + return new ModulesService( + $container->get(ModuleRepository::class), + $container->get(InfoRepository::class) + ); + }); + $container->registerProvider(OrdersService::class, static function () use ($container) { + return new OrdersService( + $container->get(OrderRepository::class), + $container->get(OrderStatusHistoryRepository::class), + $container->get(ArrayFormatter::class) + ); + }); + $container->registerProvider(OrderCartRulesService::class, static function () use ($container) { + return new OrderCartRulesService( + $container->get(OrderCartRuleRepository::class) + ); + }); + $container->registerProvider(OrderDetailsService::class, static function () use ($container) { + return new OrderDetailsService( + $container->get(OrderDetailRepository::class) + ); + }); + $container->registerProvider(OrderStatusHistoryService::class, static function () use ($container) { + return new OrderStatusHistoryService( + $container->get(OrderStatusHistoryRepository::class) + ); + }); + $container->registerProvider(ProductsService::class, static function () use ($container) { + return new ProductsService( + $container->get(ProductRepository::class), + $container->get(LanguagesService::class), + $container->get(CategoriesService::class), + $container->get(ArrayFormatter::class) + ); + }); + $container->registerProvider(ProductSuppliersService::class, static function () use ($container) { + return new ProductSuppliersService( + $container->get(ProductSupplierRepository::class) + ); + }); + $container->registerProvider(InfoService::class, static function () use ($container) { + return new InfoService( + $container->get('ps_eventbus.module'), + $container->get(InfoRepository::class), + $container->get(LanguagesService::class), + $container->get(CurrenciesService::class) + ); + }); + $container->registerProvider(SpecificPricesService::class, static function () use ($container) { + return new SpecificPricesService( + $container->get(SpecificPriceRepository::class), + $container->get(ProductRepository::class) + ); + }); + $container->registerProvider(StocksService::class, static function () use ($container) { + return new StocksService( + $container->get(StockRepository::class) + ); + }); + $container->registerProvider(StockMovementsService::class, static function () use ($container) { + return new StockMovementsService( + $container->get(StockMovementRepository::class) + ); + }); + $container->registerProvider(StoresService::class, static function () use ($container) { + return new StoresService( + $container->get(StoreRepository::class) + ); + }); + $container->registerProvider(SuppliersService::class, static function () use ($container) { + return new SuppliersService( + $container->get(SupplierRepository::class) + ); + }); + $container->registerProvider(TaxonomiesService::class, static function () use ($container) { + return new TaxonomiesService( + $container->get(TaxonomyRepository::class) + ); + }); + $container->registerProvider(ThemesService::class, static function () use ($container) { + return new ThemesService( + $container->get('ps_eventrbus.context') + ); + }); + $container->registerProvider(TranslationsService::class, static function () use ($container) { + return new TranslationsService( + $container->get(TranslationRepository::class) + ); + }); + $container->registerProvider(WishlistsService::class, static function () use ($container) { + return new WishlistsService( + $container->get(WishlistRepository::class) + ); + }); + $container->registerProvider(WishlistProductsService::class, static function () use ($container) { + return new WishlistProductsService( + $container->get(WishlistProductRepository::class) + ); + }); + } +} diff --git a/src/ServiceContainer/ServiceContainer.php b/src/ServiceContainer/ServiceContainer.php new file mode 100644 index 00000000..49f93b89 --- /dev/null +++ b/src/ServiceContainer/ServiceContainer.php @@ -0,0 +1,282 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 + */ + +namespace PrestaShop\Module\PsEventbus\ServiceContainer; + +use Monolog\Logger as MonologLogger; +use PrestaShop\Module\PsEventbus\Log\Logger; +use PrestaShop\Module\PsEventbus\ServiceContainer\Contract\IServiceProvider; +use PrestaShop\Module\PsEventbus\ServiceContainer\Contract\ISingletonService; +use PrestaShop\Module\PsEventbus\ServiceContainer\Exception\ParameterNotFoundException; +use PrestaShop\Module\PsEventbus\ServiceContainer\Exception\ProviderNotFoundException; +use PrestaShop\Module\PsEventbus\ServiceContainer\Exception\ServiceNotFoundException; + +class ServiceContainer +{ + /** + * @var string + */ + protected $configPath; + + /** + * @var array + */ + protected $config = []; + + /** + * @var array + */ + protected $services = []; + + /** + * @var \Closure[] + */ + protected $providers = []; + + /** + * @var string[] + */ + protected $provides = [ + Provider\ApiProvider::class, + Provider\CommonProvider::class, + Provider\RepositoryProvider::class, + Provider\ServiceProvider::class, + ]; + + /** + * @var MonologLogger + */ + private $logger; + + /** + * @param string $configPath + */ + public function __construct($configPath) + { + $this->configPath = $configPath; + } + + /** + * @param string $configPath + * + * @return ServiceContainer + */ + public static function createInstance($configPath) + { + $container = new ServiceContainer($configPath); + $container->loadConfig(); + $container->initLogger(); + $container->init(); + + return $container; + } + + /** + * @return void + */ + public function loadConfig() + { + $this->config = require_once $this->configPath; + } + + /** + * @return void + */ + public function init() + { + $this->logger->debug('Initializing service container'); + + foreach ($this->provides as $provider) { + if (is_a($provider, IServiceProvider::class, true)) { + $this->logger->debug('Initializing service provider ' . $provider); + + (new $provider())->provide($this); + } + } + } + + /** + * @param string $name + * + * @return mixed + * + * @throws ServiceNotFoundException + * @throws ProviderNotFoundException + */ + public function get($name) + { + if ($this->hasService($name)) { + return $this->services[$name]; + } + + if ($this->hasProvider($name)) { + $callback = $this->getProvider($name); + $service = $callback(); + } else { + $service = $this->provideInstanceFromClassname($name); + } + + if (null === $service) { + throw new ServiceNotFoundException('Service Not Found: ' . $name); + } + + $this->set($name, $service); + + $this->logger->debug('Service Loaded: ' . $name); + + return $service; + } + + /** + * @param string $name + * + * @return mixed + * + * @throws ServiceNotFoundException + * @throws ProviderNotFoundException + */ + public function getService($name) + { + return $this->get($name); + } + + /** + * @param mixed $name + * + * @return bool + */ + public function hasService($name) + { + return array_key_exists($name, $this->services); + } + + /** + * @param string $name + * @param mixed $value + * + * @return void + */ + public function set($name, $value) + { + $this->services[$name] = $value; + } + + /** + * @param string $name + * + * @return string + * + * @throws ParameterNotFoundException + */ + public function getParameter($name) + { + if (array_key_exists($name, $this->config)) { + return $this->config[$name]; + } + throw new ParameterNotFoundException('Configuration parameter "' . $name . '" not found.'); + } + + /** + * @param string $name + * @param string $default + * + * @return string + */ + public function getParameterWithDefault($name, $default) + { + if (array_key_exists($name, $this->config)) { + return $this->config[$name]; + } + + return $default; + } + + /** + * @param string $name + * + * @return bool + */ + public function hasParameter($name) + { + return array_key_exists($name, $this->config); + } + + /** + * @param string $name + * + * @return \Closure + * + * @throws ProviderNotFoundException + */ + public function getProvider($name) + { + if (array_key_exists($name, $this->providers)) { + return $this->providers[$name]; + } + throw new ProviderNotFoundException('Provider "' . $name . '" not found.'); + } + + /** + * @param string $name + * + * @return bool + */ + public function hasProvider($name) + { + return array_key_exists($name, $this->providers); + } + + /** + * @param string $name + * @param \Closure $provider + * + * @return void + */ + public function registerProvider($name, \Closure $provider) + { + $this->providers[$name] = $provider; + } + + /** + * @param string $className + * + * @return mixed + */ + protected function provideInstanceFromClassname($className) + { + if (is_a($className, ISingletonService::class, true)) { + return $className::getInstance($this); + } + + return null; + } + + /** + * @return void + */ + private function initLogger() + { + // early stage logger + $this->logger = Logger::create( + $this->getParameterWithDefault('ps_eventbus.log_level', Logger::ERROR) + ); + $this->set('ps_eventbus.logger', $this->logger); + } +}