|
| 1 | +# AopBundle |
| 2 | + |
| 3 | +[](https://packagist.org/packages/tourze/symfony-aop-bundle) |
| 4 | + |
| 5 | +[English](README.md) | [中文](README.zh-CN.md) |
| 6 | + |
| 7 | +## Overview |
| 8 | + |
| 9 | +AopBundle is a Symfony bundle that implements Aspect-Oriented Programming (AOP) using PHP 8's Attribute features. It enables adding cross-cutting concerns (like logging, caching, transactions) to your code without modifying the core logic. |
| 10 | + |
| 11 | +## Requirements |
| 12 | + |
| 13 | +- PHP 8.1+ |
| 14 | +- Symfony 6.4+ |
| 15 | + |
| 16 | +## Installation |
| 17 | + |
| 18 | +```bash |
| 19 | +composer require tourze/symfony-aop-bundle |
| 20 | +``` |
| 21 | + |
| 22 | +## Features |
| 23 | + |
| 24 | +- Define aspects using PHP 8 attributes |
| 25 | +- Support for multiple advice types (Before, After, AfterReturning, AfterThrowing) |
| 26 | +- Join point context with access to method parameters and return values |
| 27 | +- Powerful pointcut expressions for targeting specific services/methods |
| 28 | +- Built-in stopwatch support for performance monitoring |
| 29 | +- Exception handling capabilities |
| 30 | + |
| 31 | +## Basic Usage |
| 32 | + |
| 33 | +### 1. Define an Aspect |
| 34 | + |
| 35 | +Create a class with the `#[Aspect]` attribute and define advice methods: |
| 36 | + |
| 37 | +```php |
| 38 | +<?php |
| 39 | + |
| 40 | +namespace App\Aspect; |
| 41 | + |
| 42 | +use Tourze\Symfony\Aop\Attribute\Aspect; |
| 43 | +use Tourze\Symfony\Aop\Attribute\Before; |
| 44 | +use Tourze\Symfony\Aop\Model\JoinPoint; |
| 45 | + |
| 46 | +#[Aspect] |
| 47 | +class LoggingAspect |
| 48 | +{ |
| 49 | + #[Before('class.getName() === "App\\Service\\UserService" && method.getName() === "createUser"')] |
| 50 | + public function logBefore(JoinPoint $joinPoint): void |
| 51 | + { |
| 52 | + // Logging logic before method execution |
| 53 | + } |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +### 2. Available Advice Types |
| 58 | + |
| 59 | +- `#[Before]` - Executed before the target method |
| 60 | +- `#[After]` - Executed after the target method (regardless of exceptions) |
| 61 | +- `#[AfterReturning]` - Executed after successful method return |
| 62 | +- `#[AfterThrowing]` - Executed when the target method throws an exception |
| 63 | +- `#[CatchException]` - Used for exception handling |
| 64 | + |
| 65 | +### 3. JoinPoint |
| 66 | + |
| 67 | +The `JoinPoint` object provides context for the intercepted method: |
| 68 | + |
| 69 | +```php |
| 70 | +$joinPoint->getInstance(); // The service instance |
| 71 | +$joinPoint->getMethod(); // Method name being executed |
| 72 | +$joinPoint->getParams(); // Method parameters |
| 73 | +$joinPoint->getReturnValue(); // Return value (for after advice) |
| 74 | +$joinPoint->getException(); // Exception (for exception advice) |
| 75 | +$joinPoint->proceed(); // Manually execute the original method |
| 76 | +``` |
| 77 | + |
| 78 | +### 4. Pointcut Expressions |
| 79 | + |
| 80 | +Define where advice should be applied: |
| 81 | + |
| 82 | +```php |
| 83 | +// Match by service ID |
| 84 | +#[Before(serviceIds: ['app.user_service'])] |
| 85 | + |
| 86 | +// Match by class attribute |
| 87 | +#[AfterThrowing(classAttribute: CatchException::class)] |
| 88 | + |
| 89 | +// Match by method attribute |
| 90 | +#[After(methodAttribute: SomeAttribute::class)] |
| 91 | + |
| 92 | +// Match by service tags |
| 93 | +#[Before(serviceTags: ['app.loggable'])] |
| 94 | + |
| 95 | +// Match by parent class |
| 96 | +#[Before(parentClasses: [BaseRepository::class])] |
| 97 | + |
| 98 | +// Custom expression |
| 99 | +#[Before('class.getName() === "App\\Service\\UserService"')] |
| 100 | +``` |
| 101 | + |
| 102 | +### 5. Performance Monitoring |
| 103 | + |
| 104 | +```php |
| 105 | +use Tourze\Symfony\Aop\Attribute\Stopwatch; |
| 106 | + |
| 107 | +class UserService |
| 108 | +{ |
| 109 | + #[Stopwatch] |
| 110 | + public function complexOperation() |
| 111 | + { |
| 112 | + // Time-consuming operation |
| 113 | + } |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +## Best Practices |
| 118 | + |
| 119 | +1. **Keep aspects focused** |
| 120 | + - Each aspect should have a single responsibility |
| 121 | + - Avoid heavy operations in advice methods |
| 122 | + |
| 123 | +2. **Advice execution order** |
| 124 | + - Before advice is executed in declaration order |
| 125 | + - After/AfterReturning/AfterThrowing advice is executed in reverse order |
| 126 | + |
| 127 | +3. **Performance considerations** |
| 128 | + - Use AOP only when necessary |
| 129 | + - Consider the performance impact in production |
| 130 | + - Use Stopwatch to monitor method execution time |
| 131 | + |
| 132 | +4. **Exception handling** |
| 133 | + - Be careful with exception handling in advice |
| 134 | + - Consider using AfterThrowing for exception logging |
| 135 | + - Use CatchException for controlled exception handling |
| 136 | + |
| 137 | +## Configuration |
| 138 | + |
| 139 | +No additional configuration is required. The bundle automatically detects services with the `#[Aspect]` attribute and applies advice to matching services. |
| 140 | + |
| 141 | +## Contributing |
| 142 | + |
| 143 | +Contributions are welcome. Please feel free to submit a Pull Request. |
| 144 | + |
| 145 | +## License |
| 146 | + |
| 147 | +This bundle is available under the MIT license. See the [LICENSE](LICENSE) file for more information. |
0 commit comments