You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+52-11Lines changed: 52 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,17 +1,18 @@
1
1
# Schema Context Bundle
2
2
3
-
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.
3
+
The **SchemaContextBundle** provides a robust way to manage dynamic schema context across your Symfony application, especially useful for multi-tenant setups. It extracts schema information from W3C standard baggage headers (or Symfony Messenger Stamps), validates schema changes based on environment configuration, and propagates schema context throughout your application, including HTTP clients and Symfony Messenger queues.
4
4
5
5
---
6
6
7
7
## Features
8
8
9
-
- Extracts tenant schema param from baggage request header.
9
+
- Extracts tenant schema param from W3C standard `baggage` request header.
10
10
- Stores schema and baggage context in a global `BaggageSchemaResolver`.
11
+
- Validates schema changes based on environment configuration to prevent accidental schema mismatches.
11
12
- Injects schema and baggage info into Messenger messages via a middleware.
12
13
- Rehydrates schema and baggage on message consumption via a middleware.
13
-
- Provide decorator for Http clients to propagate baggage header
14
-
- Optional: Adds baggage context to Monolog log records via a processor
14
+
- Provide decorator for Http clients to propagate baggage header.
15
+
- Optional: Adds baggage context to Monolog log records via a processor.
15
16
16
17
---
17
18
@@ -35,18 +36,28 @@ Add this config to `config/packages/schema_context.yaml`:
35
36
36
37
```yaml
37
38
schema_context:
38
-
app_name: '%env(APP_NAME)%'#Application name
39
-
header_name: 'X-Tenant'#Request header to extract schema name
40
-
default_schema: 'public'#Default schema to fallback to
41
-
allowed_app_names: ['develop', 'staging', 'test'] #App names where schema context is allowed to change
39
+
environment_name: '%env(APP_ENV)%'#Current environment name (example: 'develop')
40
+
header_name: 'X-Tenant'#Key name in baggage header to extract schema name
41
+
environment_schema: '%env(ENVIRONMENT_SCHEMA)%'#The schema for this environment (example: 'public')
42
+
overridable_environments: ['develop', 'staging', 'test'] #Environments where schema can be overridden via baggage header or Symfony Messenger stamp
42
43
```
43
-
### 2. Set Environment Parameters
44
-
If you're using .env, define the app name:
44
+
45
+
**Configuration parameters:**
46
+
- `environment_name`: The name of the current environment. Best practice is to use `'%env(APP_ENV)%'` to match Symfony's environment.
47
+
- `environment_schema`: The schema for this environment.
48
+
- `header_name`: The key name in the baggage header used to extract the schema value.
49
+
- `overridable_environments`: List of environment names where schema can be overridden via baggage header or Symfony Messenger stamp.
45
50
46
51
```env
47
-
APP_NAME=develop
52
+
APP_ENV=develop
53
+
ENVIRONMENT_SCHEMA=public
48
54
```
49
55
56
+
### 3. Schema Override Protection
57
+
The bundle includes protection against accidental schema changes in production environments:
58
+
- In **non-overridable environments** (e.g., `production`): The schema is always fixed to `environment_schema`. Any attempt to override it via baggage header will throw `EnvironmentSchemaMismatchException`.
59
+
- In **overridable environments** (e.g., `develop`, `staging`): The schema can be dynamically changed via baggage header for testing and development purposes.
60
+
50
61
## Usage
51
62
52
63
```php
@@ -60,6 +71,36 @@ public function index(BaggageSchemaResolver $schemaResolver)
60
71
}
61
72
```
62
73
74
+
### Baggage Header Format
75
+
76
+
The bundle uses W3C standard `baggage` header format. Example request:
0 commit comments