hyperf / hyperf

🚀 A coroutine framework that focuses on hyperspeed and flexibility. Building microservice or middleware with ease.

Home Page:https://www.hyperf.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[FEATURE] 代理类生成优化

tw2066 opened this issue · comments

现象
目前生成的代理类和源文件的代码行数不一致,当出现报错的时候(特别是线上), 很难通过行数来找到具体的报错

例子
原始类

<?php

namespace App\Service;

use Hyperf\Macroable\Macroable;

class Goods2
{
    use Macroable;
    public function t1()
    {
        return 'goods2-t1';
    }

}

生成代理类

<?php

namespace App\Service;

use Hyperf\Macroable\Macroable;
class Goods2 extends GoodsInfoService
{
    use \Hyperf\Di\Aop\ProxyTrait;
    use \Hyperf\Di\Aop\PropertyHandlerTrait;
    function __construct()
    {
        if (method_exists(parent::class, '__construct')) {
            parent::__construct(...func_get_args());
        }
        $this->__handlePropertyHandler(__CLASS__);
    }
    use Macroable;
    public function t1()
    {
        return 'goods2-t1';
    }
}

建议
是否考虑保证生成的代理类尽可能保证原始代码行数保持一致

<?php

namespace App\Service;

use Hyperf\Macroable\Macroable;
class Goods2 extends GoodsInfoService
{   use \Hyperf\Di\Aop\ProxyTrait;use \Hyperf\Di\Aop\PropertyHandlerTrait;function __construct(){if (method_exists(parent::class, '__construct')) { parent::__construct(...func_get_args());}$this->__handlePropertyHandler(__CLASS__);}
    use Macroable;
    
    public function t1()
    {
        return 'goods2-t1';
    }
    
}
commented

如果这个函数存在 Aspect 的话,那要怎么保证行号一致呢?/狗头

@BadJacky

<?php

namespace App\Service;

use Hyperf\Macroable\Macroable;
class Goods2 extends GoodsInfoService
{   use \Hyperf\Di\Aop\ProxyTrait;use \Hyperf\Di\Aop\PropertyHandlerTrait;function __construct(){if (method_exists(parent::class, '__construct')) { parent::__construct(...func_get_args());}$this->__handlePropertyHandler(__CLASS__);}
    use Macroable;

    public function t1()
    {$__function__ = __FUNCTION__;$__method__ = __METHOD__;return self::__proxyCall(__CLASS__, __FUNCTION__, ['keys' => []], function () use($__function__, $__method__) {
        return 'goods2-t1';
    });}

}

Sentry +1 真香

为什么不能把/runtime/proxy/直接在每次版本发布时候编译出来同步更新到仓库?排查时候也有源文件给你看。

而且你也可以使用其他方式获取异常信息。

这个优化没什么必要性。

@BadJacky

<?php

namespace App\Service;

use Hyperf\Macroable\Macroable;
class Goods2 extends GoodsInfoService
{   use \Hyperf\Di\Aop\ProxyTrait;use \Hyperf\Di\Aop\PropertyHandlerTrait;function __construct(){if (method_exists(parent::class, '__construct')) { parent::__construct(...func_get_args());}$this->__handlePropertyHandler(__CLASS__);}
    use Macroable;

    public function t1()
    {$__function__ = __FUNCTION__;$__method__ = __METHOD__;return self::__proxyCall(__CLASS__, __FUNCTION__, ['keys' => []], function () use($__function__, $__method__) {
        return 'goods2-t1';
    });}

}

我设想过你会引入 Sentry ,记录日志等各种操作,唯独没想到你会给出这个方案。

如果是为了实现而实现,那你确实厉害。