天天看点

laravel5.5 php,Laravel 5.5) 加载过程instance方法

在bootstrap/app.php

$app = new Illuminate\Foundation\Application( realpath(__DIR__.'/../')

);

实例化 vendor/laravel/framework/src/Illuminate/Foundation/Application.php类 该类的魔术方法

public function __construct($basePath = null)

{

if ($basePath) { $this->setBasePath($basePath);

}

$this->registerBaseBindings();

$this->registerBaseServiceProviders();

$this->registerCoreContainerAliases();

}

查看注册 app 和container到 instances数组中

protected function registerBaseBindings()

{ static::setInstance($this);

$this->instance('app', $this);

$this->instance(Container::class, $this);

}

查看今天主要的方法 instance

流程图

laravel5.5 php,Laravel 5.5) 加载过程instance方法

public function instance($abstract, $instance)

{

$this->removeAbstractAlias($abstract);

$isBound = $this->bound($abstract);

unset($this->aliases[$abstract]); // We'll check to determine if this type has been bound before, and if it has

// we will fire the rebound callbacks registered with the container and it

// can be updated with consuming classes that have gotten resolved here.

$this->instances[$abstract] = $instance;

if ($isBound) { $this->rebound($abstract);

}

}

第一个方法 removeAbstractAlias

protected function removeAbstractAlias($searched)

{

if (! isset($this->aliases[$searched])) { return;

}

foreach ($this->abstractAliases as $abstract => $aliases) { foreach ($aliases as $index => $alias) { if ($alias == $searched) { unset($this->abstractAliases[$abstract][$index]);

}

}

}

}

第二个方法: bound

public function bound($abstract)

{ return isset($this->bindings[$abstract]) || isset($this->instances[$abstract]) || $this->isAlias($abstract);

}

第三个方法: rebound

protected function rebound($abstract)

{

$instance = $this->make($abstract);

foreach ($this->getReboundCallbacks($abstract) as $callback) { call_user_func($callback, $this, $instance);

}

}