Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of automatic packet Discovery Package Auto Discovery in Laravel5.5

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you the example analysis of automatic Package Auto Discovery discovery of packages in Laravel5.5. I hope you will get something after reading this article. Let's discuss it together.

Preface

In previous versions of Laravel, installing the package typically required several steps, such as adding a service provider to the app configuration file and registering the relevant facades. Now, starting with Laravel 5.5, Laravel can automatically detect and register service providers and facades.

Composer.json

The origin of everything comes from composer.json, and when using composer, you can specify the script you want to execute in the post-autoload-dump section, for example, in Laravel 5.5, we can see this definition:

"scripts": {"post-autoload-dump": ["Illuminate\\ Foundation\\ ComposerScripts::postAutoloadDump", "@ php artisan package:discover"]}

I am very familiar with postAutoloadDump, which is basically available in previous versions of Laravel. Its job is to clean up some caches and delete some old files.

Our focus is on @ php artisan package:discover, that is, the command @ php artisan package:discover will be executed.

What is this order for? It is actually located in Illuminate\ Foundation\ Console\ PackageDiscoverCommand, mainly by executing the build () method of Illuminate\ Foundation\ PackageManifest to discover package.

While PackageManifest has long been registered in Laravel's Container, it ensures that every time you start Laravel, you can use PackageManifest's build () method. The main logic of this build () method is:

Look for the vendor/composer/installed.json file, which is generated by composer itself and records the class map of each composer autoload.

At this point, Laravel maps these contents to parts of extra.laravel, such as:

"extra": {"laravel": {"providers": ["Barryvdh\\ Debugbar\ ServiceProvider"], "aliases": {"Debugbar": "Barryvdh\\ Debugbar\\ Facade"}

Laravel first reads the above directly and puts it into a collection, and then checks the definition of the following section before deciding whether to perform the Package Discover action:

"extra": {"laravel": {"dont-discover": ["barryvdh/laravel-debugbar"]}}

If you don't want to perform the @ php artisan package:discover discovery effect, you can simply fill in the array of dont-discover with *.

After this judgment is completed, Laravel saves the Package content that needs to be found in collection to a cache file bootstrap/cache/packages.php:

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report