Skip to content

Instantly share code, notes, and snippets.

@izayoi256
Last active November 25, 2020 08:59
Show Gist options
  • Save izayoi256/6c6bd3a800da8772669f43a651068a48 to your computer and use it in GitHub Desktop.
Save izayoi256/6c6bd3a800da8772669f43a651068a48 to your computer and use it in GitHub Desktop.
[EC-CUBE4] 既存のPurchaseFlowを維持したままProcessorを追加する

プラグインやカスタマイズで、PurchaseFlowに処理を追加したい。

だけど本体側のファイルを編集することや、他者によるカスタマイズを上書きすることは避けたい。

こんなときはSymfonyのサービスコンテナのDecorateという機能を使う。

Decorateとは

これ読んで下さい。

https://symfony.com/doc/3.4/service_container/service_decoration.html

ひと工夫

ちょっと待って! Doctrine\Common\Collections\ArrayCollectionのコンストラクタはarrayしか受け取れないんだけど!

そんなときはラッパークラスを用意してあげて、arrayでもDoctrine\Common\Collections\ArrayCollectionでも受け取れるようにしてあげましょう。

アノテーションじゃダメなの?

アッハイ、それでいいです。

  • @CartFlow
  • @ShoppingFlow
  • @OrderFlow

こんな機能があったんですねドキュメントにも書いてない機能だしこの記事を作ったあとで存在に気付きました。

単純に末尾に追加するんじゃないケース、例えば先頭に追加するとか特定のクラスインスタンスの前後に挿入するとかってときは、本稿の方法を採用したらいいんじゃないでしょうか。

<?php
namespace Customize\Common\Collections;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
class DecorativeArrayCollection extends ArrayCollection
{
public function __construct($elements = [])
{
$elements = ($elements instanceof Collection)
? $elements->toArray()
: $elements;
parent::__construct($elements);
}
}
<?php
namespace Customize\Service\PurchaseFlow\Processor;
use Eccube\Entity\ItemInterface;
use Eccube\Service\PurchaseFlow\ItemValidator;
use Eccube\Service\PurchaseFlow\PurchaseContext;
class MyValidator extends ItemValidator
{
protected function validate(ItemInterface $item, PurchaseContext $context)
{
// TODO
}
}
services:
eccube.purchase.flow.cart.item_validators_decorate:
class: Customize\Common\Collections\DecorativeArrayCollection
decorates: eccube.purchase.flow.cart.item_validators
arguments: ['@eccube.purchase.flow.cart.item_validators_decorate.inner']
calls:
- [add, ['@Customize\Service\PurchaseFlow\Processor\MyValidator']]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment