Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
name: CI

on:
pull_request:
push:
branches: [ main, develop ]

jobs:
run:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
php:
- '8.3'
Comment thread
cod-a-holic marked this conversation as resolved.
- '8.4'
coverage: ['none']
symfony-versions:
- '6.4.*'
- '7.0.*'
include:
- description: 'Log Code Coverage'
php: '8.3'
symfony-versions: '^7.0'
doctrine-orm-versions: '^3.0'
coverage: xdebug

name: PHP ${{ matrix.php }} Symfony ${{ matrix.symfony-versions }}
steps:
- name: Checkout
uses: actions/checkout@v4

- uses: actions/cache@v4
with:
path: ~/.composer/cache/files
key: ${{ matrix.php }}-${{ matrix.symfony-versions }}

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
coverage: xdebug

- name: Add PHPUnit matcher
run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"

- name: Set composer cache directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"

- name: Cache composer
uses: actions/cache@v4
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-${{ matrix.php }}-${{ matrix.symfony-versions }}-composer-${{ hashFiles('composer.json') }}
restore-keys: ${{ runner.os }}-${{ matrix.php }}-${{ matrix.symfony-versions }}-composer

- name: Update Symfony version
if: matrix.symfony-versions != ''
run: |
composer require symfony/framework-bundle:${{ matrix.symfony-versions }} --no-update --no-scripts
composer require --dev symfony/yaml:${{ matrix.symfony-versions }} --no-update --no-scripts

- name: Install dependencies
run: composer install

- name: Run PHPUnit tests
run: vendor/bin/phpunit
if: matrix.coverage == 'none'

- name: PHPUnit tests and Log Code coverage
run: vendor/bin/phpunit --coverage-clover=coverage.xml
if: matrix.coverage == 'xdebug'

- name: Upload coverage reports to Codecov
if: matrix.coverage == 'xdebug'
uses: codecov/codecov-action@v4.0.1
with:
token: ${{ secrets.CODECOV_TOKEN }}
24 changes: 24 additions & 0 deletions .github/workflows/security.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
on:
pull_request:
push:
branches: [ main, develop ]

jobs:
security-checker:
name: Security checker
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2

- name: Install dependencies
run: composer install --no-progress --no-interaction --prefer-dist

- name: Download local-php-security-checker
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

- name: Run local-php-security-checker
run: chmod +x local-php-security-checker && ./local-php-security-checker
55 changes: 55 additions & 0 deletions .github/workflows/static-analysis.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: Code style and static analysis

on:
pull_request:
push:
branches: [ main, develop ]

jobs:
php-cs-fixer:
name: PHP-CS-Fixer
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2

- name: Install dependencies
run: composer install --no-progress --no-interaction --prefer-dist

- name: Run script
run: vendor/bin/phpcs

phpstan:
name: PHPStan
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2

- name: Install dependencies
run: composer install --no-progress --no-interaction --prefer-dist

- name: Run script
run: vendor/bin/phpstan analyse

composer-validate:
name: Composer validate
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2

- name: Install dependencies
run: composer install --no-progress --no-interaction --prefer-dist

- name: Run script
run: composer composer-validate
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.idea
/vendor/
/composer.lock
/.phpcs-cache

###> phpunit/phpunit ###
/phpunit.xml
.phpunit.result.cache
###< phpunit/phpunit ###
91 changes: 89 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,89 @@
# schema-context-bundle
Schema context bundle
# Schema Context Bundle

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 from request headers.
- Stores schema context in a global `SchemaResolver`.
- Injects schema info into Messenger messages via a middleware.
- Rehydrates schema on message consumption via a middleware.

---

## Installation

```bash
composer require macpaw/schema-context-bundle
```

If you are not using Symfony Flex, register the bundle manually:

```php
// config/bundles.php
return [
Macpaw\SchemaContextBundle\SchemaContextBundle::class => ['all' => true],
];
```
## Configuration
### 1. Bundle Configuration
Add this config to `config/packages/schema_context.yaml`:

```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:

```env
APP_NAME=develop
```

## Usage

```php
use Macpaw\SchemaContextBundle\Service\SchemaResolver;

public function index(SchemaResolver $schemaResolver)
{
$schema = $schemaResolver->getSchema();
// Use schema in logic
}
```

## 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`:

```yaml
framework:
messenger:
buses:
messenger.bus.default:
middleware:
- Macpaw\SchemaContextBundle\Messenger\Middleware\SchemaMiddleware
```

## Testing
To run tests:
```bash
vendor/bin/phpunit
```

## Contributing
Feel free to open issues and submit pull requests.

## License
This bundle is released under the MIT license.

23 changes: 23 additions & 0 deletions SECURITY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Security Policy

## Reporting Security Issues
If you believe you have found a security vulnerability in any MacPaw-owned repository, please report it to us through coordinated disclosure.

Please do not report security vulnerabilities through public GitHub issues, discussions, or pull requests.

Instead, please send an email to security[@]macpaw.com.

Please include as much of the information listed below as you can to help us better understand and resolve the issue:

- The type of issue (e.g., buffer overflow, SQL injection, or cross-site scripting)
- Full paths of source file(s) related to the manifestation of the issue
- The location of the affected source code (tag/branch/commit or direct URL)
- Any special configuration required to reproduce the issue
- Step-by-step instructions to reproduce the issue
- Proof-of-concept or exploit code (if possible)
- Impact of the issue, including how an attacker might exploit the issue

This information will help us triage your report more quickly.

## Policy
See MacPaw's [Vulnerability Disclosure Policy](https://macpaw.com/vulnerability-disclosure-policy)
49 changes: 49 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"name": "macpaw/schema-context-bundle",
"description": "A Symfony bundle to provide schema context",
"type": "symfony-bundle",
"autoload": {
"psr-4": {
"Macpaw\\SchemaContextBundle\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Macpaw\\SchemaContextBundle\\Tests\\": "tests/"
}
},
"require": {
"php": ">=8.3",
"symfony/messenger": "^6.4 || ^7.0",
"symfony/http-kernel": "^6.4 || ^7.0",
"symfony/dependency-injection": "^6.4 || ^7.0",
"symfony/config": "^6.4 || ^7.0"
},
"require-dev": {
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^10.0",
"squizlabs/php_codesniffer": "3.7.*"
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
},
"scripts": {
"composer-validate": [
"composer validate"
],
"cs": [
"vendor/bin/phpcs"
],
"cs-fix": [
"vendor/bin/phpcbf"
],
"phpstan": [
"vendor/bin/phpstan analyse"
],
"phpunit": [
"vendor/bin/phpunit"
]
}
}
23 changes: 23 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
services:
_defaults:
autowire: true
autoconfigure: true
public: false

Macpaw\SchemaContextBundle\Service\SchemaResolver:
public: true
shared: true

Macpaw\SchemaContextBundle\EventListener\SchemaRequestListener:
arguments:
$schemaResolver: '@Macpaw\SchemaContextBundle\Service\SchemaResolver'
$schemaRequestHeader: '%schema_context.header_name%'
$defaultSchema: '%schema_context.default_schema%'
$appName: '%schema_context.app_name%'
$allowedAppNames: '%schema_context.allowed_app_names%'
tags:
- { name: kernel.event_subscriber }

Macpaw\SchemaContextBundle\Messenger\Middleware\SchemaMiddleware:
tags:
- { name: messenger.middleware }
Loading
Loading