tourze / env-manage-bundle
Symfony环境变量管理束,支持数据库存储和动态加载环境变量配置
Installs: 366
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
pkg:composer/tourze/env-manage-bundle
Requires
- php: ^8.1
- doctrine/dbal: ^4.0
- doctrine/doctrine-bundle: ^2.13
- doctrine/orm: ^3.0
- doctrine/persistence: ^3.1 || ^4
- easycorp/easyadmin-bundle: ^4
- knplabs/knp-menu: ^3.7
- psr/log: ^3|^2|^1
- symfony/cache: ^6.4
- symfony/cache-contracts: ^3
- symfony/config: ^6.4
- symfony/console: ^6.4
- symfony/dependency-injection: ^6.4
- symfony/doctrine-bridge: ^6.4
- symfony/dotenv: ^6.4
- symfony/event-dispatcher: ^6.4
- symfony/framework-bundle: ^6.4
- symfony/http-foundation: ^6.4
- symfony/http-kernel: ^6.4
- symfony/messenger: ^6.4
- symfony/routing: ^6.4
- symfony/yaml: ^6.4 || ^7.1
- tourze/arrayable: 0.0.*
- tourze/bundle-dependency: 0.0.*
- tourze/doctrine-helper: 0.0.*
- tourze/doctrine-indexed-bundle: 0.0.*
- tourze/doctrine-ip-bundle: 0.0.*
- tourze/doctrine-snowflake-bundle: 0.1.*
- tourze/doctrine-timestamp-bundle: 0.0.*
- tourze/doctrine-track-bundle: 0.1.*
- tourze/doctrine-user-bundle: 0.0.*
- tourze/easy-admin-menu-bundle: 0.1.*
- tourze/json-rpc-cache-bundle: 0.1.*
- tourze/json-rpc-core: 0.0.*
- twig/twig: ^3.13|^4.0
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^10.0
README
A Symfony bundle for managing environment variables in the database, supporting runtime loading, synchronization, and auditing.
This bundle provides a secure and flexible way to manage application configuration through database-stored environment variables, with automatic loading for HTTP requests, CLI commands, and message workers.
Table of Contents
- Features
- Requirements
- Installation
- Quick Start
- Configuration
- Events and Extension Points
- Advanced Usage
- Documentation
- Testing
- Contributing
- Security
- Credits
- License
- Changelog
Features
- Database-driven Configuration: Store and manage environment variables in the database
- Runtime Loading: Automatically load variables for HTTP requests, CLI commands, and message workers
- Security First: Built-in protection against dangerous environment variables (LD_PRELOAD, APP_*, etc.)
- Full Audit Trail: Track all changes with Snowflake IDs, user, IP, and timestamps
- Fine-grained Control: Enable/disable individual variables, add remarks, control synchronization
- Cache Layer: High-performance caching with automatic invalidation on changes
- Admin Interface: Ready-to-use EasyAdmin CRUD controller for management
- JSON-RPC Support: Built-in procedure for fetching public configuration
- Twig Integration: Access environment variables directly in templates
Requirements
- PHP >= 8.1
- Symfony >= 6.4
- Doctrine ORM >= 2.20
Installation
Install via Composer
composer require tourze/env-manage-bundle
Enable the Bundle
Usually auto-registered via Symfony Flex. If not, add to config/bundles.php:
Tourze\EnvManageBundle\EnvManageBundle::class => ['all' => true],
Quick Start
1. Create the Database Table
# Generate migration php bin/console make:migration # Run migration to create base_env table php bin/console doctrine:migrations:migrate
2. Add Environment Variables
use Tourze\EnvManageBundle\Entity\Env; use Doctrine\ORM\EntityManagerInterface; $env = new Env(); $env->setName('API_ENDPOINT'); $env->setValue('https://api.example.com'); $env->setValid(true); $env->setSync(false); $env->setRemark('External API endpoint'); $entityManager->persist($env); $entityManager->flush();
3. Access Variables
// In services $apiEndpoint = $_ENV['API_ENDPOINT'] ?? 'default'; // Using the service /** @var \Tourze\EnvManageBundle\Service\EnvService $envService */ $publicVars = $envService->fetchPublicArray(); // In Twig templates {{ env_value('API_ENDPOINT') }}
Configuration
Security Configuration
The bundle automatically blocks dangerous environment variables:
LD_PRELOAD- Prevents injection attacksAPP_*- Protects Symfony core configurationDATABASE_*- Prevents database credential overrideREDIS_*- Protects cache configurationJWT_*- Secures authentication tokensMESSENGER_*- Protects message queue configurationLOCK_*- Prevents lock mechanism tampering
Admin Interface
Add to your EasyAdmin dashboard:
# config/packages/easy_admin.yaml easy_admin: entities: Env: class: Tourze\EnvManageBundle\Entity\Env controller: Tourze\EnvManageBundle\Controller\Admin\EnvCrudController
Caching
Environment variables are cached for 24 hours and automatically invalidated when:
- Any environment variable is created, updated, or deleted
- Cache is manually cleared
- Application is deployed
Events and Extension Points
Event Listeners
The bundle listens to:
KernelEvents::REQUEST- Load variables for HTTP requestsWorkerStartedEvent- Load variables for message workersConsoleEvents::COMMAND- Load variables for CLI commands (except cache commands)
Entity Events
Doctrine entity listeners automatically clear cache on:
postPersist- After creating new variablespostUpdate- After updating variablespostRemove- After deleting variables
Advanced Usage
Custom Environment Service
use Tourze\EnvManageBundle\Service\EnvService; class MyEnvService implements EnvService { public function fetchPublicArray(): array { // Custom logic for public variables } }
JSON-RPC Integration
// Expose environment variables via JSON-RPC $procedure = new GetEnvConfig($envService); $result = $procedure->execute();
Documentation
- Entity Design - Database schema and entity details
- Workflow - Visual workflow diagrams
- API Reference - Complete API documentation
Testing
# Run tests ./vendor/bin/phpunit packages/env-manage-bundle/tests # Run static analysis php -d memory_limit=2G ./vendor/bin/phpstan analyse packages/env-manage-bundle
Contributing
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Development Standards
- Follow PSR-12 coding standards
- Write tests for new features
- Run PHPStan (level 5) before submitting
- Update documentation as needed
- Add meaningful commit messages
Security
If you discover any security-related issues, please email security@tourze.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Copyright (c) tourze. Please see License File for more information.
Changelog
See Releases for version history and upgrade notes.