Coder Social home page Coder Social logo

guanguans / yii-goaop Goto Github PK

View Code? Open in Web Editor NEW
6.0 2.0 0.0 29 KB

Bringing the goaop/framework to Yii. - 将 goaop/framework 集成到 Yii。在 Yii 中优雅的面向切面编程。

License: MIT License

PHP 100.00%
aop goaop aop-aspects aspects yii yii2 yii2-extension

yii-goaop's Introduction

yii-goaop

CI codecov StyleCI Latest Stable Version Total Downloads License

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

Requirement

  • Yii >= 2.0

Installation

$ composer require guanguans/yii-goaop --prefer-dist -vvv

composer.json add:

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

Configuration

yii2-app-advanced

Config config/main.php file add:

<?php

return [
    'bootstrap' => [
        'aop',
    ],
    'components' => [
        'aop' => [
            'class'   => 'Guanguans\YiiGoAop\GoAopComponent',
            'initOptions'  => [
                // 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 config/web.php file add:

return [
    'bootstrap' => [
        'aop',
    ],
    'components' => [
        'aop' => [
            'class'   => 'Guanguans\YiiGoAop\GoAopComponent',
            'initOptions'  => [
                // 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,
            ],
        ],
    ]
];

Usage

Create a aspect for 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);
    }
}

Run frontend\controllers\SiteController\actionIndex action

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.
───────┴───────────────────────────────────────────────────────────────────

Testing

$ composer test

Related Links

License

MIT

yii-goaop's People

Contributors

guanguans avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

yii-goaop's Issues

安装后运行框架报错

Exception 'yii\base\UnknownPropertyException' with message 'Setting unknown property: Guanguans\YiiGoAop\GoAopComponent::initOption'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.