tourze / symfony-aop-async-bundle
Async AOP Bundle for Symfony
Installs: 1 921
Dependents: 9
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
Type:symfony-bundle
pkg:composer/tourze/symfony-aop-async-bundle
Requires
- php: ^8.1
- psr/log: ^3|^2|^1
- symfony/config: ^6.4
- symfony/dependency-injection: ^6.4
- symfony/framework-bundle: ^6.4
- symfony/http-kernel: ^6.4
- symfony/messenger: ^6.4
- symfony/yaml: ^6.4 || ^7.1
- tourze/async-service-call-bundle: 0.0.*
- tourze/bundle-dependency: 0.0.*
- tourze/symfony-aop-bundle: ~0.0.4
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^10.0
This package is auto-updated.
Last update: 2025-10-31 20:04:13 UTC
README
A Symfony bundle that provides asynchronous method execution using AOP (Aspect-Oriented Programming) and Symfony Messenger.
Features
- Easy async execution: Mark any public method with
#[Async]attribute to execute asynchronously - AOP-based implementation: Uses aspect-oriented programming for clean and transparent async execution
- Symfony Messenger integration: Leverages Symfony Messenger for reliable message processing
- Configurable retry and delay: Support for retry count and execution delay
- Graceful fallback: Falls back to synchronous execution if async dispatch fails
Installation
composer require tourze/symfony-aop-async-bundle
Quick Start
- Enable the bundle in your
config/bundles.php:
return [ // ... other bundles Tourze\Symfony\AopAsyncBundle\AopAsyncBundle::class => ['all' => true], ];
- Mark methods for async execution:
use Tourze\Symfony\AopAsyncBundle\Attribute\Async; class EmailService { #[Async] public function sendWelcomeEmail(string $userEmail): void { // This method will be executed asynchronously // Send email logic here... } #[Async(retryCount: 3, delayMs: 5000)] public function processLargeDataset(array $data): void { // This method will be retried up to 3 times // and delayed by 5 seconds before execution } }
- Use the service normally:
class UserController extends AbstractController { public function register(EmailService $emailService): Response { // This call returns immediately, email is sent asynchronously $emailService->sendWelcomeEmail('user@example.com'); return new Response('User registered successfully'); } }
Configuration
Async Attribute Options
The #[Async] attribute supports the following options:
retryCount(int, default: 0): Number of times to retry the method if it failsdelayMs(int, default: 0): Delay in milliseconds before executing the method
#[Async(retryCount: 5, delayMs: 10000)] public function criticalTask(): void { // This task will be retried up to 5 times // and delayed by 10 seconds before execution }
Messenger Configuration
Configure Symfony Messenger in your config/packages/messenger.yaml:
framework: messenger: transports: async: '%env(MESSENGER_TRANSPORT_DSN)%' routing: 'Tourze\AsyncServiceCallBundle\Message\ServiceCallMessage': async
Limitations
To keep the implementation simple, the following limitations apply:
- Public methods only: The
#[Async]attribute only works on public methods due to AOP limitations - Object serialization: Complex nested objects and object arrays may not serialize properly
- No transaction inheritance: Async methods don't inherit database transactions
- Exception handling: Exceptions in async methods are logged but not propagated to the caller
How It Works
- When a method marked with
#[Async]is called, the AsyncAspect intercepts the call - The method call is serialized into a ServiceCallMessage
- The message is dispatched to Symfony Messenger with optional delay/retry stamps
- The original method call returns immediately without executing the method body
- The message is processed asynchronously by a Messenger worker
- If async dispatch fails, the method falls back to synchronous execution
Testing
Run the test suite:
./vendor/bin/phpunit packages/symfony-aop-async-bundle/tests
Dependencies
This bundle requires:
tourze/symfony-aop-bundle: Provides AOP functionalitytourze/async-service-call-bundle: Handles async service callssymfony/messenger: Message queue system
Contributing
Please see CONTRIBUTING.md for details.
Security
If you discover any security related issues, please create an issue in our GitHub repository.
License
The MIT License (MIT). Please see License File for more information.