我正在尝试使用 GO AOP 来允许我在我正在处理的 PHP 项目中使用方面。我使用 composer 来安装 goaop/framework。我已经为我的方面编写了如下文件。我知道 CalendarController 运行是因为其中一个函数有一个我可以看到的回声。你知道为什么这不起作用吗?
索引.php
// Initialize an application aspect container
$applicationAspectKernel = App\Http\applicationAspectKernel::getInstance();
$applicationAspectKernel->init(array(
'debug' => true, // use 'false' for production mode
// Cache directory
'cacheDir' => __DIR__ . '/path/to/cache/for/aop',
// Include paths restricts the directories where aspects should be applied, or empty for all source files
'includePaths' => array(
__DIR__ . '/../app'
)
));
MonitorAspect.php
namespace Aspects;
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;
use Psr\Log\LoggerInterface;
class MonitorAspect implements Aspect
{
/**
* Method that will be called before real method
*
* @param MethodInvocation $invocation Invocation
* @Before("execution(public App\Http\Controllers\CalendarController->*(*))")
*/
public function beforeMethodExecution(MethodInvocation $invocation)
{
\Monolog\Handler\error_log("Is this being called at all? Yes");
$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()),
"<br>\n";
}
}
applicationAspectKernel.php 命名空间 App\Http;
use Go\Core\AspectKernel;
use Go\Core\AspectContainer;
use Aspects\MonitorAspect;
/**
* Description of aspectKernel
*
*
*/
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 MonitorAspect());
}
}
编辑:修复了@Before 语句。