GO麻烦!面向方面的框架 [英] Troubles with GO! Aspect-Oriented Framework
问题描述
如果有人使用GO!框架,您能帮我吗. 我在php 5.3.13上安装框架.演示示例正在工作. 但是我自己的例子不起作用.没有执行Aspect(MethodExecution之前的方法).
If someone work with GO! framework, can you help me. I install framework on php 5.3.13. Demo example is working. But my own example doesn't work. Aspect(method beforeMethodExecution) is not perfomed.
这是我的代码.
主文件:
//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;
}
}
推荐答案
您知道,go-aop不是PHP扩展,因此它无法转换直接通过require
或include
加载的类. .在内部,它试图即时覆盖源代码,但是它应该收到一个控件(通过与composer或自定义autoloader类集成).
As you know, go-aop isn't a PHP extension, so it couldn't transform classes that were loaded directly via require
or include
. Internally it tries to overwrite the source code on-the-fly, but it should receive a control (via integration with composer or custom autoloader class).
因此,您在这里遇到错误:
So, you have an error here:
//6 Include test class
include(__DIR__.'/class/class1.php');
您已将此类显式加载到内存中,并且无法从用户空间对其进行转换.要将控件传递给框架,您应该明确地进行此操作.看这行 AopComposerLoader .php#L99 了解其工作原理.在这里,我们通过流源过滤器包含了源文件,该文件将控制权传递给了框架,它可以转换类以编织方面.
You explicitly load this class into memory and there is no way to transform it from userland. To pass a control to the framework, you should make this explicitly. Look at the line AopComposerLoader.php#L99 to have an idea how it works. Here we include a source file via the stream source filter that pass control to the framework and it can transform the class to weave an aspects.
要修正您的示例,只需将include
更改为以下内容:
To fix your example just change an include
to the following:
include (FilterInjectorTransformer::rewrite(__DIR__.'/class/class1.php'));
这篇关于GO麻烦!面向方面的框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!