YII2框架集成go!aop

AOP实践:
YII2框架自己拥有一个功能,叫作行为.它能够动态的为当前的类附加额外的功能,但这种功能在代码层级结构是静态的,有侵入性的。

下面以YII2框架集成go!aop库为例,介绍在YII2中如何实现AOP编程.(go!aop简介,能够参考go!aop的官网.)

因为YII框架拥有本身的类加载器,所在集成go!aop的时候,不能正常的工做,因此要将其禁用掉,使用composer提供的类加载器。
以下代码所示(这里使用YII2高级应用模板):

一、找到  spl_autoload_register(['Yii', 'autoload'], true, true);  (PROJECT_PATH/vendor/yiisoft/yii2/Yii.php) 将其禁用掉.php

二、执行  composer require goaop/frameworkhtml

三、修改composer.json文件,加入以下代码段:编程

"autoload": {
       "psr-4": {
         "backend\\": "backend//",
         "frontend\\": "frontend//",
         "common\\": "common//"
       }
 }

四、 在frontend 目录下建立一个components是目录,并新建一个类AopAspectKernel,例如:json

namespace frontend\components;
use frontend\aspects\MonitorAspect;
use Go\Core\AspectContainer;
use Go\Core\AspectKernel;
class AopAspectKernel extends AspectKernel
{
        protected function configureAop(AspectContainer $container)
        {
            $container->registerAspect(new MonitorAspect());
        }
}

五、在forntend目录下在新建一个类InitAopComponent,并使其实现BootstrapInterface,使其能够在YII2框架引导时被自动引导 bootstrap

namespace frontend\components;
use yii\base\BootstrapInterface;
class InitAopComponent implements BootstrapInterface
{
        public function bootstrap($app)
        {
            print_r(\Yii::$app->params['aop']);
            $applicationAspectKernel = AopAspectKernel::getInstance();
            $applicationAspectKernel->init(\Yii::$app->params['aop']);
        }
}

六、在frontend/config/params.php中新增以下代码:数组

'aop' => [
       'debug' => true,
       'appDir' => dirname(__DIR__),
       'cacheDir' => dirname(__DIR__) . '/runtime/aop',
       'includePaths' => [
           dirname(__DIR__)
       ]
   ]

七、在frontend下面新建aspects目录,并新建类MonitorAspect,代码以下:yii2

namespace frontend\aspects;
 
use Go\Aop\Aspect;
use Go\Aop\Intercept\MethodInvocation;
use Go\Lang\Annotation\Before;
class MonitorAspect implements Aspect
{
        /**
         * Method that will be called before real method
         *
         * @param MethodInvocation $invocation Invocation
         * @Before("execution(public frontend\components\AopTestComponent->*(*))")
         */
        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";
        }
}

八、修改frontend/config/main.php文件,并在components数组下新增一个key,代码以下:app

'components'=>[
       'aop' => [
           'class' => 'frontend\components\InitAopComponent'
       ]
   ]  

九、修改frontend/config/main.php文件,并在bootstrap数组下新增aop值,代码以下:composer

 'bootstrap'=>['log','aop'] 框架

原文连接:https://www.cnblogs.com/cmacro/p/9327602.html

参考连接:https://blog.csdn.net/guiyecheng/article/details/56016791

相关文章
相关标签/搜索