Pro*_*tas 6 php inversion-of-control laravel laravel-5
在一个全新的Laravel构建中,我无法覆盖IoC绑定以在应用程序的任何位置工作.
假设一个覆盖核心类的服务提供者,例如缓存:
class NewServiceProvider extends ServiceProvider
{
protected $defer = true;
public function register()
{
$this->app->singleton('cache', function($app) {
return new \stdClass; // demo purpose
});
}
public function provides()
{
return ['cache'];
}
}
Run Code Online (Sandbox Code Playgroud)
然后在app.providers
config 的底部添加提供程序.
现在修改routes.php
为以下内容并检查结果:
Route::get('/', function () {
dd(app('cache'));
});
// Results in an empty stdClass being shown. It works!
Run Code Online (Sandbox Code Playgroud)
然而,启动artisan tinker
并执行相同的操作:
$ php artisan tinker
>>> app('cache')
=> Illuminate\Cache\CacheManager
Run Code Online (Sandbox Code Playgroud)
突然间覆盖不再起作用了......
处理事件侦听器时遇到相同的行为......
这是正常的行为吗?我忽略了什么吗?或者这是某种错误?
我自己设法找到了这个问题。
看来 Artisan 使用按键排序的提供程序数组一次性加载所有延迟的提供程序:
...
"cache" => NewServiceProvider,
"cache.store" => CacheServiceProvider,
...
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,cache.store
绑定随后调用内置函数CacheServiceProvider
,因此使我们的cache
绑定变得无用,因为它包含我们需要覆盖的绑定)。
所以我必须NewServiceProvider
扩展CacheServiceProvider
并调用parent::register()
将提供者数组转换为:
...
"cache" => NewServiceProvider,
"cache.store" => NewServiceProvider,
...
Run Code Online (Sandbox Code Playgroud)
这似乎是在 Artisan 中正确解析覆盖的缓存绑定的唯一方法。