yii-goaop - 将 goaop 集成到 Yii,在 Yii 中优雅的面向切面编程

yii-goaop - 将 goaop/framework 集成到 Yii,在 Yii 中优雅的面向切面编程。

项目地址

环境要求

  • Yii >= 2.0

安装

$ composer require guanguans/yii-goaop -vvv

composer.json add:

"autoload": {
    "psr-4": {
        "backend\\": "backend/",
        "frontend\\": "frontend/",
        "common\\": "common/",
        "console\\": "console/",
        "app\\": ""
    }
}
$ composer dumpautoload

配置

yii2-app-advanced

配置 config/main.php 文件中添加:

<?php

return [
    'bootstrap' => [
        'aop',
    ],
    'components' => [
        'aop' => [
            'class'   => 'Guanguans\YiiGoAop\GoAopComponent',
            'initOption'  => [
                // AOP Debug Mode
                'debug'          => false,
                // Application Root Directory
                'appDir'         => dirname(dirname(__DIR__)),
                // AOP Cache Directory
                'cacheDir'       => dirname(__DIR__).'/runtime/aspect',
                // Cache File Mode
                'cacheFileMode'  => 511,
                // Miscellaneous AOP Engine Features
                'features'       => 0,
                // Directories White List
                'includePaths'   => [
                    dirname(__DIR__),
                ],
                // Directories Black List
                'excludePaths'   => [
                    dirname(__DIR__).'/runtime',
                    dirname(__DIR__).'/tests',
                    dirname(__DIR__).'/views',
                ],
                // AOP Container
                'containerClass' => \Go\Core\GoAspectContainer::class,
            ],
            // Yours aspects
            'aspects' => [
                frontend\aspects\LoggingAspect::class,
            ],
        ],
    ]
];

yii2-app-basic

配置 config/web.php 文件中添加:


return [
    'bootstrap' => [
        'aop',
    ],
    'components' => [
        'aop' => [
            'class'   => 'Guanguans\YiiGoAop\GoAopComponent',
            'initOption'  => [
                // AOP Debug Mode
                'debug'          => false,
                // Application Root Directory
                'appDir'         => dirname(dirname(__DIR__)),
                // AOP Cache Directory
                'cacheDir'       => dirname(__DIR__).'/runtime/aspect',
                // Cache File Mode
                'cacheFileMode'  => 511,
                // Miscellaneous AOP Engine Features
                'features'       => 0,
                // Directories White List
                'includePaths'   => [
                    dirname(__DIR__).'/assets',
                    dirname(__DIR__).'/aspects',
                    dirname(__DIR__).'/commands',
                    dirname(__DIR__).'/controllers',
                    dirname(__DIR__).'/models',
                    dirname(__DIR__).'/widgets',
                ],
                // Directories Black List
                'excludePaths'   => [
                    dirname(__DIR__).'/config',
                    dirname(__DIR__).'/mail',
                    dirname(__DIR__).'/runtime',
                    dirname(__DIR__).'/tests',
                    dirname(__DIR__).'/vagrant',
                    dirname(__DIR__).'/vendor',
                    dirname(__DIR__).'/views',
                    dirname(__DIR__).'/web',
                ],
                // AOP Container
                'containerClass' => \Go\Core\GoAspectContainer::class,
            ],
            // Yours aspects
            'aspects' => [
                app\aspects\LoggingAspect::class,
            ],
        ],
    ]
];

使用示例

创建切面 public frontend\controllers\SiteController->*Index(*)

<?php

namespace frontend\aspects;

use Go\Aop\Aspect;
use Go\Aop\Intercept\MethodInvocation;
use Go\Lang\Annotation\Before;
use Go\Lang\Annotation\After;
use Yii;

class LoggingAspect implements Aspect
{
    /**
     * Method that will be called before real method
     * @param  MethodInvocation  $invocation  Invocation
     * @Before("execution(public frontend\controllers\SiteController->*Index(*))")
     */
    public function beforeMethodExecution(MethodInvocation $invocation)
    {
        file_put_contents(Yii::$app->getRuntimePath().'/logs/logging.log', 'this is a before method testing.'.PHP_EOL, FILE_APPEND);
    }

    /**
     * Method that will be called after real method
     * @param  MethodInvocation  $invocation  Invocation
     * @After("execution(public frontend\controllers\SiteController->*Index(*))")
     */
    public function afterMethodExecution(MethodInvocation $invocation)
    {
        file_put_contents(Yii::$app->getRuntimePath().'/logs/logging.log', 'this is a after method testing.'.PHP_EOL, FILE_APPEND);
    }
}

运行访问 http://localhost:8888/index.php?r=site/index

cat frontend/runtime/logs/logging.log

───────┬───────────────────────────────────────────────────────────────────
       │ File: frontend/runtime/logs/logging.log
───────┼───────────────────────────────────────────────────────────────────
   1   │ this is a before method testing.
   2   │ this is a after method testing.
───────┴───────────────────────────────────────────────────────────────────

相关链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值