AOP插件

v0.0.1 版本
2022-01-13 版本更新时间
86 安装
2 star

简介

同时支持直接new和从容器获取需要被切入的对象,也支持三方库的切入vendor目录下的类方法, 在不改变现有代码的情况下切入需要切入的前置后置方法,用在统计http请求,Rpc,组件链路追踪,日志记录,统计函数耗时 修改函数返回结果的应用场景

安装

composer require xiaoyangguang/aop

使用

自定义切面 照着Xiaoyangguang\Aop\Examplex写


<?php

namespace Xiaoyangguang\Aop\Example;

use Xiaoyangguang\Aop\AspectInterface;

class TAspect implements AspectInterface
{
    /**
     * 前置通知
     * @param $params
     * @param $method
     * @return mixed|void
     */
    public static function beforeAdvice(&$params, $class, $method): void
    {
        var_dump('beforeAdvice');
    }

    /**
     * 后置通知
     * @param $res
     * @param $params
     * @param $method
     * @return mixed|void
     */
    public static function afterAdvice(&$res, $params, $class, $method): void
    {
        var_dump('afterAdvice');
    }

    public static function exceptionHandler($throwable, $params, $class, $method): void
    {
        // TODO: Implement exceptionHandler() method.
    }
}
  1. 配置config\plugin\xiaoyangguang\aop\aop.php文件
  2. 自定义切入类需要实现Xiaoyangguang\Aop\AspectInterface接口并配置到以上aop配置文件中
  3. 最后启动服务。

效果

php start.php start
curl  http://127.0.0.1:8787
此时控制台打印前置和后置切面函数打印值
string(12) "beforeAdvice"
string(11) "afterAdvice"