资讯专栏INFORMATION COLUMN

Swoft 源码剖析 - Swoft 中 AOP 的实现原理

chenjiang3 / 2308人阅读

摘要:官方在文档没有提供完整的但我们还是可以在单元测试中找得到的用法。解决的问题是分散在引用各处的横切关注点。横切关注点指的是分布于应用中多处的功能,譬如日志,事务和安全。通过将真正执行操作的对象委托给实现了能提供许多功能。源码剖析系列目录

作者:bromine
链接:https://www.jianshu.com/p/e13...
來源:简书
著作权归作者所有,本文已获得作者授权转载,并对原文进行了重新的排版。
Swoft Github: https://github.com/swoft-clou...

前言

AOP(面向切面编程)一方面是是开闭原则的良好实践,你可以在不修改代码的前提下为项目添加功能;更重要的是,在面向对象以外,他提供你另外一种思路去复用你的琐碎代码,并将其和你的业务代码风格开。

初探AOP

AOP是被Spring发扬光大的一个概念,在Java Web的圈子内可谓无人不晓,但是在PHP圈内其实现甚少,因此很多PHPer对相关概念很陌生。且Swoft文档直接说了一大堆术语如AOP切面切面通知连接点切入点,却只给了一个关于Aspect(切面)的示例。没有接触过AOP的PHPer对于此肯定是一头雾水的。考虑到这点我们先用一点小篇幅来谈谈相关知识,熟悉的朋友可以直接往后跳。

基于实践驱动学习的理念,这里我们先不谈概念,先帮官网把示例补全。官方在文档没有提供完整的AOP Demo,但我们还是可以在单元测试中找得到的用法。

这里是Aop的其中一个单元测试,这个测试的目的是检查AopTest->doAop()的返回值是否是:
"do aop around-before2 before2 around-after2 afterReturn2 around-before1 before1 around-after1 afterReturn1 "

//SwoftTestCasesAopTest.php
class AopTest extends TestCase
{
    public function testAllAdvice()
    {
        /* @var SwoftTestingAopAopBean $aopBean*/
        $aopBean = App::getBean(AopBean::class);
        $result = $aopBean->doAop();
        //此处是PHPUnit的断言语法,他判断AopBean Bean的doAop()方法的返回值是否是符合预期
        $this->assertEquals("do aop around-before2  before2  around-after2  afterReturn2  around-before1  before1  around-after1  afterReturn1 ", $result);
    }
} 

上面的测试使用到了AopBean::class这个Bean。这个bean有一个很简单的方法doAop(),直接返回一串固定的字符串"do aop";


发现问题了没?单元测试中$aopBean没有显式的使用编写AOP相关代码,而$aopBean->doAop()的返回值却被改写了。
这就是AOP的威力了,他可以以一种完全无感知无侵入的方式去拓展你的功能。但拓展代码并不完全是AOP的目的,AOP的意义在于分离你的零碎关注点,以一种面向对象外的思路去组织和复用你的各种零散逻辑。

AOP解决的问题是分散在引用各处的横切关注点横切关注点指的是分布于应用中多处的功能,譬如日志,事务和安全。通常来说横切关注点本身是和业务逻辑相分离的,但按照传统的编程方式,横切关注点只能零散的嵌入到各个逻辑代码中。因此我们引入了AOP,他不仅提供一种集中式的方式去管理这些横切关注点,而且分离了核心的业务代码和横切关注点,横切关注点的修改不再需要修改核心代码。

回到官方给的切面实例

test .= " before1 ";
    }

    //other code....
}

上面的AllPointAspect主要使用了3个注解去描述一个切面(Aspect)
@Aspect声明这是一个切面(Aspect)类,一组被组织起来的横切关注点。
@Before声明了一个通知(Advice)方法,即切面要干什么什么时候执行
@PointBean声明了一个切点(PointCut):即 切面(Aspect)在何处执行通知(Advice)能匹配哪些连接点

关于AOP的更多知识可以阅读

动态代理 代理模式

代理模式(Proxy /Surrogate)是GOF系23种设计模式中的其中一种。其定义为:

为对象提供一个代理,以控制对这个对象的访问。

其常见实现的序列图类图如下
序列图

类图

RealSubject是真正执行操作的实体
Subject是从RealSubject中抽离出的抽象接口,用于屏蔽具体的实现类
Proxy是代理,实现了Subject接口,一般会持有一个RealSubjecy实例,将Client调用的方法委托给RealSubject真正执行。

通过将真正执行操作的对象委托给实现了Proxy能提供许多功能。
远程代理(Remote Proxy/Ambassador):为一个不同地址空间的实例提供本地环境的代理,隐藏远程通信等复杂细节。
保护代理(Protection Proxy)对RealSubject的访问提供权限控制等额外功能。
虚拟代理(Virtual Proxy)根据实际需要创建开销大的对象
智能引用(Smart Reference)可以在访问对象时添加一些附件操作。

更多可阅读《设计模式 可复用面向对象软件的基础》的第四章

动态代理

一般而言我们使用的是静态代理,即:在编译期前通过手工或者自动化工具预先生成相关的代理类源码。
这不仅大大的增加了开发成本和类的数量,而且缺少弹性。因此AOP一般使用的代理类都是在运行期动态生成的,也就是动态代理

Swoft中的AOP

回到Swoft,之所以示例中$aopBeandoAop()能被拓展的原因就是App::getBean(AopBean::class)返回的并不是AopBean的真正实例,而是一个持有AopBean对象的动态代理
Container->set()方法是App::getBean()底层实际创建bean的方法。

//SwoftBeanContainer.php
/**
 * 创建Bean
 *
 * @param string           $name             名称
 * @param ObjectDefinition $objectDefinition bean定义
 * @return object
 * @throws ReflectionException
 * @throws InvalidArgumentException
 */
private function set(string $name, ObjectDefinition $objectDefinition)
{
    //低相关code...

    //注意此处,在返回前使用了一个Aop动态代理对象包装并替换实际对象,所以我们拿到的Bean都是Proxy
    if (!$object instanceof AopInterface) {
        $object = $this->proxyBean($name, $className, $object);//
    }

    //低相关code ....
    return $object;
}

Container->proxyBean()的主要操作有两个

调用对Bean的各个方法调用Aop->match();根据切面定义的切点获取其合适的通知,并注册到Aop->map中

//SwoftAopAop.php
/**
 * Match aop
 *
 * @param string $beanName    Bean name
 * @param string $class       Class name
 * @param string $method      Method name
 * @param array  $annotations The annotations of method
 */
public function match(string $beanName, string $class, string $method, array $annotations)
{
    foreach ($this->aspects as $aspectClass => $aspect) {
        if (! isset($aspect["point"]) || ! isset($aspect["advice"])) {
            continue;
        }

        //下面的代码根据各个切面的@PointBean,@PointAnnotation,@PointExecution 进行连接点匹配
        // Include
        $pointBeanInclude = $aspect["point"]["bean"]["include"] ?? [];
        $pointAnnotationInclude = $aspect["point"]["annotation"]["include"] ?? [];
        $pointExecutionInclude = $aspect["point"]["execution"]["include"] ?? [];

        // Exclude
        $pointBeanExclude = $aspect["point"]["bean"]["exclude"] ?? [];
        $pointAnnotationExclude = $aspect["point"]["annotation"]["exclude"] ?? [];
        $pointExecutionExclude = $aspect["point"]["execution"]["exclude"] ?? [];

        $includeMath = $this->matchBeanAndAnnotation([$beanName], $pointBeanInclude) || $this->matchBeanAndAnnotation($annotations, $pointAnnotationInclude) || $this->matchExecution($class, $method, $pointExecutionInclude);

        $excludeMath = $this->matchBeanAndAnnotation([$beanName], $pointBeanExclude) || $this->matchBeanAndAnnotation($annotations, $pointAnnotationExclude) || $this->matchExecution($class, $method, $pointExecutionExclude);

        if ($includeMath && ! $excludeMath) {
            //注册该方法级别的连接点适配的各个通知
            $this->map[$class][$method][] = $aspect["advice"];
        }
    }
}

通过Proxy::newProxyInstance(get_class($object),new AopHandler($object))构造一个动态代理

//SwoftProxyProxy.php
/**
 * return a proxy instance
 *
 * @param string           $className
 * @param HandlerInterface $handler
 *
 * @return object
 */
public static function newProxyInstance(string $className, HandlerInterface $handler)
{
    $reflectionClass   = new ReflectionClass($className);
    $reflectionMethods = $reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED);

    // the template of methods
    $id             = uniqid();
    $proxyClassName = basename(str_replace("", "/", $className));
    $proxyClassName = $proxyClassName . "_" . $id;
    //动态类直接继承RealSubject
    $template
        = "class $proxyClassName extends $className {
        private $hanadler;
        public function __construct($handler)
        {
            $this->hanadler = $handler;
        }
    ";
    // the template of methods
    //proxy类会重写所有非static非构造器函数,将实现改为调用给$handler的invoke()函数
    $template .= self::getMethodsTemplate($reflectionMethods);
    $template .= "}";
    //通过动态生成的源码构造一个动态代理类,并通过反射获取动态代理的实例
    eval($template);
    $newRc = new ReflectionClass($proxyClassName);

    return $newRc->newInstance($handler);
}

构造动态代理需要一个SwoftProxyHandlerHandlerInterface实例作为$handler参数,AOP动态代理使用的是AopHandler,其invoke()底层的关键操作为Aop->doAdvice()

//SwoftAopAop.php
/**
 * @param object $target  Origin object
 * @param string $method  The execution method
 * @param array  $params  The parameters of execution method
 * @param array  $advices The advices of this object method
 * @return mixed
 * @throws ReflectionException|Throwable
 */
public function doAdvice($target, string $method, array $params, array $advices)
{
    $result = null;
    $advice = array_shift($advices);

    try {

        // Around通知条用
        if (isset($advice["around"]) && ! empty($advice["around"])) {
            $result = $this->doPoint($advice["around"], $target, $method, $params, $advice, $advices);
        } else {
            // Before
            if ($advice["before"] && ! empty($advice["before"])) {
                // The result of before point will not effect origin object method
                $this->doPoint($advice["before"], $target, $method, $params, $advice, $advices);
            }
            if (0 === count($advices)) {
                 //委托请求给Realsuject
                $result = $target->$method(...$params);
            } else {
                //调用后续切面
                $this->doAdvice($target, $method, $params, $advices);
            }
        }

        // After
        if (isset($advice["after"]) && ! empty($advice["after"])) {
            $this->doPoint($advice["after"], $target, $method, $params, $advice, $advices, $result);
        }
    } catch (Throwable $t) {
        if (isset($advice["afterThrowing"]) && ! empty($advice["afterThrowing"])) {
            return $this->doPoint($advice["afterThrowing"], $target, $method, $params, $advice, $advices, null, $t);
        } else {
            throw $t;
        }
    }

    // afterReturning
    if (isset($advice["afterReturning"]) && ! empty($advice["afterReturning"])) {
        return $this->doPoint($advice["afterReturning"], $target, $method, $params, $advice, $advices, $result);
    }

    return $result;
}

通知的执行(Aop->doPoint())也很简单,构造ProceedingJoinPoint,JoinPoint,Throwable对象,并根据通知的参数声明注入。

//SwoftAopAop.php
/**
 * Do pointcut
 *
 * @param array  $pointAdvice the pointcut advice
 * @param object $target      Origin object
 * @param string $method      The execution method
 * @param array  $args        The parameters of execution method
 * @param array  $advice      the advice of pointcut
 * @param array  $advices     The advices of this object method
 * @param mixed  $return
 * @param Throwable $catch    The  Throwable object caught
 * @return mixed
 * @throws ReflectionException
 */
private function doPoint(
    array $pointAdvice,
    $target,
    string $method,
    array $args,
    array $advice,
    array $advices,
    $return = null,
    Throwable $catch = null
) {
    list($aspectClass, $aspectMethod) = $pointAdvice;

    $reflectionClass = new ReflectionClass($aspectClass);
    $reflectionMethod = $reflectionClass->getMethod($aspectMethod);
    $reflectionParameters = $reflectionMethod->getParameters();

    // Bind the param of method
    $aspectArgs = [];
    foreach ($reflectionParameters as $reflectionParameter) {
        //用反射获取参数类型,如果是JoinPoint,ProceedingJoinPoint,或特定Throwable,则注入,否则直接传null
        $parameterType = $reflectionParameter->getType();
        if ($parameterType === null) {
            $aspectArgs[] = null;
            continue;
        }

        // JoinPoint object
        $type = $parameterType->__toString();
        if ($type === JoinPoint::class) {
            $aspectArgs[] = new JoinPoint($target, $method, $args, $return, $catch);
            continue;
        }

        // ProceedingJoinPoint object
        if ($type === ProceedingJoinPoint::class) {
            $aspectArgs[] = new ProceedingJoinPoint($target, $method, $args, $advice, $advices);
            continue;
        }
        
        //Throwable object
        if (isset($catch) && $catch instanceof $type) {
            $aspectArgs[] = $catch;
            continue;
        }
        $aspectArgs[] = null;
    }

    $aspect = ean($aspectClass);

    return $aspect->$aspectMethod(...$aspectArgs);
}

以上就是AOP的整体实现原理了。

Swoft源码剖析系列目录:https://segmentfault.com/a/11...

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/30703.html

相关文章

  • Swoft 源码剖析 - 目录

    摘要:作者链接來源简书著作权归作者所有,本文已获得作者授权转载,并对原文进行了重新的排版。同时顺手整理个人对源码的相关理解,希望能够稍微填补学习领域的空白。系列文章只会节选关键代码辅以思路讲解,请自行配合源码阅读。 作者:bromine链接:https://www.jianshu.com/p/2f6...來源:简书著作权归作者所有,本文已获得作者授权转载,并对原文进行了重新的排版。Swoft...

    qpwoeiru96 评论0 收藏0
  • Swoft 源码剖析 - Swoft IOC 容器实现原理

    摘要:作者链接來源简书著作权归作者所有,本文已获得作者授权转载,并对原文进行了重新的排版。前言为应用提供一个完整的容器作为依赖管理方案,是功能,模块等功能的实现基础。的依赖注入管理方案基于服务定位器。源码剖析系列目录 作者:bromine链接:https://www.jianshu.com/p/a23...來源:简书著作权归作者所有,本文已获得作者授权转载,并对原文进行了重新的排版。Swof...

    Astrian 评论0 收藏0
  • Swoft 源码剖析 - Swoft 注解机制

    摘要:中的注解注解是里面很多重要功能特别是,容器的基础。主流的框架中使用的注解都是借用型注释块型注释中的定义自己的注解机制。在中是注解信息的最终装载容器。使用的信息构造实例或获取现有实例以上就是注解机制的整体实现了。源码剖析系列目录 作者:bromine链接:https://www.jianshu.com/p/ef7...來源:简书著作权归作者所有,本文已获得作者授权转载,并对原文进行了重新...

    zzbo 评论0 收藏0
  • Swoft 源码剖析 - RPC 功能实现

    摘要:值得一提的是目前的服务即服务,暂没有其他的服务功能,所以基本上相关的配置指代的就是。会将请求传递给各个中间件,最终最终传递给处理。源码剖析系列目录 作者:bromine链接:https://www.jianshu.com/p/411...來源:简书著作权归作者所有,本文已获得作者授权转载,并对原文进行了重新的排版。Swoft Github: https://github.com/swo...

    marser 评论0 收藏0
  • Swoft 源码剖析 - Swoole和Swoft那些事 (Http/Rpc服务篇)

    摘要:和服务关系最密切的进程是中的进程组,绝大部分业务处理都在该进程中进行。随后触发一个事件各组件通过该事件进行配置文件加载路由注册。事件每个请求到来时仅仅会触发事件。服务器生命周期和服务基本一致,详情参考源码剖析功能实现 作者:bromine链接:https://www.jianshu.com/p/4c0...來源:简书著作权归作者所有,本文已获得作者授权转载,并对原文进行了重新的排版。S...

    张汉庆 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<