tourze / symfony-aop-coroutine-bundle
协程支持
Installs: 18
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: ^8.1
- ext-session: *
- doctrine/orm: ^3.0
- monolog/monolog: ^3.1
- symfony/config: ^6.4
- symfony/dependency-injection: ^6.4
- symfony/event-dispatcher: ^6.4
- symfony/framework-bundle: ^6.4
- symfony/http-kernel: ^6.4
- symfony/service-contracts: ^3.5
- symfony/yaml: ^6.4 || ^7.1
- tourze/symfony-aop-bundle: ~0.0.4
- tourze/symfony-runtime-context-bundle: 0.0.*
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^10.0
README
A Symfony bundle providing coroutine-based service isolation via AOP, enabling each request context to have its own independent service instances.
Features
- Coroutine-based service isolation using AOP
- Easily mark services as coroutine-enabled with the
#[AsCoroutine]
attribute - Built-in coroutine support for common services (request_stack, twig, monolog, etc.)
- Automatic context management for each request, with resource cleanup
- Enhanced logging with coroutine context ID
Installation
- Requires PHP 8.1+
- Requires Symfony 6.4+
Install via Composer:
composer require tourze/symfony-aop-coroutine-bundle
Register the bundle in config/bundles.php
:
return [ Tourze\Symfony\AopCoroutineBundle\AopCoroutineBundle::class => ['all' => true], ];
Quick Start
- Mark your service as coroutine-enabled:
use Tourze\Symfony\AopCoroutineBundle\Attribute\AsCoroutine; #[AsCoroutine] class UserService { private $state; public function setState($state) { $this->state = $state; } }
- Inject and use the service in your controller. Each request will have its own isolated state.
Built-in Coroutine Services
The following services are coroutine-enabled by default:
- Request-related:
request_stack
,session_listener
,router_listener
- Twig:
twig
,twig.loader.native_filesystem
- Security:
security.untracked_token_storage
,security.csrf.token_storage
- Logging:
monolog.logger
,monolog.logger.*
- Others:
debug.stopwatch
, Doctrine EntityManager, Session Handler
Advanced Usage & Configuration
- You can enable coroutine for additional services via service tags, inheritance, or the
#[AsCoroutine]
attribute - Logging processor automatically adds
context_id
to all log records - Full support for coroutine-enabled Doctrine EntityManager and Session Handler
Usage Notes
- Coroutine services consume more memory; only use for services that require state isolation
- Methods returning
static
are not supported; singleton services are not recommended - Avoid circular dependencies in coroutine services
Contribution Guide
- Contributions are welcome via Issues and PRs
- Code style should follow PSR standards; please run tests before submitting PRs
- Run tests with:
./vendor/bin/phpunit packages/symfony-aop-coroutine-bundle/tests
License
- MIT License
- Author: tourze team
Changelog
See CHANGELOG or Git history for version updates and important changes.