2014-06-17 47 views
1

如果有人使用GO!框架,你能幫助我嗎? 我在php 5.3.13上安裝框架。演示示例正在工作。 但我自己的例子不起作用。方面(MethodMethodExecution之前的方法)未執行。GO出現問題!面向方面的框架

這是我的代碼。

主文件:

//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; 
} 


} 

回答

2

如你所知,去的AOP ISN這是一個PHP擴展,所以它可以不轉換通過requireinclude直接加載的類。它在內部嘗試覆蓋源代碼,但它應該接收控件(通過與composer或自定義autoloader類的集成)。

所以,你這裏有一個錯誤:

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

您明確這個類加載到內存中,並沒有辦法從用戶空間改造它。要將控件傳遞給框架,應該明確地做出這一點。看看行AopComposerLoader.php#L99有一個想法是如何工作的。在這裏,我們通過stream source filter包含一個源文件,它將控制傳遞給框架,它可以轉換類來編織一個方面。

要解決你的例子只是改變include以下幾點:

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