lisachenko / yii-aspect
Yii 和 Go! aop 示例项目
dev-master
2013-09-28 15:11 UTC
Requires
- php: >=5.3.2
- lisachenko/go-aop-php: 0.4.1
- yiisoft/yii: 1.1.*
This package is auto-updated.
Last update: 2024-09-17 09:08:25 UTC
README
Yii 框架骨架和一些调整 + Go AOP PHP.
该项目允许您使用 Yii 框架测试 面向切面编程。
安装
安装过程非常简单,只需几个步骤
git clone https://github.com/lisachenko/yii-aspect.git && cd yii-aspect composer install
之后,添加一个 /app
文件夹作为 web 服务器的主目录,然后在浏览器中打开它。
切面定义
切面类定义在 TestMonitorAspect 类中
<?php namespace Aspect; use Go\Aop\Aspect; use Go\Aop\Intercept\FieldAccess; use Go\Aop\Intercept\MethodInvocation; use Go\Lang\Annotation\After; use Go\Lang\Annotation\Before; use Go\Lang\Annotation\Around; use Go\Lang\Annotation\Pointcut; /** * Monitor aspect */ class TestMonitorAspect implements Aspect { /** * Method that will be called before real method * * @param MethodInvocation $invocation Invocation * @Before("within(**)") */ public function beforeMethodExecution(MethodInvocation $invocation) { $obj = $invocation->getThis(); echo 'Calling Before Interceptor for method: ', is_object($obj) ? get_class($obj) : $obj, $invocation->getMethod()->isStatic() ? '::' : '->', $invocation->getMethod()->getName(), '()', ' with arguments: ', json_encode($invocation->getArguments()), "<br>\n"; } }
它是如何工作的?
AOP 的主要特性是不需要对原始源代码进行任何更改。有一个织入器根据切面中定义的规则修改原始类和方法的逻辑。所以,在我们的例子中,我们创建了一个建议(beforeMethodExecution() 方法的主体),它将接收 MethodInvocation 实例作为参数。下一步是决定建议应该在何处应用(这是一个切入点)。在这里,我们使用 "**" 定义了适用于所有公共和受保护的公共和受保护的公共和受保护的(动态和静态)方法的 "within" 切入点,这将匹配任何类。使用 "@Before" 注解来指定我们希望在原始方法之前调用我们的钩子。
每个切面都应该在应用程序中注册 切面内核。