In-depth analysis of the Package in Laravel5.5, automatic Discovery of Package Auto Discovery, laravel5.5package

Source: Internet
Author: User
Tags autoload

In-depth analysis of the Package in Laravel5.5, automatic Discovery of Package Auto Discovery, laravel5.5package

Preface

In earlier Laravel versions, the installation package usually requires several steps, such as adding a service provider to the app configuration file and registering related facades. Now, from Laravel 5.5, Laravel can automatically detect and register the service providers and facades.

This article focuses not on how it is used, but on its source code and how Package Auto Discovery is implemented.

Composer. json

All origins come from composer. json. When using composer, you can specify the script you want to execute in the post-autoload-dump section. For example, when using Laravel 5.5, we can see the following definition:

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

I am very familiar with postAutoloadDump. all earlier versions of Laravel are available. Its job is to clear some caches and delete some old files.

Our focus is@php artisan package:discover, That is, it will execute@php artisan package:discover This command.

What is this command? It is actually located in Illuminate \ Foundation \ Console \ PackageDiscoverCommand, mainly by executing Illuminate \ Foundation \ PackageManifestbuild() To discover the package.

PackageManifest has long been registered in Laravel's Container, so it can ensure that PackageManifest can be used every time Laravel is started.build() Method, thisbuild()The main logic of the method is:

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

At this time, Laravel maps the content to the extra. laravel part, for example:

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

Laravel first reads the above content and places it in a collection, then checks the definition of the following part, and determines whether to execute the Package Discover action:

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

If you do not want to execute@php artisan package:discover You can directly fill in * in the dont-discover array *.

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

<?php return array ( 'barryvdh/laravel-debugbar' => array ( 'providers' => array (  0 => 'Barryvdh\\Debugbar\\ServiceProvider', ), 'aliases' => array (  'Debugbar' => 'Barryvdh\\Debugbar\\Facade', ), ),);

How does Laravel obtain Package information?

At this time, we need to look at the following two services when the Laravel project is started:

  • \ Illuminate \ Foundation \ Bootstrap \ RegisterFacades
  • \ Illuminate \ Foundation \ Bootstrap \ RegisterProvider

The first service above will use Illuminate \ Foundation \ AliasLoader to load all alias, in AliasLoader:

// in RegisterFacades::bootstrap()AliasLoader::getInstance(array_merge( $app->make('config')->get('app.aliases', []), $app->make(PackageManifest::class)->aliases()))->register();

As you can see, it first reads the app. php aliases array, and then with bootstrap/cache/packages. php packages array merge. At this time, you can obtain all packages information for discovery and built-in loading. In this way, you can define your alias directly in app. php to overwrite the automatically discovered package, so that your project can still run smoothly.

Summary

This article combs the principles and source code of Laravel 5.5 Package Auto Discovery, hoping to help you understand Laravel 5.5 Package Auto Discovery.

Well, the above is all the content of this article. I hope the content of this article has some reference and learning value for everyone's learning or work. If you have any questions, please leave a message to us, thank you for your support.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.