Home > PHP Framework > ThinkPHP > ThinkPHP6 source code analysis application initialization

ThinkPHP6 source code analysis application initialization

藏色散人
Release: 2019-08-29 17:56:12
forward
3635 people have browsed it

ThinkPHP6 source code analysis application initialization

App Construct

First let’s take a look at what is done in __construct, basically any framework They will do some basic operations here, that is, extend from here.

public function __construct(string $rootPath = '')
{
    $this->thinkPath   = dirname(__DIR__) . DIRECTORY_SEPARATOR;
    $this->rootPath    = $rootPath ? rtrim($rootPath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR : $this->getDefaultRootPath();
    $this->appPath     = $this->rootPath . 'app' . DIRECTORY_SEPARATOR;
    $this->runtimePath = $this->rootPath . 'runtime' . DIRECTORY_SEPARATOR;
    if (is_file($this->appPath . 'provider.php')) {
        $this->bind(include $this->appPath . 'provider.php');
    }
    static::setInstance($this);
    $this->instance('app', $this);
    $this->instance('think\Container', $this);
}
Copy after login

● Judging from the parameter rootPath of the magic method, it supports customizing the root directory path.

● Set up thinkPath, rootPath, appPath, runtimePath

● Bind the default service provider, providing a total of two, app\Reques and app\ExceptionHandle, actually you use The Request is it. Specifically go to appPath to view

● Set the current container instance APP

● Bind the App($this) instance to the container, respectively app and think\Container

here It should be noted that the App class inherits Container, so it binds its own instance to the container.

It seems that the entire application has been initialized here? Here I need to put part of the content of Request run here, because that is the main initialization work of the framework. I don't think it is reasonable to put this part of the initialization work in Request run.

Main initialization

public function initialize()
{
    $this->initialized = true;
    $this->beginTime = microtime(true);
    $this->beginMem  = memory_get_usage();
    // 加载环境变量
    if (is_file($this->rootPath . '.env')) {
        $this->env->load($this->rootPath . '.env');
    }
    $this->configExt = $this->env->get('config_ext', '.php');
    $this->debugModeInit();
    // 加载全局初始化文件
    $this->load();
    // 加载框架默认语言包
    $langSet = $this->lang->defaultLangSet();
    $this->lang->load($this->thinkPath . 'lang' . DIRECTORY_SEPARATOR . $langSet . '.php');
    // 加载应用默认语言包
    $this->loadLangPack($langSet);
    // 监听AppInit
    $this->event->trigger('AppInit');
    date_default_timezone_set($this->config->get('app.default_timezone', 'Asia/Shanghai'));
    // 初始化
    foreach ($this->initializers as $initializer) {
        $this->make($initializer)->init($this);
    }
    return $this;
}
Copy after login

● Loading the .env environment variable file

● Loading the configuration file and files within the application

● Load common.php

in the application ● Load the helper function in helper.php

● in the thinkPath directory● Load the configuration file

● Load the event in the application directory. php event

● Register the service.php service in the application directory

● Load the language pack

● Listen to the AppInit event and use this event to do some pre-request work

● Set time zone

● Inject all services and start the service

Service registration

During the initialization process, perform service registration, then What does service registration do? How to use the service?

public function register($service, bool $force = false)
{
    $registered = $this->getService($service);
    if ($registered && !$force) {
        return $registered;
    }
    if (is_string($service)) {
        $service = new $service($this);
    }
    if (method_exists($service, 'register')) {
        $service->register();
    }
    if (property_exists($service, 'bind')) {
        $this->bind($service->bind);
    }
    $this->services[] = $service;
}
Copy after login

● Whether the service has been registered, if you need to force re-registration

● Instantiation of the service

● If the register method is implemented, you need to execute the register method

● If the bind attribute is set, you need to bind the service instance to the container

● Finally merge it into the entire service array and wait for the boot

service to start

Currently, there are only the following three services during initialization. In the $this->initializers array,

foreach ($this->initializers as $initializer) {
        $this->make($initializer)->init($this);
}
Copy after login

these three services are:

think\initializer\BootService
think\initializer\Error
think\initializer\RegisterService
Copy after login

● Error service is Used to handle framework exceptions and errors

● RegisterService literally means to register services

● BootService is to enable services

Error handling will be discussed later, here it is said Let's look at RegisterService and BootService.

When RegisterService is made from Container

There is a hidden static method make. Every time the instance object is made from Container for the first time, the make method will be executed. Of course, it must first be You implement the method.

The Init method will then be executed. When you enter RegisterService, you will see this method. The method content is as follows:

public function init(App $app)
{
    $file = $app->getRootPath() . 'runtime' . DIRECTORY_SEPARATOR . 'services.php';
    $services = $this->services;
    if (is_file($file)) {
        $services = array_merge($services, include $file);
    }
    foreach ($services as $service) {
        if (class_exists($service)) {
            $app->register($service);
        }
    }
}
Copy after login

This method is very strange, a little different from what I imagined. The service is obtained directly from the runtime directory, not from service.php in the config directory. Why is this so? Due to the development of composer, the TP framework can also provide automatic discovery of packages, which also proves that the development team is constantly moving closer to the community. Let’s take a look at how this is achieved.

Because this is all due to composer, so take a look at composer.json under rootPath. Go to the bottom and you will find the following configuration

"scripts": {
    "post-autoload-dump": [
        "@php think service:discover",
        "@php think vendor:publish"
    ]
}
Copy after login

From the configuration point of view, the framework A total of two instructions are provided, service:discover and vendor:publish. I won’t go into the specific implementation here. You only need to know that package discovery is implemented by service:discover.

Also, three services are injected by default here.

PaginatorService::class,
ValidateService::class,
ModelService::class,
Copy after login

Finally let’s take a look at BootService. This is very simple. From the naming point of view, it is not difficult to see that the following is the code to start the service normally, but what needs to be explained here is that the boot method must be implemented in the service class before it can be started.

public function init(App $app)
{
    $app->boot();
}
Copy after login

The above is the initialization of the entire application, the specific details will be discussed later.

This article comes from the ThinkPHP framework technical article column: //m.sbmmt.com/phpkj/thinkphp/

The above is the detailed content of ThinkPHP6 source code analysis application initialization. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:learnku.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template