laravelba / proxyvel
此包已被弃用且不再维护。未建议替代包。
laravel IoC 容器服务代理
dev-master
2015-02-19 14:47 UTC
Requires
- illuminate/container: ~4.2
- ocramius/proxy-manager: ~1.0
Requires (Dev)
- laravel/framework: ~4.2
- phpunit/phpunit: ~4.0
This package is not auto-updated.
Last update: 2022-02-01 12:44:46 UTC
README
ProxyManager的Laravel实现。
为什么?
在进行密集的构造函数注入时,为了创建应用程序,需要大量的对象。由于PHP为每个请求创建所有环境,这对PHP来说是一大负担,而且可能大多数对象图都不会被使用。
此包通过提供代理而不是真实对象来帮助您。
关于这一点的更详细解释,请阅读@Ocramius关于它的看法或访问ProxyManager文档
使用方法
Laravel的架构在IoC容器方面不够灵活:《Illuminate\Foundation\Application》对象扩展了《Illuminate\Container\Container》,这使得替换变得困难。
此包提供了两个扩展,即《Application》和《Container》。您可能不需要两者都使用。
此外,此包还提供了一种通过《Specification》模式来决定IoC容器将代理哪些对象的方法
// Proxy all the things! $specification = new Proxyvel\Specifications\ProxyEverything; // Proxy by the given namespaces $specification = new Proxyvel\Specifications\ProxyNamespace([ 'App\Services', 'App\Repositories' ]); // Proxy only the given cases / class names $specification = new Proxyvel\Specifications\ProxyCases([ // You can always proxy Laravel's services by using the key Laravel sets 'db', // Proxy some concrete classes 'App\Services\FooService', // And some interfaces 'App\Contracts\FooRepositoryInterface' ]); // Proxy by regular expression (powerful, but usually complex) $specification = new Proxyvel\Specifications\ProxyByRegExp([ '/Interface$/', '/^Abstract/' ]); // Combine multiple specifications $specification = new Proxyvel\Specifications\ProxyCollection([ new Proxyvel\Specifications\ProxyCases(/* ... */), new Proxyvel\Specifications\ProxyNamespace(/* ... */), ]); // Define by negation (for those of you who see the glass half-empty) $specification = new Proxyvel\Specifications\ProxyNegator( new Proxyvel\Specifications\ProxyNamespace('App\Entities') );
Laravel项目中的使用
这里没有《ServiceProvider》。您需要编辑您的《bootstrap/start.php》文件,并将《$app》变量替换为这个包的《Application》。
// in bootstrap/start.php $app = new Proxyvel\Application; // If you don't call this, the container will just defer to Laravel's default behavior $app->setProxyConfiguration( // Example Specification, build the one you need here new Proxyvel\Specifications\ProxyEverything, // Only instances of AbstractLazyFactory are accepted for now new ProxyManager\Factory\LazyLoadingValueHolderFactory );
在Laravel项目中,每个《Container》实例都是通过初始的《Application》解析的,所以您只需要这个。
Laravel项目之外的使用
如果您在Laravel项目之外使用Laravel的《Container》,则需要实例化此包的《Container》扩展。
// Wherever you first instantiate it $container = new Proxyvel\Container( // Example Specification, build the one you need here new Proxyvel\Specifications\ProxyEverything, // Only instances of AbstractLazyFactory are accepted for now new ProxyManager\Factory\LazyLoadingValueHolderFactory );
由于Liskov替换原则,此包的扩展可以在期望《Container》的任何地方使用。