Home  >  Article  >  Backend Development  >  Introducing the loading process and principle of Facade in Laravel

Introducing the loading process and principle of Facade in Laravel

巴扎黑
巴扎黑Original
2017-09-26 09:51:301439browse

Facade is actually a static proxy of a class in the container. It allows you to statically call any method of any object stored in the container. The following article mainly introduces you to the loading process of Facade in Laravel. For information related to the principle, friends in need can refer to it.

Preface

This article mainly introduces the relevant content about the Facade loading process and principles in Laravel, and shares it for your reference and study. Not much to say below, let’s take a look at the detailed introduction.

Introduction

Facades (pronounced: /fəˈsäd/) provide a " Static" interface. You don't have to use a bunch of namespaces or instantiate the object to access its specific methods.


use Config;

class Test
{
 public function index()
 {
 return Config::get('app.name');
 }
}

Facade startup and registration

Facade startup guidance is in Illuminate\ Registered in Foundation\Bootstrap\RegisterFacades.


public function bootstrap(Application $app)
{
 Facade::clearResolvedInstances();
 Facade::setFacadeApplication($app);

 AliasLoader::getInstance(array_merge(
 $app->make('config')->get('app.aliases', []),
 $app->make(PackageManifest::class)->aliases()
 ))->register();
}

The default alias configuration is read from aliases under the app configuration file. PackageManifest is a new package automatic discovery rule added by laravel 5.5. Here we will temporarily Aliases provided by PackageManifest packages are not considered.

Among them, array_merge returns an array in the following format:


 "App" => "Illuminate\Support\Facades\App"
 "Artisan" => "Illuminate\Support\Facades\Artisan"
 "Auth" => "Illuminate\Support\Facades\Auth"
 "Blade" => "Illuminate\Support\Facades\Blade"
 ...

The above code will register all facades for automatic loading through AliasLoader. The core is php's spl_autoload_register.


 /**
 * Prepend the load method to the auto-loader stack.
 *
 * @return void
 */
 protected function register()
 {
 if (! $this->registered) {
  spl_autoload_register([$this, 'load'], true, true);

  $this->registered = true;
 }
 }

After the registration is completed, all subsequent use classes will be automatically loaded through the load function.

Note: When defining spl_autoload_register here, the last parameter is passed true. When this parameter is true, spl_autoload_register() will add the function to the head of the queue instead of the tail. (Automatic loading is completed first through this function)

In other words,


<?php

use Config;
use App\User;

class Test
{
 public function index()
 {
 Config::get(&#39;app.name&#39;);
 new User();
 }
}

No matter what we use is the specific existing class (App \User) or alias (Config), the automatic loading will be completed first through the load function. When the function returns false, other automatic loading functions will complete the automatic loading (such as composer psr-4).

In the load method of AliasLoader, the class_alias function is mainly used to implement automatic loading of aliases.


public function load($alias)
{
 if (isset($this->aliases[$alias])) {
 return class_alias($this->aliases[$alias], $alias);
 }
}

About class_alias Here is an official example:


class foo { }

class_alias(&#39;foo&#39;, &#39;bar&#39;);

$a = new foo;
$b = new bar;

// the objects are the same
var_dump($a == $b, $a === $b); //true
var_dump($a instanceof $b); //false

// the classes are the same
var_dump($a instanceof foo); //true
var_dump($a instanceof bar); //true

var_dump($b instanceof foo); //true
var_dump($b instanceof bar); //true

Loading of Facade

When we use Facade, such as:


<?php

use Config;

class Test
{
 public function index()
 {
 Config::get(&#39;app.name&#39;);
 }
}

In fact, Illuminate\Support\Facades is loaded \Config class (because we have registered class_alias), equivalent to:


<?php

use Illuminate\Support\Facades\Config;

class Test
{
 public function index()
 {
  Config::get(&#39;app.name&#39;);
 }
}

And all Facades inherit from the Illuminate\Support\Facades\Facade class, in this base A __callStatic method is defined in the class so that we can easily use the Facade (without instantiation).


<?php

public static function __callStatic($method, $args)
{
 $instance = static::getFacadeRoot();

 if (! $instance) {
  throw new RuntimeException(&#39;A facade root has not been set.&#39;);
 }

 return $instance->$method(...$args);
}

The getFacadeRoot method is used to obtain the specific instance column of the alias class. We know that all Facade classes need to define a getFacadeAccessor method. Possible return values ​​of this method are:

  • String type string (such as config, db)

  • String type string-like ( Such as App\Service\SomeService)

  • Object specific instantiated object

  • Closure closure

For example, the getFacadeAccessor method of Config Facade is as follows:


protected static function getFacadeAccessor()
{
 return &#39;config&#39;;
}

getFacadeRoot method will retrieve the corresponding from the container based on the return value of getFacadeAccessor() Real object.


public static function getFacadeRoot()
{
 $name = static::getFacadeAccessor();
 
 if (is_object($name)) {
  return $name;
 }

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

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

Since the config instance has been registered in the APP container


<?php
//Illuminate\Foundation\Bootstrap/LoadConfiguration

$app->instance(&#39;config&#39;, $config = new Repository($items));

so\Config: :get('app.name', 'dafault') The actual access is the get('app.name', 'default') method of the Repository actual column.

The above is the detailed content of Introducing the loading process and principle of Facade in Laravel. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn