Skip to content

Commit 122ad1e

Browse files
committed
feat: schema-context-bundle
1 parent 262fcaf commit 122ad1e

20 files changed

Lines changed: 732 additions & 2 deletions

.github/workflows/ci.yaml

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
name: CI
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches: [ main, develop ]
7+
8+
jobs:
9+
run:
10+
runs-on: ubuntu-latest
11+
strategy:
12+
fail-fast: false
13+
matrix:
14+
php:
15+
- '8.3'
16+
- '8.4'
17+
coverage: ['none']
18+
symfony-versions:
19+
- '6.4.*'
20+
- '7.0.*'
21+
include:
22+
- description: 'Log Code Coverage'
23+
php: '8.3'
24+
symfony-versions: '^7.0'
25+
doctrine-orm-versions: '^3.0'
26+
coverage: xdebug
27+
28+
name: PHP ${{ matrix.php }} Symfony ${{ matrix.symfony-versions }}
29+
steps:
30+
- name: Checkout
31+
uses: actions/checkout@v4
32+
33+
- uses: actions/cache@v4
34+
with:
35+
path: ~/.composer/cache/files
36+
key: ${{ matrix.php }}-${{ matrix.symfony-versions }}
37+
38+
- name: Setup PHP
39+
uses: shivammathur/setup-php@v2
40+
with:
41+
php-version: ${{ matrix.php }}
42+
coverage: xdebug
43+
44+
- name: Add PHPUnit matcher
45+
run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"
46+
47+
- name: Set composer cache directory
48+
id: composer-cache
49+
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
50+
51+
- name: Cache composer
52+
uses: actions/cache@v4
53+
with:
54+
path: ${{ steps.composer-cache.outputs.dir }}
55+
key: ${{ runner.os }}-${{ matrix.php }}-${{ matrix.symfony-versions }}-composer-${{ hashFiles('composer.json') }}
56+
restore-keys: ${{ runner.os }}-${{ matrix.php }}-${{ matrix.symfony-versions }}-composer
57+
58+
- name: Update Symfony version
59+
if: matrix.symfony-versions != ''
60+
run: |
61+
composer require symfony/framework-bundle:${{ matrix.symfony-versions }} --no-update --no-scripts
62+
composer require --dev symfony/yaml:${{ matrix.symfony-versions }} --no-update --no-scripts
63+
64+
- name: Install dependencies
65+
run: composer install
66+
67+
- name: Run PHPUnit tests
68+
run: vendor/bin/phpunit
69+
if: matrix.coverage == 'none'
70+
71+
- name: PHPUnit tests and Log Code coverage
72+
run: vendor/bin/phpunit --coverage-clover=coverage.xml
73+
if: matrix.coverage == 'xdebug'
74+
75+
- name: Upload coverage reports to Codecov
76+
if: matrix.coverage == 'xdebug'
77+
uses: codecov/codecov-action@v4.0.1
78+
with:
79+
token: ${{ secrets.CODECOV_TOKEN }}

.github/workflows/security.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
on:
2+
pull_request:
3+
push:
4+
branches: [ main, develop ]
5+
6+
jobs:
7+
security-checker:
8+
name: Security checker
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: Checkout
12+
uses: actions/checkout@v2
13+
14+
- name: Setup PHP
15+
uses: shivammathur/setup-php@v2
16+
17+
- name: Install dependencies
18+
run: composer install --no-progress --no-interaction --prefer-dist
19+
20+
- name: Download local-php-security-checker
21+
run: curl -s -L -o local-php-security-checker https://github.com/fabpot/local-php-security-checker/releases/download/v1.0.0/local-php-security-checker_1.0.0_linux_amd64
22+
23+
- name: Run local-php-security-checker
24+
run: chmod +x local-php-security-checker && ./local-php-security-checker
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
name: Code style and static analysis
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches: [ main, develop ]
7+
8+
jobs:
9+
php-cs-fixer:
10+
name: PHP-CS-Fixer
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout
14+
uses: actions/checkout@v2
15+
16+
- name: Setup PHP
17+
uses: shivammathur/setup-php@v2
18+
19+
- name: Install dependencies
20+
run: composer install --no-progress --no-interaction --prefer-dist
21+
22+
- name: Run script
23+
run: vendor/bin/phpcs
24+
25+
phpstan:
26+
name: PHPStan
27+
runs-on: ubuntu-latest
28+
steps:
29+
- name: Checkout
30+
uses: actions/checkout@v2
31+
32+
- name: Setup PHP
33+
uses: shivammathur/setup-php@v2
34+
35+
- name: Install dependencies
36+
run: composer install --no-progress --no-interaction --prefer-dist
37+
38+
- name: Run script
39+
run: vendor/bin/phpstan analyse
40+
41+
composer-validate:
42+
name: Composer validate
43+
runs-on: ubuntu-latest
44+
steps:
45+
- name: Checkout
46+
uses: actions/checkout@v2
47+
48+
- name: Setup PHP
49+
uses: shivammathur/setup-php@v2
50+
51+
- name: Install dependencies
52+
run: composer install --no-progress --no-interaction --prefer-dist
53+
54+
- name: Run script
55+
run: composer composer-validate

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.idea
2+
/vendor/
3+
/composer.lock
4+
/.phpcs-cache
5+
6+
###> phpunit/phpunit ###
7+
/phpunit.xml
8+
.phpunit.result.cache
9+
###< phpunit/phpunit ###

README.md

Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,89 @@
1-
# schema-context-bundle
2-
Schema context bundle
1+
# Schema Context Bundle
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.
4+
5+
---
6+
7+
## Features
8+
9+
- Extracts tenant schema from request headers.
10+
- Stores schema context in a global `SchemaResolver`.
11+
- Injects schema info into Messenger messages via a middleware.
12+
- Rehydrates schema on message consumption via a middleware.
13+
14+
---
15+
16+
## Installation
17+
18+
```bash
19+
composer require macpaw/schema-context-bundle
20+
```
21+
22+
If you are not using Symfony Flex, register the bundle manually:
23+
24+
```php
25+
// config/bundles.php
26+
return [
27+
Macpaw\SchemaContextBundle\SchemaContextBundle::class => ['all' => true],
28+
];
29+
```
30+
## Configuration
31+
### 1. Bundle Configuration
32+
Add this config to `config/packages/schema_context.yaml`:
33+
34+
```yaml
35+
schema_context:
36+
app_name: '%env(APP_NAME)%' # Application name
37+
header_name: 'X-Tenant' # Request header to extract schema name
38+
default_schema: 'public' # Default schema to fallback to
39+
allowed_app_names: ['develop', 'staging', 'test'] # App names where schema context is allowed to change
40+
```
41+
### 2. Set Environment Parameters
42+
If you're using .env, define the app name:
43+
44+
```env
45+
APP_NAME=develop
46+
```
47+
48+
## Usage
49+
50+
```php
51+
use Macpaw\SchemaContextBundle\Service\SchemaResolver;
52+
53+
public function index(SchemaResolver $schemaResolver)
54+
{
55+
$schema = $schemaResolver->getSchema();
56+
// Use schema in logic
57+
}
58+
```
59+
60+
## Messenger Integration
61+
The bundle provides a middleware that automatically:
62+
63+
* Adds a SchemaStamp to dispatched messages
64+
65+
* Restores the schema context on message handling
66+
67+
Enable the middleware in your `messenger.yaml`:
68+
69+
```yaml
70+
framework:
71+
messenger:
72+
buses:
73+
messenger.bus.default:
74+
middleware:
75+
- Macpaw\SchemaContextBundle\Messenger\Middleware\SchemaMiddleware
76+
```
77+
78+
## Testing
79+
To run tests:
80+
```bash
81+
vendor/bin/phpunit
82+
```
83+
84+
## Contributing
85+
Feel free to open issues and submit pull requests.
86+
87+
## License
88+
This bundle is released under the MIT license.
89+

SECURITY.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Security Policy
2+
3+
## Reporting Security Issues
4+
If you believe you have found a security vulnerability in any MacPaw-owned repository, please report it to us through coordinated disclosure.
5+
6+
Please do not report security vulnerabilities through public GitHub issues, discussions, or pull requests.
7+
8+
Instead, please send an email to security[@]macpaw.com.
9+
10+
Please include as much of the information listed below as you can to help us better understand and resolve the issue:
11+
12+
- The type of issue (e.g., buffer overflow, SQL injection, or cross-site scripting)
13+
- Full paths of source file(s) related to the manifestation of the issue
14+
- The location of the affected source code (tag/branch/commit or direct URL)
15+
- Any special configuration required to reproduce the issue
16+
- Step-by-step instructions to reproduce the issue
17+
- Proof-of-concept or exploit code (if possible)
18+
- Impact of the issue, including how an attacker might exploit the issue
19+
20+
This information will help us triage your report more quickly.
21+
22+
## Policy
23+
See MacPaw's [Vulnerability Disclosure Policy](https://macpaw.com/vulnerability-disclosure-policy)

composer.json

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"name": "macpaw/schema-context-bundle",
3+
"description": "A Symfony bundle to provide schema context",
4+
"type": "symfony-bundle",
5+
"autoload": {
6+
"psr-4": {
7+
"Macpaw\\SchemaContextBundle\\": "src/"
8+
}
9+
},
10+
"autoload-dev": {
11+
"psr-4": {
12+
"Macpaw\\SchemaContextBundle\\Tests\\": "tests/"
13+
}
14+
},
15+
"require": {
16+
"php": ">=8.3",
17+
"symfony/messenger": "^6.4 || ^7.0",
18+
"symfony/http-kernel": "^6.4 || ^7.0",
19+
"symfony/dependency-injection": "^6.4 || ^7.0",
20+
"symfony/config": "^6.4 || ^7.0"
21+
},
22+
"require-dev": {
23+
"phpstan/phpstan": "^1.10",
24+
"phpunit/phpunit": "^10.0",
25+
"squizlabs/php_codesniffer": "3.7.*"
26+
},
27+
"config": {
28+
"allow-plugins": {
29+
"dealerdirect/phpcodesniffer-composer-installer": true
30+
}
31+
},
32+
"scripts": {
33+
"composer-validate": [
34+
"composer validate"
35+
],
36+
"cs": [
37+
"vendor/bin/phpcs"
38+
],
39+
"cs-fix": [
40+
"vendor/bin/phpcbf"
41+
],
42+
"phpstan": [
43+
"vendor/bin/phpstan analyse"
44+
],
45+
"phpunit": [
46+
"vendor/bin/phpunit"
47+
]
48+
}
49+
}

config/services.yaml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
services:
2+
_defaults:
3+
autowire: true
4+
autoconfigure: true
5+
public: false
6+
7+
Macpaw\SchemaContextBundle\Service\SchemaResolver:
8+
public: true
9+
shared: true
10+
11+
Macpaw\SchemaContextBundle\EventListener\SchemaRequestListener:
12+
arguments:
13+
$schemaResolver: '@Macpaw\SchemaContextBundle\Service\SchemaResolver'
14+
$schemaRequestHeader: '%schema_context.header_name%'
15+
$defaultSchema: '%schema_context.default_schema%'
16+
$appName: '%schema_context.app_name%'
17+
$allowedAppNames: '%schema_context.allowed_app_names%'
18+
tags:
19+
- { name: kernel.event_subscriber }
20+
21+
Macpaw\SchemaContextBundle\Messenger\Middleware\SchemaMiddleware:
22+
tags:
23+
- { name: messenger.middleware }

0 commit comments

Comments
 (0)