Problem/Motivation

Commerce collects its resolvers, checkers and processors with Drupal's service_collector tag, which instantiates every tagged service when the collector is built and calls an add*() method for each. Core is moving to Symfony tagged iterators (#[AutowireIterator] / !tagged_iterator, e.g. PathProcessorManager, CachedDiscoveryClearer, WorkspaceManager), which build services lazily, on iteration.

Chains stop at the first match, so with an iterator the resolvers after it are never instantiated. Several of these chains run on every rendered price.

#3639236 introduced ChainOrganizationResolver with #[AutowireIterator]. This issue converts the rest to follow it. Removing the add*() methods is a BC break, so it has to land in 4.0.0.

Proposed resolution

Inject the tagged services with #[AutowireIterator(tag: '...')], drop the service_collector tags and remove the add*() / getResolvers() methods from the classes and interfaces:

  • commerce.chain_country_resolver (commerce.country_resolver)
  • commerce.chain_locale_resolver (commerce.locale_resolver)
  • commerce_store.chain_store_resolver (commerce_store.store_resolver)
  • commerce_price.chain_price_resolver (commerce_price.price_resolver)
  • commerce_order.chain_order_type_resolver (commerce_order.order_type_resolver)
  • commerce_order.availability_manager (commerce_order.availability_checker)
  • commerce_order.order_refresh (commerce_order.order_preprocessor, commerce_order.order_processor)
  • commerce_checkout.chain_checkout_flow_resolver (commerce_checkout.checkout_flow_resolver)
  • commerce_tax.chain_tax_rate_resolver (commerce_tax.tax_rate_resolver)

The chain interfaces (ChainCountryResolverInterface, …) stay as empty interfaces extending the resolver interface, so the chain can still be autowired unambiguously.

No functional change in ordering: both mechanisms sort by the tag's priority attribute (default 0, descending) and keep definition order on ties. Tags stay as they are, e.g. { name: commerce.price_resolver, priority: 100 }.

Out of scope: PriceCalculator, which groups processors by adjustment_type through PriceCalculatorPass.

Remaining tasks

  • Convert the services listed above.
  • Update the tests calling add*() at runtime: AvailabilityManagerTest (unit, pass the checkers to the constructor), OrderRefreshTest and CartOrderRefreshTest (tag a test processor in a test module instead).
  • Change record.

User interface changes

None.

API changes

  • The add*() and getResolvers() methods are removed from the chain classes and interfaces, AvailabilityManagerInterface and OrderRefreshInterface. Code adding a resolver, checker or processor at runtime tags a service or decorates the collector instead.
  • Constructors receive an iterable instead of the services being added afterwards. Subclasses overriding the constructor need to pass it on.
  • A service tagged with the wrong interface is no longer caught when the container is built (the add*() type hint did that); it fails when the chain calls it.

Data model changes

None.

To check before posting:
- I wrote down the five chain_* service IDs that weren't in my grep output by following the naming pattern: country, locale, checkout flow, tax rate, price. The two order services I saw directly. Confirm them in the services.yml files.
- AvailabilityManager and OrderRefresh aren't chains and don't stop at the first match. They gain lazy instantiation but no early exit.
- I didn't check whether contrib calls these add*() methods at runtime. The usual contrib extension point is tagging, so the impact should be small, but that's an assumption.

Issue fork commerce-3639772

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

jsacksick created an issue.