Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] 依赖注入 #2560

Closed
guiqibusixin opened this issue Sep 21, 2020 · 5 comments · Fixed by #2565
Closed

[BUG] 依赖注入 #2560

guiqibusixin opened this issue Sep 21, 2020 · 5 comments · Fixed by #2565
Labels
bug Something isn't working

Comments

@guiqibusixin
Copy link

Linux d4433e5f5f1e 4.18.0-147.el8.x86_64 #1 SMP Wed Dec 4 21:51:45 UTC 2019 x86_64 Linux
PHP 7.4.5 (cli) (built: May 10 2020 04:05:50) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
hyperf/cache                         v2.0.9             A cache component for hyperf.
hyperf/command                       v2.0.10            Command for hyperf
hyperf/config                        v2.0.9             An independent component that provides confi...
hyperf/constants                     v2.0.9             A constants component for hyperf.
hyperf/contract                      v2.0.9             The contracts of Hyperf.
hyperf/database                      v2.0.10            A flexible database library.
hyperf/db-connection                 v2.0.10            A hyperf db connection handler for hyperf/da...
hyperf/devtool                       v2.0.10            A Devtool for Hyperf.
hyperf/di                            v2.0.10            A DI for Hyperf.
hyperf/dispatcher                    v2.0.10            A HTTP Server for Hyperf.
hyperf/event                         v2.0.10            an event manager that implements PSR-14.
hyperf/exception-handler             v2.0.10            Exception handler for hyperf
hyperf/filesystem                    v2.0.11            flysystem integration for hyperf
hyperf/framework                     v2.0.3             A coroutine framework that focuses on hypers...
hyperf/guzzle                        v2.0.3             Swoole coroutine handler for guzzle
hyperf/http-message                  v2.0.3             microservice framework base on swoole
hyperf/http-server                   v2.0.9             A HTTP Server for Hyperf.
hyperf/logger                        v2.0.11            A logger component for hyperf.
hyperf/memory                        v2.0.3             An independent component that use to operate...
hyperf/model-cache                   v2.0.10            A model cache component for hyperf.
hyperf/model-listener                v2.0.3             A model listener for Hyperf.
hyperf/paginator                     v2.0.3             A paginator component for hyperf.
hyperf/pool                          v2.0.3             An independent universal connection pool com...
hyperf/process                       v2.0.9             A process component for hyperf.
hyperf/redis                         v2.0.3             A redis component for hyperf.
hyperf/retry                         v2.0.11            A retry component for hyperf.
hyperf/server                        v2.0.9             A base server library for Hyperf.
hyperf/testing                       v2.0.3             Testing for hyperf
hyperf/translation                   v2.0.3             An independent translation component, forked...
hyperf/utils                         v2.0.0             A tools package that could help developer so...
hyperf/validation                    v2.0.9             hyperf validation

swoole

Swoole => enabled
Author => Swoole Team <team@swoole.com>
Version => 4.5.2
Built => Sep 11 2020 08:43:00
coroutine => enabled
epoll => enabled
eventfd => enabled
signalfd => enabled
spinlock => enabled
rwlock => enabled
openssl => OpenSSL 1.1.1g  21 Apr 2020
http2 => enabled
pcre => enabled
zlib => 1.2.11
mutex_timedlock => enabled
pthread_barrier => enabled
mysqlnd => enabled
async_redis => enabled

Directive => Local Value => Master Value
swoole.enable_coroutine => On => On
swoole.enable_library => On => On
swoole.enable_preemptive_scheduler => Off => Off
swoole.display_errors => On => On
swoole.use_shortname => Off => Off
swoole.unixsock_buffer_size => 8388608 => 8388608

Description:

依赖注入对象失败。

可能原因:注入类的成员函数中使用了匿名类继承了另外一个类。导致在抽象语法树AST解析的时候误认为此类有父类,导致注入失败。错误信息大概的意思是这个类没有父类,不能使用parent关键字。

抛出的异常:

[ERROR] Cannot use "parent" when current class scope has no parent[20] in /www/runtime/container/proxy/XXX.proxy.php
[ERROR] #0 /www/vendor/hyperf/di/src/Resolver/ObjectResolver.php(99): App\Wx\Service\XXX->__construct()
#1 /www/vendor/hyperf/di/src/Resolver/ObjectResolver.php(66): Hyperf\Di\Resolver\ObjectResolver->createInstance()
#2 /www/vendor/hyperf/di/src/Resolver/ResolverDispatcher.php(62): Hyperf\Di\Resolver\ObjectResolver->resolve()
#3 /www/vendor/hyperf/di/src/Resolver/DepthGuard.php(75): Hyperf\Di\Resolver\ResolverDispatcher->Hyperf\Di\Resolver\{closure}()
#4 /www/vendor/hyperf/di/src/Resolver/ResolverDispatcher.php(63): Hyperf\Di\Resolver\DepthGuard->call()
#5 /www/vendor/hyperf/di/src/Container.php(182): Hyperf\Di\Resolver\ResolverDispatcher->resolve()
#6 /www/vendor/hyperf/di/src/Container.php(82): Hyperf\Di\Container->resolveDefinition()
#7 /www/vendor/hyperf/di/src/Container.php(117): Hyperf\Di\Container->make()
#8 /www/vendor/hyperf/di/src/Aop/RegisterInjectPropertyHandler.php(43): Hyperf\Di\Container->get()
#9 /www/vendor/hyperf/utils/src/Functions.php(269): Hyperf\Di\Aop\RegisterInjectPropertyHandler::Hyperf\Di\Aop\{closure}()
#10 /www/vendor/hyperf/di/src/Aop/PropertyHandlerTrait.php(65): call()

Steps To Reproduce:

  • 创建一个类,类中成员函数使用了匿名类继承了另外一个类
class Service{
    public function payNotify(RequestInterface $request, ResponseInterface $response){
        $payNotify = new class extends \Yurun\PaySDK\Weixin\Notify\Pay
        {
            /**
             * 后续执行操作
             * @return void
             */
            protected function __exec()
            {
                //var_dump($this->swooleRequest->getQueryParams());
                //var_dump($this->swooleRequest->post);
            }
        };
        //..........................................
       //..........................................
    }
}
  • 在其他类中注入Service类时发生了错误(错误信息上面有)
namespace App\XXX\Controller;


use Hyperf\Di\Annotation\Inject;
class XXX extends AbstractController
{
    /**
     * @Inject
     * @var Service
     */
    private $service;
//...................................................
}
  • Runtime中的Service代理类(AST生成),发现这个类没有父类,但是使用parent关键字
class VipService
{
    use \Hyperf\Di\Aop\ProxyTrait;
    use \Hyperf\Di\Aop\PropertyHandlerTrait;
    function __construct()
    {
        if (method_exists(parent::class, '__construct')) {
            parent::__construct(...func_get_args());
        }
        self::__handlePropertyHandler(__CLASS__);
    }
//.........................................
//.........................................
//.........................................
}
@guiqibusixin guiqibusixin added the bug Something isn't working label Sep 21, 2020
@guiqibusixin guiqibusixin changed the title [BUG] 依赖注入[BUG] Sep 21, 2020
@guiqibusixin
Copy link
Author

guiqibusixin commented Sep 21, 2020

  • 解决办法1:
    既然是没有父类导致的,那么继承一个父类就好了,比如BaseServcie类
  • 导致错误的其它方式:
    (1)在一个文件中定义多个类,并且其中含有一个类有父类
  • 根本原因:
    抽象语法树AST没有正确的判断这个类是否有父类

@guiqibusixin guiqibusixin changed the title 依赖注入[BUG] [BUG] 依赖注入 Sep 21, 2020
@limingxinleo
Copy link
Member

这个 VipService 就是内部有 匿名类 的 Service ???

@guiqibusixin
Copy link
Author

guiqibusixin commented Sep 22, 2020

是的哈,vipService就是前面所说的Service。这里忘记改名字了

@limingxinleo
Copy link
Member

image

按照 PR 里的修改,试试看

@guiqibusixin
Copy link
Author

好。晚点试下

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants