Go! AOP PHP 框架技术文档
2024-12-25 05:37:30作者:范靓好Udolf
1. 安装指南
1.1 使用 Composer 安装
Go! AOP 框架可以通过 Composer 进行安装。以下是安装步骤:
-
使用 Composer 下载框架及其依赖:
composer require goaop/framework -
Composer 会将框架安装到项目的
vendor/goaop/framework目录中。
1.2 创建应用的 Aspect Kernel
为了在应用中集成 AOP,首先需要创建一个 AspectKernel 类。这个类将管理应用中的所有切面。
创建应用的 AspectKernel 类,继承自 Go\Core\AspectKernel:
<?php
// app/ApplicationAspectKernel.php
use Go\Core\AspectKernel;
use Go\Core\AspectContainer;
class ApplicationAspectKernel extends AspectKernel
{
protected function configureAop(AspectContainer $container)
{
// 在这里注册切面
}
}
1.3 在前端控制器中配置 Aspect Kernel
在应用的前端控制器中初始化 AspectKernel:
// front-controller, 例如 Symfony2 的 web/app_dev.php
include __DIR__ . '/vendor/autoload.php'; // 使用 Composer 自动加载
$applicationAspectKernel = ApplicationAspectKernel::getInstance();
$applicationAspectKernel->init([
'debug' => true, // 使用 'false' 用于生产模式
'appDir' => __DIR__ . '/..', // 应用根目录
'cacheDir' => __DIR__ . '/path/to/cache/for/aop', // AOP 缓存目录
'includePaths' => [
__DIR__ . '/../src/'
]
]);
2. 项目使用说明
2.1 创建切面
切面是 AOP 的核心概念。Go! AOP 框架使用简单的 PHP 类来定义切面。以下是一个示例,展示如何拦截所有方法并显示它们的名称:
// Aspect/MonitorAspect.php
namespace Aspect;
use Go\Aop\Aspect;
use Go\Aop\Intercept\MethodInvocation;
use Go\Lang\Annotation\Before;
class MonitorAspect implements Aspect
{
/**
* 在真实方法执行前调用
*
* @param MethodInvocation $invocation 调用
* @Before("execution(public Example->*(*))")
*/
public function beforeMethodExecution(MethodInvocation $invocation)
{
echo '调用前拦截器: ',
$invocation,
' 参数: ',
json_encode($invocation->getArguments()),
"<br>\n";
}
}
2.2 注册切面
在 AspectKernel 的 configureAop 方法中注册切面:
// app/ApplicationAspectKernel.php
use Aspect\MonitorAspect;
protected function configureAop(AspectContainer $container)
{
$container->registerAspect(new MonitorAspect());
}
3. 项目 API 使用文档
3.1 切面类型
Go! AOP 框架支持多种切面类型,包括:
- Before: 在方法执行前调用。
- After: 在方法执行后调用。
- Around: 在方法执行前后调用,可以修改返回值。
3.2 切点语法
切点语法用于定义切面的应用位置。例如:
execution(public Example->*(*)): 拦截Example类中的所有公共方法。execution(static Example::*(*)): 拦截Example类中的所有静态方法。
4. 项目安装方式
Go! AOP 框架的安装方式非常简单,只需通过 Composer 安装即可。安装完成后,按照上述步骤配置 AspectKernel 和切面即可开始使用。
4.1 可选配置
- 自定义注解缓存: 可以通过
annotationCache配置选项使用其他缓存引擎。 - 支持 Doctrine 实体编织: 提供了实验性的支持,需要注册事件订阅器来解决映射问题。
通过以上步骤,您可以轻松地在 PHP 项目中集成 Go! AOP 框架,实现面向切面编程。
登录后查看全文
热门项目推荐
相关项目推荐
暂无数据
项目优选
收起
deepin linux kernel
C
27
11
OpenHarmony documentation | OpenHarmony开发者文档
Dockerfile
540
3.77 K
Ascend Extension for PyTorch
Python
351
415
本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。
C++
889
612
openEuler内核是openEuler操作系统的核心,既是系统性能与稳定性的基石,也是连接处理器、设备与服务的桥梁。
C
338
185
openJiuwen agent-studio提供零码、低码可视化开发和工作流编排,模型、知识库、插件等各资源管理能力
TSX
987
253
openGauss kernel ~ openGauss is an open source relational database management system
C++
169
233
暂无简介
Dart
778
193
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
1.35 K
758
华为昇腾面向大规模分布式训练的多模态大模型套件,支撑多模态生成、多模态理解。
Python
115
141