国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Shopware 6、Symfony 中的抽象類和依賴注入
P粉680087550
P粉680087550 2024-01-09 08:44:57
[PHP討論組]

目前我正在開發(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解釋。謝謝!

P粉680087550
P粉680087550

全部回復(fù)(1)
P粉248602298

如果我理解正確的話,您只希望第一個(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 方法。

最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
關(guān)于我們 免責(zé)申明 意見反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長!
關(guān)注服務(wù)號(hào) 技術(shù)交流群
PHP中文網(wǎng)訂閱號(hào)
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時(shí)隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號(hào)
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://m.miracleart.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)