1,我们需要阐明PHP开发,所以让我们打开灯。这引导框架并准备好使用它,然后它将加载此应用程序,以便我们可以运行它并发送响应回到浏览器并让我们的用户满意。(原文翻译)
/*
|--------------------------------------------------------------------------
| Turn On The Lights
|--------------------------------------------------------------------------
|
| We need to illuminate PHP development, so let us turn on the lights.
| This bootstraps the framework and gets it ready for use, then it
| will load up this application so that we can run it and send
| the responses back to the browser and delight our users.
|
*/
$app = require_once __DIR__.'/../bootstrap/app.php';
2,查看app.php
2.1,我们要做的第一件事是创建一个新的Laravel应用程序实例它是Laravel所有组件的“胶水”,而且是系统的IoC容器绑定所有各个部分。(原文翻译)
|--------------------------------------------------------------------------
| Create The Application
|--------------------------------------------------------------------------
|
| The first thing we will do is create a new Laravel application instance
| which serves as the "glue" for all the components of Laravel, and is
| the IoC container for the system binding all of the various parts.
|
*/
$app = new Illuminate\Foundation\Application(
$_ENV['APP_BASE_PATH'] ?? dirname(__DIR__)
);
2.2 查看 Illuminate\Foundation\Application 下的construct方法 ,(创建一个新的Illuminate应用程序实例)。
**
* Create a new Illuminate application instance.
*
* @param string|null $basePath
* @return void
*/
public function __construct($basePath = null)
{
if ($basePath) {
$this->setBasePath($basePath);
}
//注册基础绑定
$this->registerBaseBindings();
//注册基础服务
$this->registerBaseServiceProviders();
//注册容器别名
$this->registerCoreContainerAliases();
}
首先查看第一个方法registerBaseBindings,把基本的绑定容器
2.2.1,调用setInstance ,将现有的实例注册为实例中的共享容器,查看setInstance这个方法
/**
* Register the basic bindings into the container.
*
* @return void
*/
protected function registerBaseBindings()
{
//1,调用静态方法setInstance设置实例
static::setInstance($this);
//2,调用Instance ,将现有的实例注册为实例中的共享容器,查看Instance这个方法(代码instance)
//注册app,Container
$this->instance('app', $this);
$this->instance(Container::class, $this);
//3,在容器中注册mix获取版本化的mix文件路径
$this->singleton(Mix::class);
$this->instance(PackageManifest::class, new PackageManifest(
new Filesystem, $this->basePath(), $this->getCachedPackagesPath()
));
}
代码instance
//代码instance
public function instance($abstract, $instance)
{
//从上下文绑定别名缓存中删除别名,查看removeAbstractAlias方法(代码removeAbstractAlias)
$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);
}
return $instance;
}
removeAbstractAlias代码
removeAbstractAlias代码
/**
* Remove an alias from the contextual binding alias cache.
*
* @param string $searched
* @return void
*/
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]);
}
}
}
}
2.2.2查看registerBaseServiceProviders 注册所有基本服务提供商
/**
* Register all of the base service providers.
*
* @return void
*/
protected function registerBaseServiceProviders()
{
//注册事件服务
$this->register(new EventServiceProvider($this));
//注册日志
$this->register(new LogServiceProvider($this));
//注册路由
$this->register(new RoutingServiceProvider($this));
}
2.2.3 registerCoreContainerAliases 注册核心容器别名
3,接下来,我们需要将一些重要的接口绑定到容器中我们将能够在需要时解决它们。内核服务于来自Web和CLI的对此应用程序的传入请求。
/*
|--------------------------------------------------------------------------
| Bind Important Interfaces
|--------------------------------------------------------------------------
|
| Next, we need to bind some important interfaces into the container so
| we will be able to resolve them when needed. The kernels serve the
| incoming requests to this application from both the web and CLI.
|
*/
//注册一些共享的核心类
$app->singleton(
Illuminate\Contracts\Http\Kernel::class,
App\Http\Kernel::class
);
$app->singleton(
Illuminate\Contracts\Console\Kernel::class,
App\Console\Kernel::class
);
$app->singleton(
Illuminate\Contracts\Debug\ExceptionHandler::class,
App\Exceptions\Handler::class
);
网友评论