/**
* Bind all of the application paths in the container.
*
* @return void
*/
protected function bindPathsInContainer()
{
$this->instance('path', $this->path());
/********/
$this->instance('path.bootstrap', $this->bootstrapPath());
}
/**
* Register an existing instance as shared in the container.
*
* @param string $abstract
* @param mixed $instance
* @return mixed
*/
public function instance($abstract, $instance)
{
/* 折叠 */
// 程序继续运行,执行到此。
$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);
}
return $instance;
}
该运行 $this->bound 了
/**
* Determine if the given abstract type has been bound.
*
* @param string $abstract
* @return bool
*/
public function bound($abstract)
{
// $this->bindings == $this->instances == [];
// 通过下面的 isAlias 可以知道,此时这里相当于 return false || false || false;
// 返回了 false
return isset($this->bindings[$abstract]) ||
isset($this->instances[$abstract]) ||
$this->isAlias($abstract);
}
/**
* Determine if a given string is an alias.
*
* @param string $name
* @return bool
*/
public function isAlias($name)
{
return isset($this->aliases[$name]);
}
/**
* Register an existing instance as shared in the container.
*
* @param string $abstract
* @param mixed $instance
* @return mixed
*/
public function instance($abstract, $instance)
{
/* 折叠 */
// 程序继续运行,执行到此。
// 又清空了一遍。
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['path'] = '/app'
$this->instances[$abstract] = $instance;
// $isBound == false
if ($isBound) {
$this->rebound($abstract);
}
// return '/path/to/var/www/app';
return $instance;
}
回到 $this->instance() 被调用的位置,如下:
/**
* Bind all of the application paths in the container.
*
* @return void
*/
protected function bindPathsInContainer()
{
$this->instance('path', $this->path());
/********/
$this->instance('path.bootstrap', $this->bootstrapPath());
}
Day 4
composer 的内容已经基本看过了,在
public/index.php
中,进行到这里了。即
bootstrap/app.php
这里的
Illuminate\Foundation\Application
在vendor/laravel/framework/src/Illuminate/Foundation/Application.php
这里看到,Illuminate\Foundation\ApplicationApplication 类继承了
Illuminate\Container\Container
类。 Illuminate\Foundation\ApplicationApplication 实现了如下接口:而继承的 Illuminate\Container\ContainerContainer 类实现了如下接口:
先不看这些接口,因为这些接口只是相当于一个类的框架(骨头),先看实现。 那么就要找
Illuminate\Foundation\Application
和Illuminate\Container\Container
的__construct
Illuminate\Foundation\ApplicationApplication 类有构造方法,而 Illuminate\Container\ContainerContainer 没有构造方法。 所以先看这里
vendor/laravel/framework/src/Illuminate/Foundation/Application.php
的__construct
这里调用了
$this->setBasePath
, 并传入了 basePath 即 laravel 项目的根目录/
(非网站根目录/public
); 而在$this->setBasePath
中是这样的:$this->bindPathsInContainer()
先看第一条
$this->instance('path', $this->path());
'path'
不用说了,是个 string ,重点先看这个$this->path()
此时 我们知道了,在
$this->instance()
中的全部参数,即:$this->instance('path', '/path/to/var/www/app');
这里的
$this->instance('path', '/path/to/var/www/app')
调用的是父类即 Illuminate\Container\ContainerContainer 的instance
方法 在 Illuminate\Container\ContainerContainer 类文件vendor/laravel/framework/src/Illuminate/Container/Container.php
中找到该方法https://github.com/Samtoto/laravel-review/blob/8ee3b5dd61b78bfb023984ed573eb7dff442011a/laravel/vendor/laravel/framework/src/Illuminate/Container/Container.php#L404-L429
这里调用了
$this->removeAbstractAlias($abstract);
也就是$this->removeAbstractAlias('path');
回到
$this->instance('path', '/path/to/var/www/app')
该运行
$this->bound
了回到
$this->instance('path', '/path/to/var/www/app')
回到
$this->instance()
被调用的位置,如下:这里可以举一反三知道, 对
(array) $this->instances
进行了赋值. 通过dd($this->instances)
打印一下看看回到调用了
$this->bindPathsInContainer
的位置file: vendor/laravel/framework/src/Illuminate/Foundation/Application.php
可以看到,这里直接返回了,回到
__construct
继续$this->registerBaseBindings();
file: vendor/laravel/framework/src/Illuminate/Foundation/Application.php