我需要深入了解laravel。自从我们开始使用 laravel 以来,我必须向我的开发团队解释一切。
如果有错误请更正:当 laravel 启动时,由于性能提高,它将服务提供者拆分为 'eager' 和 'deferred',然后注册所有 'eager' 提供者,但不注册 'deferred'。
我的问题:
每次我们使用延迟服务时,例如:
$validator = Validator::make(
//..
);
laravel 如何加载和注册该类/服务?我只是在
Illuminate\Foundation\ProviderRepository
上找到这可能与第 70 行相关。类(class)$app->setDeferredServices($manifest['deferred']);
但后来我卡住了。抱歉英语不好,希望大家理解。谢谢。
最佳答案
我也想知道这个问题的答案,但我决定自己去找。
如果你去app/storage/meta
有一个services.json
列出 eager
和 deferred
类。这样我们就不会为每个请求加载所有类,因为我们可能永远不需要 Form
类,如果我们正在构建一个 API。ProviderRepository
类扫描 app/config/app.php
中列出的所有提供程序并为它们中的每一个实例化提供程序(这些不是实际的库)。
这些提供商例如 CacheServiceProvider
在 Illuminate/Cache
有一个名为 defer
的变量这决定了库是否会被急切加载或延迟到需要时。
这是来自 ProviderRepository
的引述
When recompiling the service manifest, we will spin through each of the providers and check if it's a deferred provider or not. If so we'll add it's provided services to the manifest and note the provider.
对于
CacheServiceProvider
这设置为 true
.所以让我们说
Validator::make()
这里称为是我所收集的;每个Facade
如Validator
立面延伸基地Facade
类(class) Illuminate\Support\Facades\Facade
.这有一个神奇的方法
__callStatic($method, $args)
这很不言自明,这种方法的要点是$instance = static::resolveFacadeInstance(static::getFacadeAccessor());
在这种情况下,外观访问器返回执行验证的实际类的字符串 (
Illuminate\Validation\Validator
):protected static function getFacadeAccessor() { return 'validator'; }
主要方法
resolveFacadeInstance
检查返回的访问器是否为对象。如果是,它只是简单地返回它,因为它可能是一个自定义的 Laravel 包。这个方法的第二部分检查它是否已经被解析,如果它已经返回。最后一部分:
return static::$resolvedInstance[$name] = static::$app[$name];
调用
Illuminate\Foundation\Application
(扩展了 Illuminate\Container\Container
类)通过 array access接口(interface)位于前面提到的父类中。所以
static::$app[$name]
调用:public function offsetGet($key)
{
return $this->make($key);
}
这位于
Container
类(class)这将我们引向
make()
Application
中的方法类(class):public function make($abstract, $parameters = array())
{
if (isset($this->deferredServices[$abstract]))
{
$this->loadDeferredProvider($abstract);
}
return parent::make($abstract, $parameters);
}
我会让你深入研究
parent::make()
方法,因为这将开始变得非常冗长,但就目前而言,这是它加载延迟提供程序并通过 loadDeferredProvider()
实例化它的地方public function loadDeferredProviders()
{
// We will simply spin through each of the deferred providers and register each
// one and boot them if the application has booted. This should make each of
// the remaining services available to this application for immediate use.
foreach (array_unique($this->deferredServices) as $provider)
{
$this->register($instance = new $provider($this));
if ($this->booted) $instance->boot();
}
$this->deferredServices = array();
}
代码块中的注释应该解释其余发生的事情。
从这里开始,
__callStatic($method, $args)
的最后一部分触发名为 Validator::make()
的实际方法.public static function __callStatic($method, $args)
{
$instance = static::resolveFacadeInstance(static::getFacadeAccessor());
switch (count($args))
{
case 0:
return $instance->$method();
case 1:
return $instance->$method($args[0]);
case 2:
return $instance->$method($args[0], $args[1]);
case 3:
return $instance->$method($args[0], $args[1], $args[2]);
case 4:
return $instance->$method($args[0], $args[1], $args[2], $args[3]);
default:
return call_user_func_array(array($instance, $method), $args);
}
}
所以门面调用
Illuminate\Validation\Validator
的非静态方法validate()
.我很确定这对于 4.0 是准确的,但如果有任何错误,请纠正我。
关于php - Laravel 4 如何加载延迟提供者?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19866067/