tourze/symfony-aop-coroutine-bundle

协程支持

0.1.0 2025-05-04 20:15 UTC

This package is auto-updated.

Last update: 2025-05-09 09:20:27 UTC


README

English | 中文

Latest Version Build Status Quality Score Total Downloads

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

  1. Mark your service as coroutine-enabled:
use Tourze\Symfony\AopCoroutineBundle\Attribute\AsCoroutine;

#[AsCoroutine]
class UserService
{
    private $state;
    public function setState($state) { $this->state = $state; }
}
  1. 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.