php - Laravel 4 如何加载延迟提供者?

标签 php laravel-4

我需要深入了解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列出 eagerdeferred类。这样我们就不会为每个请求加载所有类,因为我们可能永远不需要 Form类,如果我们正在构建一个 API。
ProviderRepository类扫描 app/config/app.php 中列出的所有提供程序并为它们中的每一个实例化提供程序(这些不是实际的库)。

这些提供商例如 CacheServiceProviderIlluminate/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()这里称为是我所收集的;每个FacadeValidator立面延伸基地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/

相关文章:

php - PHP显式变量在闭包中的优势

javascript - 如何将输入的 id 值存储到另一个页面上的变量?

php - 什么时候在 PHP 中使用静态方法/字段?

php - 免费的 RTE [富文本编辑器] 帮助需要集成它

laravel - 实际目录覆盖 Laravel 路由

mysql - 如何防止JSON字符串中的\r\n等

Laravel:什么是模型?

php - Elgg白屏: undefined function Elgg\\mysql_connect()

rest - 在 REST API 上管理多对多关系的标准是什么?

php - Laravel 不发送电子邮件也不给出错误