Home >Development Tools >composer >Composer implements automatic loading of projects (Composer manages dependency packages)

Composer implements automatic loading of projects (Composer manages dependency packages)

藏色散人
藏色散人forward
2020-01-03 16:22:363277browse

Composer implements automatic loading of projects (Composer manages dependency packages)

ComposerManage dependent packages and realize automatic loading

composer uses require and remove commands Add and delete dependency packages to the project.

composer can implement the PSR-4 automatic loading specification by configuring the composer.json file in the project root directory.

Detailed explanation of the composer.json architecture. Here are details on how to configure this file.

By studying the source code of TP5.1, the automatic loading of TP5 is mainly rewritten based on the ClassLoader class produced by composer in the vendor directory after the composer install command is executed. The Composer automatic loading mechanism establishes the mapping relationship between the namespace and the file relative to the project root directory path, and then registers the automatic loading of the system through the spl_autoload_register method. Among the automatic loading logic, the most important thing is how to search based on the path generated above. to the corresponding file and include it.

Composer automatic loading detailed process:

● Configure the automatic loading mapping rules of autoload in the composer.json file in the root directory.

● Call the composer install/update command to generate the vendor/composer folder and related files. The mapping between namespace and file path is saved in the autoload_psr4.php file. At the same time, the ClassLoader.php file is generated, which is an automatically loaded tool class. By parsing the namespace, relevant files are found for inclusion.

● Include the autoload.php file under the vendor folder in the entry file. If you want to change the auto-loading related functions and customize some operations, you can write a loader tool class based on the ClassLoader file generated by composer. The premise is to understand the auto-loading principle of composer and the PSR4 auto-loading specification. For details, please refer to the implementation of the Loader class in tp5.

For more composer-related technical articles, please visit the composer column: https://www.php.cn/tool/composer/

The above is the detailed content of Composer implements automatic loading of projects (Composer manages dependency packages). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete