macpaw / schema-context-bundle
A Symfony bundle to provide schema context
Installs: 209
Dependents: 2
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: >=8.3
- symfony/config: ^6.4 || ^7.0
- symfony/dependency-injection: ^6.4 || ^7.0
- symfony/http-client: ^6.4 || ^7.0
- symfony/http-kernel: ^6.4 || ^7.0
- symfony/messenger: ^6.4 || ^7.0
Requires (Dev)
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^10.0
- squizlabs/php_codesniffer: 3.7.*
This package is auto-updated.
Last update: 2025-08-04 17:33:05 UTC
README
The SchemaContextBundle provides a lightweight way to manage dynamic schema context across your Symfony application, especially useful for multi-tenant setups. It allows schema resolution based on request headers and propagates schema information through Symfony Messenger.
Features
- Extracts tenant schema param from baggage request header.
- Stores schema context in a global
SchemaResolver
. - Injects schema info into Messenger messages via a middleware.
- Rehydrates schema on message consumption via a middleware.
- Provide decorator for Http clients to propagate schema header
Installation
composer require macpaw/schema-context-bundle
If you are not using Symfony Flex, register the bundle manually:
// config/bundles.php return [ Macpaw\SchemaContextBundle\SchemaContextBundle::class => ['all' => true], ];
Configuration
1. Bundle Configuration
Add this config to config/packages/schema_context.yaml
:
schema_context: app_name: '%env(APP_NAME)%' # Application name header_name: 'X-Tenant' # Request header to extract schema name default_schema: 'public' # Default schema to fallback to allowed_app_names: ['develop', 'staging', 'test'] # App names where schema context is allowed to change
2. Set Environment Parameters
If you're using .env, define the app name:
APP_NAME=develop
Usage
use Macpaw\SchemaContextBundle\Service\SchemaResolver; public function index(SchemaResolver $schemaResolver) { $schema = $schemaResolver->getSchema(); // Use schema in logic }
Schema-Aware HTTP Client
Decorate your http client in your service configuration:
services: schema_aware_payment_http_client: class: Macpaw\SchemaContextBundle\HttpClient\SchemaAwareHttpClient decorates: payment_http_client #http client to decorate arguments: - '@schema_aware_payment_http_client.inner' - '@Macpaw\SchemaContextBundle\Service\SchemaResolver' - '%schema_context.header_name%'
Messenger Integration
The bundle provides a middleware that automatically:
-
Adds a SchemaStamp to dispatched messages
-
Restores the schema context on message handling
Enable the middleware in your messenger.yaml
:
framework: messenger: buses: messenger.bus.default: middleware: - Macpaw\SchemaContextBundle\Messenger\Middleware\SchemaMiddleware
Testing
To run tests:
vendor/bin/phpunit
Contributing
Feel free to open issues and submit pull requests.
License
This bundle is released under the MIT license.