hyperf / lazy-loader-incubator
Fund package maintenance!
Open Collective
hyperf.wiki/#/zh-cn/donate
Requires
- php: >=8.1
- hyperf/code-parser: ^3.1
- hyperf/config: ^3.1
- hyperf/di: ^3.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- mockery/mockery: ^1.0
- phpstan/phpstan: ^1.0
- phpunit/phpunit: ^10.0
- swoole/ide-helper: dev-master
Suggests
- swow/swow: Required to create swow components.
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()); } }