目前我正在開發(fā)基于 Symfony 的 Shopware 6 擴(kuò)展。我不明白的是如何實(shí)現(xiàn)抽象類和依賴注入。
所以我希望能夠重構(gòu)代碼,并經(jīng)常使用這些方法,但在另一個(gè)上下文中(使用另一個(gè)存儲(chǔ)庫)
<?php declare(strict_types=1); namespace WShopService; use ShopwareCoreFrameworkContext; use ShopwareCoreFrameworkDataAbstractionLayerSearchCriteria; use ShopwareCoreFrameworkDataAbstractionLayerEntityRepository; use ShopwareCoreFrameworkDataAbstractionLayerSearchFilterEqualsFilter; use ShopwareCoreFrameworkUuidUuid; /** * Service for writing Products */ class ProductService { private EntityRepository $productRepository; private MediaImageService $mediaImageService; private EntityRepository $productMediaRepository; public function __construct( EntityRepository $productRepository, MediaImageService $mediaImageService, EntityRepository $productMediaRepository ) { $this->productRepository = $productRepository; $this->mediaImageService = $mediaImageService; $this->productMediaRepository = $productMediaRepository; } private function createProduct(array $data, Context $context = null): void { $context = $context ?? Context::createDefaultContext(); $this->productRepository->create([ $data ], $context); } public function updateProduct(array $data): void { $this->productRepository->update([$data], Context::createDefaultContext()); } public function getExistingProductId(string $productNumber): ?string { $criteria = new Criteria(); $criteria->addFilter(new EqualsFilter('productNumber', $productNumber)); return $this->productRepository->searchIds($criteria, Context::createDefaultContext())->firstId(); } }
如您所見,構(gòu)造(產(chǎn)品存儲(chǔ)庫)內(nèi)部存在依賴注入。現(xiàn)在我的問題是,我如何創(chuàng)建抽象類,即存儲(chǔ)這些方法,但子類將使用所需的存儲(chǔ)庫“重寫”父構(gòu)造?例如,我想在產(chǎn)品存儲(chǔ)庫上使用getDataId(現(xiàn)在稱為getExistingProductId,但將在抽象類中重構(gòu)并重命名)方法,但是對(duì)于下一堂課我想在類別存儲(chǔ)庫上使用相同的方法?
Service.xml 又名依賴注入器
<service id="wshop_product_service" class="WShopServiceProductService"> <argument type="service" id="product.repository"/> <argument id="wshop_media_image_service" type="service"/> <argument type="service" id="product_media.repository"/> </service>
我是 OOP 的新手。請(qǐng)?zhí)峁┖玫氖纠痛a解釋。謝謝!
如果我理解正確的話,您只希望第一個(gè)參數(shù)可以互換,并且示例中的 3 個(gè)方法應(yīng)該在抽象中實(shí)現(xiàn)。這是一個(gè)想法。
摘要:
abstract class AbstractEntityService { protected EntityRepository $repository; public function __construct(EntityRepository $repository) { $this->repository = $repository; } public function create(array $data, ?Context $context = null): void { $context = $context ?? Context::createDefaultContext(); $this->repository->create([ $data ], $context); } public function update(array $data): void { $this->repository->update([$data], Context::createDefaultContext()); } abstract public function getDataId(array $params): ?string; protected function searchId(Criteria $criteria): ?string { return $this->repository->searchIds( $criteria, Context::createDefaultContext() )->firstId(); } }
您在構(gòu)造函數(shù)中獲取存儲(chǔ)庫,并在抽象中實(shí)現(xiàn)有關(guān)通用存儲(chǔ)庫的所有通用方法。您想要在擴(kuò)展類中實(shí)現(xiàn)的 getDataId
方法,因?yàn)槟鷮?duì)每個(gè)方法都使用了特定的條件(大概)。因此,您只需通過定義抽象簽名來強(qiáng)制擴(kuò)展類中的實(shí)現(xiàn)即可。
您的服務(wù)等級(jí):
class ProductService extends AbstractEntityService { private MediaImageService $mediaImageService; private EntityRepository $productMediaRepository; public function __construct( EntityRepository $productRepository, MediaImageService $mediaImageService, EntityRepository $productMediaRepository ) { parent::__construct($productRepository); $this->mediaImageService = $mediaImageService; $this->productMediaRepository = $productMediaRepository; } public function getDataId(array $params): ?string { if (!isset($params['productNumber'])) { return null; } $criteria = new Criteria(); $criteria->addFilter(new EqualsFilter('productNumber', $params['productNumber'])); return $this->searchId($criteria); } // your other methods using the injected services }
在擴(kuò)展類中,您僅將存儲(chǔ)庫傳遞給父構(gòu)造函數(shù),因?yàn)槠渌⑷氲姆?wù)僅在此特定實(shí)例中使用。您可以在創(chuàng)建特定條件的地方實(shí)現(xiàn) getDataId
并使用該條件調(diào)用受保護(hù)的(因?yàn)樗荒苡蓴U(kuò)展使用)searchId
方法。
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://m.miracleart.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)