beerfranz / execution-context-bundle
Symfony bundle providing unified ExecutionContext between HTTP, messenger and command.
Package info
github.com/beerfranz/ExecutionContextBundle
Type:symfony-bundle
pkg:composer/beerfranz/execution-context-bundle
Requires
- php: >=8.4.1
- symfony/console: ^7.4 || ^8.0
- symfony/framework-bundle: ^7.4 || ^8.0
- symfony/messenger: ^7.4 || ^8.0
- symfony/security-bundle: ^7.4 || ^8.0
- symfony/uid: ^7.4 || ^8.0
Requires (Dev)
- api-platform/core: ^4.3
- doctrine/doctrine-bundle: ^3.0
- doctrine/doctrine-migrations-bundle: ^3.6
- doctrine/orm: ^3.4
- phpunit/phpunit: ^10.5
- symfony/browser-kit: ^7.4 || ^8.0
- symfony/http-client: ^7.4 || ^8.0
- symfony/monolog-bundle: ^3.10 || ^4
- symfony/yaml: ^7.4 || ^8.0
- zenstruck/messenger-test: ^1.12
Suggests
- open-telemetry/api: To propagate the current trace ID into the ExecutionContext.
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-11-28 13:16:45 UTC
README
The ExecutionContext service hold this info, whatever the context:
- user
- current execution context: (
http,messengerorcommand) - Dryrun mode (implementing dryrun is on your hands)
What this bundle do
Use this bundle if you want to:
- have identification to your commands or messages.
- use Symfony voters during message or console command.
What this bundle do NOT
This bundle do NOT:
- add authentication, aka verification of the identity (for HTTP context, this bundle use the Symfony firewall).
- add a dryrun mode to your application: it only provide a way to know if the request expect a dryrun.
Usage
You can see code examples in test folder.
Inject the Beerfranz\ExecutionContext\ExecutionContext service to your class to have access to its informations:
use Beerfranz\ExecutionContext\ExecutionContext; final class MyService { public function __construct(private readonly ExecutionContext $executionContext) { } public function doSomething(): void { if ($this->executionContext->isInteractive()) { // ... } } }
Available contexts are exposed as constants on the class:
ExecutionContext::HTTPExecutionContext::MESSENGERExecutionContext::COMMAND
If the user is not defined, the user is a ExecutionContextInternalUser, implementing UserInterface, with getRoles() = [] and getUserIdentifier() = <config.yml: beerfranz_execution_context.fallback_user_identifiers.[context]>.
Available methods:
init(string $context = self::HTTP, ?UserInterface $user = null, bool $dryrun = false, ?string $tenantId = null)reset(): clean current context.getContext(): stringgetUser(): UserInterfaceisInternalUser(?string $userId = null): bool: if user is faked by this bundleisHttp(): bool: true if context is HTTPisMessenger(): bool: true if context is messengerisCommand(): bool: true if context is commandisInteractive(): bool: true if context is HTTP or commandsetDryRun(bool $dryrun): selfisDryRun(): boolgetTenantId(): ?stringsetTenantId(?string $tenantId): self
Tenant
The bundle provide an interface Beerfranz\ExecutionContext\Entity\ExecutionContextTenantInterface
Implement it on your entities to have a common way to get the tenant ID:
$this->executionContext->setTenantId($entity->getTenantIdentifier());
HTTP context
Set dryrun using HTTP header X-Dry-Run: "true".
Command context
Extend your commands by Beerfranz\ExecutionContext\Command\AbstractCommand.
use Beerfranz\ExecutionContext\Command\AbstractCommand; use Beerfranz\ExecutionContext\ExecutionContext; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; class MyCommand extends AbstractCommand { public function __construct( ExecutionContext $executionContext, ) { parent::__construct($executionContext); } protected function configure(): void { parent::configure(); } protected function initialize(InputInterface $input, OutputInterface $output): void { parent::initialize($input, $output); } protected function execute(InputInterface $input, OutputInterface $output): int { // Your code } }
The configure() add to your command:
--user <userId>: define the user.--dryrun: is dryrun enabled.
The ExecutionContext class have specific methods:
initFromEventCommand(ConsoleCommandEvent $event): void: used by command event listener.initFromCommand(?string $userId = null, bool $dryrun = false): void: used byAbstractCommand:initialize().
Messenger context
For this context, messages should implement Beerfranz\ExecutionContext\Messenger\MessageInterface to add this informations:
getMessageId(): ?string: uniq ID of the message.getCorrelationId(): ?string: ID of the root message parent in case of chaining messages.getCausationId(): ?string: ID of the parent message in case of chaining messages.getProducerId(): ?string: ID of the producer of the message.getUserId(): ?string: return theUserInterface->getUserIdentifier().getTenantId(): ?string: tenant identifier.isDrunRun(): bool: true if dryrun is expected.
Use this helper to generate a message, based on the current context:
$message = $this->executionContext->newMessage(MyMessage::class [, messageId]);
Don't set messageId, let the code generate one for you. It's only for easier testing.
The ExecutionContext class have specific methods:
getCurrentMessage(): ?MessageInterface: the message the produce the current code iteration.setCurrentMessage(?MessageInterface $currentMessage): self: Setter of the current message.newMessage(?string $messageClass = null, ?string $messageId = null): MessageInterface: helper to generate a new message that will have references of the current message. Never set parameter$messageId, except for testing usage.initFromEvent(WorkerMessageReceivedEvent $event): void: Used by the message event subscriber.
Installation
Register the bundle in config/bundles.php:
return [ // ... Beerfranz\ExecutionContext\BeerfranzExecutionContextBundle::class => ['all' => true], ];
Register the listeners on event kernel in config/services.yaml:
services: Beerfranz\ExecutionContext\EventListener\HttpListener: tags: - kernel.event_subscriber Beerfranz\ExecutionContext\EventListener\MessengerListener: tags: - kernel.event_subscriber Beerfranz\ExecutionContext\EventListener\CommandListener: tags: - kernel.event_subscriber
Your User entity must implement Symfony\Component\Security\Core\User\UserInterface
Your User repository must implement Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface
Warning:
security:
providers:
app_user_provider:
entity:
class: Beerfranz\ExecutionContext\AppTest\Entity\User
property: username => disable loadUserByIdentifier from repo
Finally, configure the service:
beerfranz_execution_context: user_class: 'App\Entity\User' fallback_user_identifiers: default: default http: default-http command: default-command messenger: default-messenger # Or if you prefer UUID format, this is an example with UUIDv8 # default: 00000000-0000-8000-a000-000000000000 # http: 00000000-0000-8000-a000-000000000001 # command: 00000000-0000-8000-a000-000000000002 # messenger: 00000000-0000-8000-a000-000000000003
Known issue
- Console event listener is not triggered: need to implement set/reset ExecutionContext at command level.
TODO
- check comportment when configuration looks like:
security: providers: app_user_provider: entity: class: Beerfranz\ExecutionContext\AppTest\Entity\User property: uuid # Uuid is not a string
- forbid usage of
ExecutionContextInternalUser(force user to be applicative user). ⚠️ should be bypassable for some commands like app initialization, or migration.
Tests
docker compose run --rm php composer update
docker compose up
docker compose run --rm php vendor/bin/phpunit --filter testCommandUser1
docker compose run --rm php composer validate --strict