GO的麻烦!面向方面的框架

时间:2014-06-17 18:37:02

标签: php aop goaop

如果有人与GO一起工作!框架,你能帮助我吗? 我在php 5.3.13上安装了框架。演示示例正在运行。 但我自己的例子并不奏效。没有执行Aspect(方法beforeMethodExecution)。

这是我的代码。

主档案:

//1 Include kernel and all classes
if (file_exists(__DIR__ .'/../../vendor/autoload.php')) {
     $loader = include __DIR__ .'/../../vendor/autoload.php';
}
// 2 Make own ascpect kernel

use Go\Core\AspectKernel;
use Go\Core\AspectContainer;

class Kernel extends AspectKernel{
  /**
   * Configure an AspectContainer with advisors, aspects and pointcuts
   *
   * @param AspectContainer $container
   *
   * @return void
   */
   public function configureAop(AspectContainer $container)
  {
  }
}

//3 Initiate aspect kernel

$Kernel = Kernel::getInstance();

$Kernel->init();

//4 Include aspect
include(__DIR__.'/aspectclass/AspectClass.php');

$aspect = new DebugAspect();

//5 register aspect
$Kernel->getContainer()->registerAspect($aspect);


//6 Include test class

include(__DIR__.'/class/class1.php'); 


//7 Execute test class

$Class = new General('test');
$Class->publicHello();

带测试类的文件:

class General{
protected $message = '';

public function __construct($message)
{
    $this->message = $message;
}

public function publicHello()
{
    echo 'Hello, you have a public message: ', $this->message, "<br>", PHP_EOL;
}

}

带方面的文件:

use Go\Aop\Aspect;
use Go\Aop\Intercept\FieldAccess;
use Go\Aop\Intercept\FunctionInvocation;
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;
use Go\Lang\Annotation\DeclareParents;
use Go\Lang\Annotation\DeclareError;

class DebugAspect implements Aspect{

/**
 * Method that should be called before real method
 *
 * @param MethodInvocation $invocation Invocation
 * @Before("execution(General->*(*))")
 *
 */
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()),
    PHP_EOL;
}


}

1 个答案:

答案 0 :(得分:3)

如您所知,go-aop不是PHP扩展,因此它无法转换直接通过requireinclude加载的类。在内部,它试图动态覆盖源代码,但它应该接收一个控件(通过与composer或自定义自动加载器类集成)。

所以,你在这里有一个错误:

//6 Include test class
include(__DIR__.'/class/class1.php');

您明确将此类加载到内存中,并且无法从userland转换它。要将控件传递给框架,您应该明确地进行此操作。查看AopComposerLoader.php#L99行以了解它是如何工作的。这里我们通过stream source filter包含一个源文件,它将控制传递给框架,它可以将类转换为编织方面。

要修复您的示例,只需将include更改为以下内容:

include (FilterInjectorTransformer::rewrite(__DIR__.'/class/class1.php'));