天天看點

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);

}

}