hyperf/lazy-loader-incubator

v0.1.0 2024-03-06 07:21 UTC

This package is auto-updated.

Last update: 2024-11-06 09:17:31 UTC


README

中文 | English

Hyperf di 延迟加载

composer require hyperf/lazy-loader-incubator

发布配置文件

php bin/hyperf vendor:publish hyperf/lazy-loader-incubator

使用

// app/Service/UserInterface
interface UserInterface {
    public function work();
}
// app/Service/UserService
class UserService implements UserInterface{
    public function work() 
    {
        return 'working...';
    }
}
// app/Controller/IndexController
namespace App\Controller;

use App\Service\UserInterface;
use Hyperf\Di\Annotation\Inject;

class IndexController {
    #[Inject(lazy: true)]
    private UserInterface $userService;
    
    public function index() {
        return $this->userService->work();
    }
}

生成延迟代理类为

// runtime/container/proxy/Hyperf_Lazy_UserService_xxx.php
namespace HyperfLazy\UserService;

/**
 * Be careful: This is a lazy proxy, not the real HyperfTest\Stub\FooService from container.
 *
 * {@inheritdoc}
 */
class Foo extends \App\Service\UserService
{
    use \Hyperf\Di\LazyLoader\LazyProxyTrait;
    const PROXY_TARGET = 'HyperfTest\\Stub\\FooService';
    public function work()
    {
        return $this->__call(__FUNCTION__, func_get_args());
    }
}