------ tp5 underlying mechanism automatically load the source code analysis like tp5.1

tp framework as a mainstream framework, has released version 6.0, which is equivalent 3. * version is reconstructed, and today we achieved to study under tp5.1 automatically loaded from the source point of view

    As a single entry frame, the file looks from the entrance, entry file in public / under, then why should the majority of the frame entrance files into subfolders beneath it?

 First, in order to separate movement, because now tp framework are generally single entry, since it is a single entry, then must do rewrite, if the static files and program files put together.

Routing framework is bound to be screened each request, so these frameworks spontaneously to separate resource files and program files area, on a different folder, so the whole

Point of view, is why the entrance will be in the subdirectory.

   Second, for safety, privileges under very strict division of linux, decibel atmosphere read, write, execute. On this basis, all files are divided into groups, where the group and other groups. This division can

Better file permissions to sort out, to avoid upload vulnerability (user upload php file is executed), and so on.

    1. We look at the entry file:

 

    2. tp5.1 introduced entry file loaded base.php file, and then base.php file loaded loader.php class, and performed Loader :: register () static method, we look at the implementation of the internal register method what?

 2.1) built-in function is executed in the 79th row apl_autoloader_register i.e. register () method (), the first parameter to this function receiving an anonymous function or callback method, whenever role php

Will be executed when calling a class that does not exist among the callback function, and carries a parameter value is the name of the class (if the class name space), as there is not registered with the namespace introduced in line base.php20 exception mechanism, then this is the parameter value carries is: think \ Error.

  

 2.2) continue to look down, 80 rows with 81 Loader class, namely, to get the absolute path of the project and obtain the absolute path vender directory composer folder, we look at the printout

Behind 2.3) line 85, to determine whether there is a composer folder, the existence of autoload_static.php file, since version 5.1, php official website is no longer available for download version, only supports download through the composer, so this file must exist. Then load the class file.

2.4) 89 lines executed get_declared_classes () function, which function is to obtain an array defined by the current script in the name of the class composition (including the introduction of their own class, and php built-in classes). Line 90 is then removed last element of this array, i.e. autoload_static.php just introduced in class, the return value is: Composer \ Autoload \ ComposerStaticInit3ec0ccb9b30037c3270e4e4566239878

2.5) 91 lines, cycles will just get to class members to copy the properties of this class Loader, there are two static member properties in commercial law class: $ prefixLengthsPsr4, $ prefixDirsPsr4. FIG form as follows:

 The two members of the attribute is generated based psr-4 specification rules, you can not understand their own Baidu understand. Here copy to the member properties of this class, the file path looks for classes will be used when loading files behind, following repeat.

3) We look at the registered namespace definition: Registered think two and two tratis folder path, call self :: addNamespace method, the main thing is to do these two namespaces, file path psr-4 specification form member properties added to the two mentioned above $ prefixLengthsPsr4, $ prefixDirsPsr4. $ PrefixLengthsPsr4 rules: The first letter namespace dimensional array as a first key, a second key as the namespace dimensional array, as this namespace string length value of the second dimension of the array. $ PrefixDirsPsr4 rules: namespace as the first key-dimensional array, the corresponding absolute file path as the second-dimensional array of values, key set the second digit is incremented index value. At this time, static members of this class attribute $ prefixLengthsPsr4, $ prefixDirsPsr4 values ​​as shown below:

 4) .106 line that loads the library mapping file,

It looks for the project root directory \ runtime \ classmap.php file, one-dimensional array of values ​​in this file assigned to the members of this class attribute $ classMap. This file is executed by tp5.1 command line command: php think optimize: autoload generation. The resulting file contains the mapping of absolute path name of the class and file all that will be introduced in class, this document will improve the efficiency of search of documents generated during the general project is completed, if the follow-up there is new to the group, this file needs to be regenerated to find new types of documents. It will be mentioned later why would improve the efficiency of loading the class. The default is no such document.

5) The last line of this method is 118 lines, automatic load extend the directory, call self :: addAutoLoadDir () method to do that: to extend the project root directory absolute path into the member properties in the $ fallbackDirsPsr4.

6) The method of execution is automatically loaded when Loader :: autoload

  Mentioned above spl_autoload_register () function, the implementation of this function in the argument of the call if the class does not exist. So autoload will call the method of the class. This method code:

Above that, the parameters of this method $ class class name with namespace value, line 127 is determined whether the presence of such names in this class $ classAlias ​​member attribute, if present, such will set the alias names, start value of this property is an empty array. Continue down, call self :: findFile method and class name passed as a parameter and return value $ file, below the 140 line introduced this document. Let's look at what things do findFile method:

 6.1) 143 line, first determine whether there is such a name key member properties $ classmap, if there is a direct return. We say over the implementation of this member property value is generated tp5.1 command line, so after the files are no longer generated execution cycle to determine whether a file exists down, directly returns the file path, file search efficiency can be improved.

6.2)

If logic lookup is performed in accordance with the class file psr-4 specification, it is specifically: extraction with the first letter of the name space class, it is determined whether the member attributes $ prefixLengthsPsr4 first letter key exists, this key then cycle value, it is determined whether there exists a namespace $ prefixLengthsPsr4 dimensional bond, if present, remove the absolute path corresponding to the namespace attribute according $ prefixDirsPsr4, and then stitching the file name, whether the file exists, if the absolute path of the file is returned.

6.3)

 All values ​​splicing file name under cyclic $ fallbackDirsPsr4 property that returns the absolute path and determine whether the file exists, if there is returned.

6.4):

 

 Psr-0 according to whether the file exists and determines rule returns, psr-4 similar to the above with

 

Guess you like

Origin www.cnblogs.com/candii/p/11774172.html