如何设置Go! AOP PHP与Yii 1.1.13一起使面向方面编程工作?

时间:2013-09-25 18:43:48

标签: php yii integration aop goaop

我正在尝试设置Go! AOP Php与我的Yii Framework应用程序。

我做了以下事情:

1-Installed Go! AOP Php与作曲家通过在composer.json中添加以下行。

    "lisachenko/go-aop-php":"0.4.*"

2 - 添加了像这样的应用程序内核

<?php
// app/ApplicationAspectKernel.php

require_once 'TestMonitorAspect.php';

use Aspect\TestMonitorAspect;
use Go\Core\AspectKernel;
use Go\Core\AspectContainer;

/**
 * Application Aspect Kernel
 */
class ApplicationAspectKernel extends AspectKernel
{

    /**
     * Configure an AspectContainer with advisors, aspects and pointcuts
     *
     * @param AspectContainer $container
     *
     * @return void
     */
    protected function configureAop(AspectContainer $container)
    {
        $container->registerAspect(new TestMonitorAspect());
    }
}

3 - 还添加了TestMonitorAspect。

<?php
// Aspect/MonitorAspect.php

namespace Aspect;

require_once realpath(__DIR__.'/../../vendor/lisachenko/go-aop-php/src/Go/Aop/Aspect.php');

use Go\Aop\Aspect;
use Go\Aop\Intercept\FieldAccess;
use Go\Aop\Intercept\MethodInvocation;
use Go\Lang\Annotation\After;
use Go\Lang\Annotation\Before;
use Go\Lang\Annotation\Around;
use Go\Lang\Annotation\Pointcut;

/**
 * Monitor aspect
 */
class TestMonitorAspect implements Aspect
{

    /**
     * Method that will be called before real method
     *
     * @param MethodInvocation $invocation Invocation
     * @Before("within(**)")
     */
    public function beforeMethodExecution(MethodInvocation $invocation)
    {
        \Yii::trace(__CLASS__.'AOP Hello World','system.*');
    }
}
?>

由于他的yii-aspect github项目集成示例适用于Yii 2.0,我使用了我自己的ApplicationAspectKernel的index.php初始化版本。

我做的是在Yii应用程序的onBeginRequest上初始化ApplicationAspectKernel,然后运行应用程序,如下所示:

$app = Yii::createWebApplication($env->configWeb);
$app->onBeginRequest = function($event) {
            include __DIR__ . '/protected/vendor/autoload.php'; // use composer

            include __DIR__ . '/protected/vendor/lisachenko/go-aop-php/src/Go/Core/AspectKernel.php';
            include __DIR__ . '/protected/extensions/go-aop-php/ApplicationAspectKernel.php';

// Initialize an application aspect container
            $applicationAspectKernel = ApplicationAspectKernel::getInstance();
            $applicationAspectKernel->init(array(
                'debug' => true, // use 'false' for production mode
// Cache directory
                'cacheDir' => null,
                // Include paths restricts the directories where aspects should be applied, or empty for all source files
                'includePaths' => array()
            ));
        };
$app->run();

我们无法使用任何方面。

有没有人对如何解决这个问题有任何想法?任何帮助或指导都非常感谢。谢谢!

我们咨询过的资源是:

  1. http://go.aopphp.com/docs/
  2. https://github.com/lisachenko/go-aop-php
  3. https://github.com/lisachenko/yii-aspect
  4. http://net.tutsplus.com/tutorials/php/aspect-oriented-programming-in-php-with-go/

1 个答案:

答案 0 :(得分:1)

我重新创建了我的https://github.com/lisachenko/yii-aspect存储库,使用composer来安装Yii和Go! AOP框架在一起。请检查一下。

UPD1 :packagist上还有yii-aspect项目的单行安装程序:

composer create-project lisachenko/yii-aspect --stability=dev

UPD2 :有关配置的详细手册也可在我的文章Aspect-Oriented Programming With Yii中找到