GoAOP框架,can';不要让简单的方面工作

GoAOP框架,can';不要让简单的方面工作,goaop,Goaop,我试着用图书馆玩了一段时间,但从未成功地让它工作过。我已经阅读了好几次,并复制了一些示例,但都没能让它们发挥作用。我现在想要达到的只是一个简单的方面 我有以下几个文件: app/ApplicationAspectKernel.php <?php require './aspect/MonitorAspect.php'; use Go\Core\AspectKernel; use Go\Core\AspectContainer; /** * Application Aspect Ker

我试着用图书馆玩了一段时间,但从未成功地让它工作过。我已经阅读了好几次,并复制了一些示例,但都没能让它们发挥作用。我现在想要达到的只是一个简单的方面

我有以下几个文件:

app/ApplicationAspectKernel.php

<?php
require './aspect/MonitorAspect.php';

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 Aspect\MonitorAspect());
    }
}

GoAOP框架设计用于autoloader,这意味着它只能处理通过composer autoloader间接加载的类

当您通过
require_once./app/Example.php'手动包含类时类被PHP立即加载,并且不能被AOP转换,所以什么都不会发生,因为类已经存在于PHP的内存中

为了使AOP工作,您应该将类加载委托给
编写器
,并为您的类使用PSR-0/PSR-4标准。在这种情况下,AOP将钩住自动加载过程,并在需要时执行转换


有关框架内部的更多详细信息,请参阅我的答案。这些信息对您应该很有用。

自动加载类而不是要求它们解决了问题,谢谢。在您的回答中,您说库将仅与autoloader一起工作,然后具体地说是composer autoloader。它是否必须来自composer,或者如果我要创建自己的autoloader类来注册这些类,那么仍然可以工作?默认情况下,只支持composer,但是如果您有自己的逻辑,您可以轻松地采用AopComposerLoader中的代码。主线是找到文件名,然后用
FilterInjectorTransformer::rewrite($file)将其包装起来
<?php
require './vendor/autoload.php';
require_once './ApplicationAspectKernel.php';

// Initialize an application aspect container
$applicationAspectKernel = ApplicationAspectKernel::getInstance();
$applicationAspectKernel->init(array(
        'debug' => true, // Use 'false' for production mode
        // Cache directory
        'cacheDir' => __DIR__ . '/cache/', // Adjust this path if needed
        // Include paths restricts the directories where aspects should be applied, or empty for all source files
        'includePaths' => array(__DIR__ . '/app/')
));

require_once './app/Example.php';


$e = new Example();
$e->test1();
$e->test2('parameter');
<?php
namespace Aspect;

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 MonitorAspect implements Aspect
{

    /**
     * Method that will be called before real method
     *
     * @param MethodInvocation $invocation Invocation
     * @Before("execution(public Example->*(*))")
     */
    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()),
        "<br>\n";
    }
}
<?php


class Example {
    public function test1() {
        print 'test1' . PHP_EOL;
    }

    public function test2($param) {
        print $param . PHP_EOL;
    }
}