Thinkphp6 source analysis of analytical request

Time:2021-5-3

Get request object

Before we get to the point, we need to focus on the make method. Because people who have used it in the last version have begun to accept the concept of container. This new version enhances the function of container. The essence of creating a request object is the make method. Rather, the whole framework core class is using this method. Let’s take a look at how this method creates objects. Please refer to the notes of each paragraph for details

public function make(string $abstract, array $vars = [], bool $newInstance = false)
{
    //First of all, the instances property is an array, which is the main storage container object
    //If the object exists in the container and does not need to be recreated, it is directly obtained from the container
    if (isset($this->instances[$abstract]) && !$newInstance) {
        return $this->instances[$abstract];
    }
    //The bind attribute has been touched from the previous application initialization,
    //This is the binding identifier of the container object. Different from the instances array,
   //It just stores a class string. You need to get the object instance after instantiation. Here is the function

    //If the bind ID exists
    if (isset($this->bind[$abstract])) {
        //Get from bind
        $concrete = $this->bind[$abstract];
        //Concrete may be a class name or an anonymous function
        //Direct execution of anonymous functions
        if ($concrete instanceof Closure) {
            $object = $this->invokeFunction($concrete, $vars);
        } else {
            //Otherwise, continue to make because the object has not been created yet
            return $this->make($concrete, $vars, $newInstance);
        }
    } else {
        //If the required class name is not specified in the identifier, it needs to be instantiated.
        //In fact, this is the real creation class, and the following methods directly depend on injection
        $object = $this->invokeClass($abstract, $vars);
    }
    //The classes that do not need to create instances are directly managed in the container
    if (!$newInstance) {
        $this->instances[$abstract] = $object;
    }
    //Finally, the object is returned
    return $object;
}

Maybe I’m still a little confused here. I’ll just use request creation here to sort it out. Let’s look at the process of creating a request.

analysis

The following code is OKrunMethod, this code needs careful scrutiny, otherwise you can’t know thisRequestWhich is the target?

//Automatically create request object
$request = $request ?? $this->app->make('request', [], true);
$this->app->instance('request', $request);

$newinstance is set to true when a request is created, indicating that each request needs to be re created. At this time, instances does not have a request object, so continue to ignore it.
Before entering the bind, let’s see what’s in the bind?

array(22) {
  ["app"]=>
  string(9) "think\App"
  ["cache"]=>
  string(11) "think\Cache"
  ["config"]=>
  string(12) "think\Config"
  ["console"]=>
  string(13) "think\Console"
  ["cookie"]=>
  string(12) "think\Cookie"
  ["db"]=>
  string(8) "think\Db"
  ["env"]=>
  string(9) "think\Env"
  ["event"]=>
  string(11) "think\Event"
  ["http"]=>
  string(10) "think\Http"
  ["lang"]=>
  string(10) "think\Lang"
  ["log"]=>
  string(9) "think\Log"
  ["middleware"]=>
  string(16) "think\Middleware"
  ["request"]=>
  string(13) "think\Request"
  ["response"]=>
  string(14) "think\Response"
  ["route"]=>
  string(11) "think\Route"
  ["session"]=>
  string(13) "think\Session"
  ["validate"]=>
  string(14) "think\Validate"
  ["view"]=>
  string(10) "think\View"
  ["filesystem"]=>
  string(16) "think\Filesystem"
  ["Psr\Log\LoggerInterface"]=>
  string(9) "think\Log"
  ["think\Request"]=>
  string(11) "app\Request"
  ["think\exception\Handle"]=>
  string(19) "app\ExceptionHandle"
}

By default, there are many class names. These are the default of the framework. We don’t need to worry about them. Let’s just look at the request key name and ‘think / request’, as well as the last twothink\Requestandthink\exception\Handle
Is injected in the framework initialization, not clear can see the previous section.think\RequestThat’s when it was injected.

Now let’s get to the point. It’s obvious that ‘request’ exists in bind, and the value of request is not an anonymous function, but a stringthink\Request, can only continue to create make, findthink\RequestCorresponding valueapp\RequestIt doesn’t exist in bind, so we can resolve the class name directly. It is clear here that the actual framework does not use the think / request object, but the app / request object. After parsing, the request object is also put into the container. Here is the execution of the request.


More learning content can be accessedAs long as you can read it, your salary will go up a step

The above contents hope to help youA lot of PHPer always encounter some problems and bottlenecks when they are upgrading. They write too much business code and have no sense of direction. They don’t know where to start to improve. For this, I have sorted out some materials, including but not limited to:Distributed architecture, high scalability, high performance, high concurrency, server performance tuning, tp6, laravel, yii2, redis, spool, swoft, Kafka, MySQL optimization, shell script, docker, microservice, nginxAnd so on many knowledge points advanced dry goods need can be free to share with you, need can click the link to getAdvanced PHP monthly salary 30K > > > architect growth path [free access to videos and interview documents]

In this paper, the blog group issued a multi article and other operational tools platformOpenWriterelease

Recommended Today

Large scale distributed storage system: Principle Analysis and architecture practice.pdf

Focus on “Java back end technology stack” Reply to “interview” for full interview information Distributed storage system, which stores data in multiple independent devices. Traditional network storage system uses centralized storage server to store all data. Storage server becomes the bottleneck of system performance and the focus of reliability and security, which can not meet […]