setono / sylius-static-contexts-bundle
A Symfony bundle that gives you static contexts
Package info
github.com/Setono/sylius-static-contexts-bundle
Type:symfony-bundle
pkg:composer/setono/sylius-static-contexts-bundle
v2.0.0
2026-04-13 08:03 UTC
Requires
- php: >=8.2
- sylius/channel: ^2.0
- sylius/locale: ^2.0
- symfony/config: ^6.4 || ^7.4
- symfony/dependency-injection: ^6.4 || ^7.4
- symfony/http-kernel: ^6.4 || ^7.4
- symfony/service-contracts: ^3.0
Requires (Dev)
- jangregor/phpstan-prophecy: ^2.3
- matthiasnoback/symfony-dependency-injection-test: ^6.3
- phpspec/prophecy-phpunit: ^2.5
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.0
- phpstan/phpstan-symfony: ^2.0
- phpunit/phpunit: ^11.0
- setono/code-quality-pack: ^3.1
- shipmonk/composer-dependency-analyser: ^1.8
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Provides static channel and locale contexts for Sylius. This allows you to set the channel and locale programmatically instead of relying on HTTP request resolution — useful for CLI commands, message handlers, and testing.
Both contexts are registered at priority 256, so they take precedence over Sylius's default request-based contexts whenever a value is set.
Requirements
- PHP >= 8.2
- Symfony ^6.4 || ^7.4
- Sylius v2
For Sylius v1 support, use the
1.xbranch.
Installation
composer require setono/sylius-static-contexts-bundle
Usage
Setting the channel
use Setono\SyliusStaticContextsBundle\Context\StaticChannelContext; final class YourCommand extends Command { public function __construct(private readonly StaticChannelContext $channelContext) { parent::__construct(); } protected function execute(InputInterface $input, OutputInterface $output): int { // Set by channel code (looks up the channel via the repository) $this->channelContext->setChannelCode('web_us'); // Or set by channel object directly $this->channelContext->setChannel($channel); // Now any service that depends on ChannelContextInterface // will resolve to this channel // ... // Reset when done (also happens automatically via Symfony's ResetInterface) $this->channelContext->reset(); return Command::SUCCESS; } }
Setting the locale
use Setono\SyliusStaticContextsBundle\Context\StaticLocaleContext; final class YourCommand extends Command { public function __construct(private readonly StaticLocaleContext $localeContext) { parent::__construct(); } protected function execute(InputInterface $input, OutputInterface $output): int { $this->localeContext->setLocaleCode('en_US'); // Now any service that depends on LocaleContextInterface // will resolve to this locale // ... $this->localeContext->reset(); return Command::SUCCESS; } }