Laravel 4:立面如何解决? [英] Laravel 4 : How are Facades resolved?

查看:56
本文介绍了Laravel 4:立面如何解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究引擎盖下Laravel 4 facades 的情况.

I've kind of looking at what's going on Laravel 4 facades under the hood.

让我们以这个立面为例:

Let's take this Facade as an example:

File::get(someArgs);

如果我没记错的话,一步一步(过于简化)的调用将是:

If i'm not mistaken, the step by step (oversimplified) invocation would be:

//static method invocation which are all extended from Facade class
File::__callStatic(get, someArgs)
//returns an instance of FileSystem
File::resolveFacedeInstance('files') 
FileSystem->get(someArgs)

我很困惑的是下面方法 File :: resolveFacadeInstance()下方的注释行:

What I'am confused about is in the commented line below of the method File::resolveFacadeInstance() below:

protected static function resolveFacadeInstance($name)
{
    if (is_object($name)) return $name;

    if (isset(static::$resolvedInstance[$name]))
    {
        return static::$resolvedInstance[$name];
    }



    /**
    * The line that i'm confused about
    */

    return static::$resolvedInstance[$name] = static::$app[$name];
}

我的问题是:

  • File :: $ app 是如何在Facade类中初始化或分配值的?
  • 如果 File :: get()是调用的Facade

  • How is File::$app even initialized or assigned a value inside the Facade class
  • If File::get() is the invoked Facade

静态:: $ app [$ name] 将解析为我认为Application ['files'] 或Application->文件,它们又调用Application-> __ get('files'),因为 Application 类中没有 files 属性.

static::$app[$name] would resolve to i think Application['files'] or Application->files which in turn calls Application->__get('files') since there's no files property inside Application class.

如果仅此方法的内容,将如何返回FileSystem类?

How would FileSystem Class be return if this is only the content of this method?

public function __get($key)
{
    return $this[$key];
}

推荐答案

我将尝试简单地描述一下:

I'll try to describe in short :

因此,您已经知道通过Facade类的__callStatic方法调用了resolveFacadeInstance方法,并且组件的Facade (i.e. File extends Facade)扩展了该Facade类.

So, you already know that resolveFacadeInstance method is called via __callStatic method of Facade class and component's Facade (i.e. File extends Facade) extends this Facade class.

在框架的启动过程中,从public/index.php行开始,开始执行bootstrap/start.php文件

During the boot-up process of the framework, from public/index.php following line starts the execution of bootstrap/start.php file

$app = require_once __DIR__.'/../bootstrap/start.php';

因此,在此(c6)文件中,您可以看到类似的代码

So, in this (bootstrap/start.php) file you can see some code like

// the first line, initiate the application
$app = new Illuminate\Foundation\Application;
// ...
// ...
// notice this line
require $framework.'/Illuminate/Foundation/start.php';
// ...
// last line
return $app;

在此代码段中,require $framework.'/Illuminate/Foundation/start.php';行开始执行Foundation/start.php文件,在此文件中,您可能会看到类似的内容

In this code snippet, require $framework.'/Illuminate/Foundation/start.php'; line starts the execution of Foundation/start.php file and in this file you may see something like this

// ...

Facade::clearResolvedInstances();
// Notice this line
Facade::setFacadeApplication($app);

此行(如上所述)将application实例的application实例设置为Facade类中的$app属性

This (given above) line sets application instanse to $app property in the Facade class

// support/Facades/Facade.php
public static function setFacadeApplication($app)
{
    static::$app = $app;
}

然后在底部的Foundation/start.php文件中,您会看到类似的内容

Then in the Foundation/start.php file at the bottom, you can see something like this

/*
|--------------------------------------------------------------------------
| Register The Core Service Providers
|--------------------------------------------------------------------------
|
| The Illuminate core service providers register all of the core pieces
| of the Illuminate framework including session, caching, encryption
| and more. It's simply a convenient wrapper for the registration.
|
*/

$providers = $config['providers'];

$app->getProviderRepository()->load($app, $providers);

$app->boot();

在上面给出的此代码段中,框架注册的所有核心组件以及您所知道的每个组件都有一个服务提供者类(即FilesystemServiceProvider),并且在每个服务提供者类中都有一个方法register (对于FilesystemServiceProvider)

In this code snippet given above, all the core components registered by the framework and as you know that, every component has a service provider class (i.e. FilesystemServiceProvider) and in every service provider class there is a method register which is (for FilesystemServiceProvider)

/**
 * Register the service provider.
 *
 * @return void
 */
public function register()
{
    $this->app['files'] = $this->app->share(function() { return new Filesystem; });
}

好吧,在这种情况下,$this->app['files']设置(return new Filesystem)一个匿名函数,该函数在执行时返回filesystem

Well, in this case $this->app['files'] setting (return new Filesystem) an anonymous function, which returns the filesystem when gets executed

$this->app['files'] = $this->app->share(function() { return new Filesystem; });

$app['files'],因此,当您调用File::get()时,它将最终调用匿名函数,在这种情况下,将调用下一行

to $app['files'] so, when you call the File::get(), it finally calls the anonymous function and in this case, the following line

return static::$resolvedInstance[$name] = static::$app[$name];

调用static::$app['file'];的函数,此函数返回实例,但是在返回之前,它将实例存储在$resolvedInstance变量中,因此,下次它可以从变量返回实例,而无需再次调用匿名函数. 因此,看起来static::$resolvedInstance[$name] = static::$app[$name];调用了匿名函数,该函数返回实例,并且该app是通过启动过程启动的较早注册的函数.

Calls the function for static::$app['file']; and this function returns the instance but before returning, it stores the instance in the $resolvedInstance variable, so, next time it can return the instance from the variable without calling the anonymous function again. So, it looks like that, static::$resolvedInstance[$name] = static::$app[$name]; calls the anonymous function which returns the instance and this function was registered earlier, when the app was started through boot up process.

重要提示:

Application扩展了Container,而Container扩展了 ArrayAccess 类,这就是为什么可以使用数组符号设置(获取)$app对象的属性.

Application extends Container and Container extends ArrayAccess class and that's why, a property of the $app object could be (accessed) set/get using array notation.

我试图给您一个想法,但是您必须逐步查看代码,您不会只阅读/跟踪一次代码就可以得到它.

I've tried to give you an idea but you have to look in to the code, step by step, you won't get it only reading/tracking the code once.

这篇关于Laravel 4:立面如何解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆