Skip to content

Commit

Permalink
Merge pull request #5147 from kurozumi/sort-purchase-flow
Browse files Browse the repository at this point in the history
PurchaseFlowのProcessorやValidatorにpriorityを設定できるようにする
  • Loading branch information
ji-eunsoo committed Apr 15, 2024
2 parents 53667c6 + d55f3b0 commit e44c828
Show file tree
Hide file tree
Showing 6 changed files with 352 additions and 167 deletions.
375 changes: 240 additions & 135 deletions app/config/eccube/packages/purchaseflow.yaml

Large diffs are not rendered by default.

30 changes: 30 additions & 0 deletions app/config/eccube/services_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,36 @@ services:
Eccube\Service\PurchaseFlow\Processor\AddPointProcessor:
autowire: true
public: true
Eccube\Service\PurchaseFlow\Processor\DeliverySettingValidator:
autowire: true
public: true
Eccube\Service\PurchaseFlow\Processor\EmptyItemsValidator:
autowire: true
public: true
Eccube\Service\PurchaseFlow\Processor\PaymentTotalLimitValidator:
autowire: true
public: true
Eccube\Service\PurchaseFlow\Processor\PointDiffProcessor:
autowire: true
public: true
Eccube\Service\PurchaseFlow\Processor\PriceChangeValidator:
autowire: true
public: true
Eccube\Service\PurchaseFlow\Processor\ProductStatusValidator:
autowire: true
public: true
Eccube\Service\PurchaseFlow\Processor\StockDiffProcessor:
autowire: true
public: true
Eccube\Service\PurchaseFlow\Processor\ClassCategoryValidator:
autowire: true
public: true
Eccube\Service\PurchaseFlow\Processor\PaymentTotalNegativeValidator:
autowire: true
public: true
Eccube\Service\PurchaseFlow\Processor\PointRateProcessor:
autowire: true
public: true
Eccube\Service\Composer\ComposerApiService:
autowire: true
public: true
87 changes: 67 additions & 20 deletions src/Eccube/DependencyInjection/Compiler/PurchaseFlowPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,54 +18,101 @@
use Eccube\Annotation\CartFlow;
use Eccube\Annotation\OrderFlow;
use Eccube\Annotation\ShoppingFlow;
use Eccube\Service\PurchaseFlow\PurchaseContext;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

class PurchaseFlowPass implements CompilerPassInterface
{
public const ITEM_PREPROCESSOR_TAG = 'eccube.item.preprocessor';
use PriorityTaggedServiceTrait;

public const ITEM_VALIDATOR_TAG = 'eccube.item.validator';
public const ITEM_HOLDER_PREPROCESSOR_TAG = 'eccube.item.holder.preprocessor';
public const ITEM_HOLDER_VALIDATOR_TAG = 'eccube.item.holder.validator';
public const ITEM_HOLDER_POST_VALIDATOR_TAG = 'eccube.item.holder.post.validator';
public const ITEM_PREPROCESSOR_TAG = 'eccube.item.preprocessor';
public const ITEM_HOLDER_PREPROCESSOR_TAG = 'eccube.item.holder.preprocessor';
public const DISCOUNT_PROCESSOR_TAG = 'eccube.discount.processor';
public const ITEM_HOLDER_POST_VALIDATOR_TAG = 'eccube.item.holder.post.validator';
public const PURCHASE_PROCESSOR_TAG = 'eccube.purchase.processor';

public function process(ContainerBuilder $container)
{
$flowDefs = [
CartFlow::class => $container->getDefinition('eccube.purchase.flow.cart'),
ShoppingFlow::class => $container->getDefinition('eccube.purchase.flow.shopping'),
OrderFlow::class => $container->getDefinition('eccube.purchase.flow.order'),
$flowTypes = [
PurchaseContext::CART_FLOW => $container->findDefinition('eccube.purchase.flow.cart'),
PurchaseContext::SHOPPING_FLOW => $container->findDefinition('eccube.purchase.flow.shopping'),
PurchaseContext::ORDER_FLOW => $container->findDefinition('eccube.purchase.flow.order'),
];

$processorTags = [
self::ITEM_PREPROCESSOR_TAG => 'addItemPreprocessor',
self::ITEM_VALIDATOR_TAG => 'addItemValidator',
self::ITEM_HOLDER_PREPROCESSOR_TAG => 'addItemHolderPreprocessor',
self::ITEM_HOLDER_VALIDATOR_TAG => 'addItemHolderValidator',
self::ITEM_HOLDER_POST_VALIDATOR_TAG => 'addItemHolderPostValidator',
self::DISCOUNT_PROCESSOR_TAG => 'addDiscountProcessor',
self::PURCHASE_PROCESSOR_TAG => 'addPurchaseProcessor',
/**
* purchaseflow.yamlに定義を追加した場合の処理
*/
foreach ($this->getProcessorTags() as $tag => $methodName) {
/** @var Reference $id */
foreach ($this->findAndSortTaggedServices($tag, $container) as $id) {
$def = $container->findDefinition($id);
foreach ($def->getTag($tag) as $attributes) {
if (isset($attributes['flow_type'])) {
/**
* @var string $flowType
* @var Definition $purchaseFlowDef
*/
foreach ($flowTypes as $flowType => $purchaseFlowDef) {
if ($flowType === $attributes['flow_type']) {
$purchaseFlowDef->addMethodCall($methodName, [$id]);
}
}
}
}
}
}

$flowDefs = [
CartFlow::class => $container->findDefinition('eccube.purchase.flow.cart'),
ShoppingFlow::class => $container->findDefinition('eccube.purchase.flow.shopping'),
OrderFlow::class => $container->findDefinition('eccube.purchase.flow.order'),
];

// TODO doctrine/anntationsをv2へアップデート。影響がある場合は要調査。
//AnnotationRegistry::registerAutoloadNamespace('Eccube\Annotation', __DIR__.'/../../../../src');
//AnnotationRegistry::registerAutoloadNamespace('Eccube\Annotation', __DIR__ . '/../../../../src');
$reader = new AnnotationReader();

foreach ($processorTags as $tag => $methodName) {
$ids = $container->findTaggedServiceIds($tag);
foreach ($ids as $id => $tags) {
/**
* アノテーションで追加対象のフローを指定した場合の処理
*/
foreach ($this->getProcessorTags() as $tag => $methodName) {
/** @var Reference $id */
foreach ($this->findAndSortTaggedServices($tag, $container) as $id) {
$def = $container->getDefinition($id);
/**
* @var string $annotationName
* @var Definition $purchaseFlowDef
*/
foreach ($flowDefs as $annotationName => $purchaseFlowDef) {
$anno = $reader->getClassAnnotation(new \ReflectionClass($def->getClass()), $annotationName);
if ($anno) {
$purchaseFlowDef->addMethodCall($methodName, [new Reference($id)]);
$purchaseFlowDef->addMethodCall($methodName, [$id]);
$purchaseFlowDef->setPublic(true);
}
}
}
}
}

/**
* @return string[]
*/
private function getProcessorTags(): array
{
return [
self::ITEM_PREPROCESSOR_TAG => 'addItemPreprocessor',
self::ITEM_VALIDATOR_TAG => 'addItemValidator',
self::ITEM_HOLDER_PREPROCESSOR_TAG => 'addItemHolderPreprocessor',
self::ITEM_HOLDER_VALIDATOR_TAG => 'addItemHolderValidator',
self::ITEM_HOLDER_POST_VALIDATOR_TAG => 'addItemHolderPostValidator',
self::DISCOUNT_PROCESSOR_TAG => 'addDiscountProcessor',
self::PURCHASE_PROCESSOR_TAG => 'addPurchaseProcessor',
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
use Eccube\Entity\Order;
use Eccube\Entity\Payment;
use Eccube\Repository\DeliveryRepository;
use Eccube\Service\PurchaseFlow\ItemHolderValidator;
use Eccube\Service\PurchaseFlow\ItemHolderPostValidator;
use Eccube\Service\PurchaseFlow\PurchaseContext;

/**
* 支払い方法が一致しない明細がないかどうか.
*/
class PaymentValidator extends ItemHolderValidator
class PaymentValidator extends ItemHolderPostValidator
{
/**
* @var DeliveryRepository
Expand Down
5 changes: 4 additions & 1 deletion src/Eccube/Service/PurchaseFlow/Processor/TaxProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
use Eccube\Entity\Master\TaxType;
use Eccube\Entity\Order;
use Eccube\Repository\TaxRuleRepository;
use Eccube\Service\OrderHelper;
use Eccube\Service\PurchaseFlow\ItemHolderPreprocessor;
use Eccube\Service\PurchaseFlow\PurchaseContext;
use Eccube\Service\TaxRuleService;
use Eccube\Service\OrderHelper;

class TaxProcessor implements ItemHolderPreprocessor
{
Expand All @@ -50,8 +50,10 @@ class TaxProcessor implements ItemHolderPreprocessor
/**
* TaxProcessor constructor.
*
* @param EntityManagerInterface $entityManager
* @param TaxRuleRepository $taxRuleRepository
* @param TaxRuleService $taxRuleService
* @param OrderHelper $orderHelper
*/
public function __construct(
EntityManagerInterface $entityManager,
Expand Down Expand Up @@ -160,6 +162,7 @@ protected function getTaxType($OrderItemType)
* - ポイント値引き: 税込
*
* @param $OrderItemType
*
* @deprecated OrderHelper::getTaxDisplayTypeを使用してください
*
* @return TaxDisplayType
Expand Down
18 changes: 9 additions & 9 deletions src/Eccube/Service/PurchaseFlow/PurchaseFlow.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,14 +225,14 @@ public function rollback(ItemHolderInterface $target, PurchaseContext $context)
}
}

public function addPurchaseProcessor(PurchaseProcessor $processor)
public function addPurchaseProcessor(PurchaseProcessor $purchaseProcessor)
{
$this->purchaseProcessors[] = $processor;
$this->purchaseProcessors[] = $purchaseProcessor;
}

public function addItemHolderPreprocessor(ItemHolderPreprocessor $holderPreprocessor)
public function addItemHolderPreprocessor(ItemHolderPreprocessor $itemHolderPreprocessor)
{
$this->itemHolderPreprocessors[] = $holderPreprocessor;
$this->itemHolderPreprocessors[] = $itemHolderPreprocessor;
}

public function addItemPreprocessor(ItemPreprocessor $itemPreprocessor)
Expand All @@ -250,9 +250,9 @@ public function addItemHolderValidator(ItemHolderValidator $itemHolderValidator)
$this->itemHolderValidators[] = $itemHolderValidator;
}

public function addItemHolderPostValidator(ItemHolderPostValidator $itemHolderValidator)
public function addItemHolderPostValidator(ItemHolderPostValidator $itemHolderPostValidator)
{
$this->itemHolderPostValidators[] = $itemHolderValidator;
$this->itemHolderPostValidators[] = $itemHolderPostValidator;
}

public function addDiscountProcessor(DiscountProcessor $discountProcessor)
Expand Down Expand Up @@ -384,7 +384,7 @@ public function dump()
return get_class($processor);
};
$flows = [
0 => $this->flowType.' flow',
0 => $this->flowType . ' flow',
'ItemValidator' => $this->itemValidators->map($callback)->toArray(),
'ItemHolderValidator' => $this->itemHolderValidators->map($callback)->toArray(),
'ItemPreprocessor' => $this->itemPreprocessors->map($callback)->toArray(),
Expand All @@ -401,9 +401,9 @@ public function dump()
$out = '';
foreach ($tree as $key => $value) {
if (is_numeric($key)) {
$out .= $value.PHP_EOL;
$out .= $value . PHP_EOL;
} else {
$out .= $key.PHP_EOL;
$out .= $key . PHP_EOL;
}
}

Expand Down

0 comments on commit e44c828

Please sign in to comment.