Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Sylius Static Contexts Bundle Laravel Package

setono/sylius-static-contexts-bundle

A Sylius bundle for defining static contexts and reusing them throughout your store, simplifying configuration and keeping context-dependent behavior consistent across channels, locales, and other store settings.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimum requirements (v2.0.0): PHP 8.2+, Symfony ^6.4 || ^7.4, Sylius v2 (sylius/channel ^2.0, sylius/locale ^2.0).

composer require setono/sylius-static-contexts-bundle:^2.0

Then register the bundle in config/bundles.php:

Setono\SyliusStaticContextsBundle\SetonoSyliusStaticContextsBundle::class => ['all' => true],

Where to look first:

  1. config/services.php (new in v2, previously services.xml) — this is where the context services are wired and where you can see the tag priorities that make them win over Sylius defaults.
  2. StaticChannelContext and StaticLocaleContext — the two public services you interact with.
  3. The bundle test suite (DI extension test, bundle test) — a compact reference for expected registration behavior.

First use case (the one that pays for itself immediately): console commands and tests. Sylius's default ChannelContextInterface / LocaleContextInterface resolve from the request, so anything running outside an HTTP context (messenger handlers, CLI commands, factories in tests) blows up or resolves ambiguously. Swap in the static contexts to pin a channel and locale deterministically:

$channelContext->setChannel($channel);
$localeContext->setLocale($locale);

Implementation Patterns

Pinning context in a test. Replace Sylius's context services with the static ones (or override them in config/services_test.yaml) and set the channel/locale in setUp(). This removes the need to bootstrap a fake request just to make contexts resolvable.

protected function setUp(): void
{
    $this->channelContext->setChannel($this->channelRepository->findOneByCode('WEB'));
    $this->localeContext->setLocale('en_US');
}

Pinning context in console commands / messenger handlers. Inject StaticChannelContext and StaticLocaleContext, set them before dispatching to domain logic that needs an active channel (e.g. resolving product translations, channel-priced money). This is the cleanest way to make channel-dependent services usable from CLI.

Overriding Sylius's default contexts container-wide. Because the bundle's services are tagged (and the DI extension test asserts tag priorities), you can rely on the static contexts taking precedence in place of Sylius's request-based contexts. Verify which service ID wins in your app with bin/console debug:container --tag=sylius.context.channel after installing.

Single-channel / single-locale applications. If your app is effectively one channel and one locale, registering this bundle is a legitimate alternative to implementing custom context services — you avoid re-deriving context from the request entirely.

Resetting between operations. In a worker/daemon or a test that mutates context, call reset() in teardown so the next unit of work starts clean; reset() and null-unsetting behavior are explicitly covered by tests in v2.

Gotchas and Tips

v2 is a dependency-only release — but a hard gate. There are no public API changes: StaticChannelContext and StaticLocaleContext keep identical interfaces and behavior. The breaking changes are all upstream requirements (PHP 8.2, Symfony 6.4+, Sylius v2, symfony/service-contracts ^3.0). If your app is on Sylius v1 or PHP 8.1, you cannot take this upgrade — stay on the 1.x branch.

Upgrade path. Bump your app to PHP 8.2+, Symfony 6.4+, and Sylius v2 first, then composer require setono/sylius-static-contexts-bundle:^2.0. There is nothing to change in your code if you only use the public API.

XML → PHP service config. Internal only, but relevant if you ever decorated or overrode the bundle's services by parsing its services.xml: that file no longer exists. Anything referencing it (custom compiler passes, overrides keyed on file path) must be updated. Symfony 7.4 deprecated XmlFileLoader, which is why this changed — don't reintroduce XML config yourself.

Tag priorities matter. The contexts are registered via tags with priorities. If you also register your own context implementation, you need to reason about priority ordering — otherwise the static context may silently win (or lose) to your service. Use debug:container --tag=... rather than guessing.

Context leakage in long-running processes. Static context is, by definition, mutable global state. In a worker or a request-per-boot test suite, forgetting reset() means a channel/locale from a previous job leaks into the next. Make reset() part of your teardown, not an afterthought.

Null means "unset," not "clear to default." Unsetting removes the value; it does not fall back to request-derived context. If code downstream assumes a context is always present, guard for null.

Tooling changes affect contributors, not consumers. Psalm → PHPStan level 9, PHPUnit 11, Rector v2 (UP_TO_PHP_82), and setono/code-quality-pack ^3.1 are maintainer-facing. Only relevant if you fork or submit PRs — expect PHPStan-level-9 strictness on contributions.

CI matrix is the supported target. PHP 8.2/8.3/8.4 against Symfony ~6.4.0 / ~7.4.0. Treat 8.4 on Symfony 6.4 as the least-trodden combination and test it explicitly if you're on that pairing.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
elmogy/larafast
xeviant/laravel-paystack
gorilladash/laravel-website-sdk
martialbe/flysystem-oss
chondal/3312client
jeroen-g/flickr
opscale-co/nova-genbi
itsjustvita/laravel-retention
tequilarapido/trackit
gregsphotography/site-builder-filament
cabaoctavio/laravel-query-log
gregsphotography/site-builder-components
io-digital/clickatell
snowsoft/laravel-admin
oxygen/auth
relicsoftware/barakah-payment-gateway
tobya/work-with-composer
equipc/laravel-email-validator
equipc/laravel-email-database-log
adedsec/nova-translated-labels