icefox/aspect
Composer 安装命令:
composer require icefox/aspect
包简介
Aspect Oriented Programming for PHP
README 文档
README
A minimalistic Aspect-Oriented Programming (AOP) implementation for PHP that uses attributes and Composer's autoloading system to weave aspects into classes at runtime.
Installation
composer install icefox/aspect
Usage
Create an Aspect class:
namespace Tests\Aspects;
use Attribute;
#[Attribute(Attribute::TARGET_METHOD)]
class MyAspect
{
public function before(object|string $target, mixed ...$args): void
{
// $target is the class (for static methods) or object (for instance methods)
// Access all parameters before the wrapped function is called
var_dump($args);
}
public function after(object|string $target, mixed $result): mixed
{
// Access the returned value with $result
var_dump($result);
// modify the return value, or simply return the unmodified value
return $result;
}
}
Register all Aspect classes on application bootstrap:
$cacheDir =
$useCache = false;
$weaver = new AspectWeaver(
[MyAspect::class],
// provide a cache directory to avoid reprocessing class
sys_get_temp_dir() . '/cache/php-aop-cache';
// whether to use cache. Disable in dev, enable in production
false,
// PSR compatible log, for debugging.
new NullLogger(),
);
// Provide with classes or namespaces will be evaluated for Aspects
// trying to evaluate all vendor classes will result in a bad time
// at minimum, use withNamespaces to filter your application
// classes will be evaluated if they match the list of classes OR belong to the namespace
$loader = AspectBuilder::begin()
// list of class names
->withClasses([ MyClass::class ])
// namespace prefix
->withNamespaces([ "MyApplication\\" ])
->build($weaver)
->register();
Create a class and mark a method with your Aspect class:
<?php
class MyClass
{
#[MyAspect]
public function add(int $a, int $b): int
{
echo "Adding {$a} + {$b}\n";
return $a + $b;
}
}
Use the class normally
<?php
$object = new MyClass();
$result = $calc->add(5, 3);
/// MyAspect@before will var_dump([5, 3])
/// MyClass@add will echo "Adding 5 + 3\n"
/// MyAspect@after will var_dump(8)
Proxy Generation Strategy
The weaver uses a two-class approach:
- Original Class (renamed): The original class is included in the proxy file with a modified name (e.g.,
__AopOriginal_Calculator) - Proxy Class: A new class with the original name that extends the renamed original class and overrides methods marked with
#[Aspect]
TODO
Features that may or may not exist in the future
- Around Advice: Allow aspects to control method execution
- Demonstrate exception handling
- Demonstrate Aspect constructor arguments
References
统计信息
- 总下载量: 5
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-2.0-only
- 更新时间: 2026-01-05