Home Backend Development PHP Tutorial Detailed introduction to Package Auto Discovery in Laravel5.5

Detailed introduction to Package Auto Discovery in Laravel5.5

Sep 15, 2017 am 09:39 AM
package

众所周知Laravel 5.5 发布在即,目前已经确定会增加一个神奇的新特性:Package Auto Discovery。下面这篇文章主要给大家深入的介绍了关于Laravel5.5中包自动发现Package Auto Discovery的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。

前言

在之前的 Laravel 版本中,安装包通常需要几个步骤,例如添加服务提供器到 app 配置文件并注册相关的 facades。现在,从 Laravel 5.5 开始,Laravel 可以自动检测并注册服务提供器和 facades。

本文不是聚焦于他是怎么用的,而是看看它的源码,是怎么实现Package Auto Discovery的。

composer.json

一切的起源都是来自 composer.json ,在使用 composer 的时候,你可以在 post-autoload-dump 部分指定你想执行的脚本,比如在 Laravel 5.5 的时候,我们可以看到这样的定义:


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

对于 postAutoloadDump 是很熟悉了,Laravel 之前的版本基本都有,它的工作是清理一些缓存,删除一些旧的文件。

我们的关注重点是@php artisan package:discover ,也就是会执行@php artisan package:discover 这个命令。

这个命令是干嘛的呢?它其实是位于Illuminate\Foundation\Console\PackageDiscoverCommand 中,主要是通过执行Illuminate\Foundation\PackageManifest 的 build() 方法来达到 发现 package 的目的。

而 PackageManifest 早就注册在 Laravel 的 Container 中,那么它可以保证每次在启动 Laravel 的时候都能使用 PackageManifest 的 build() 方法,这个 build() 方法主要的逻辑就是:

找寻 vendor/composer/installed.json 这个文件,这个文件是 composer 自己生成的,记录着每一次的 composer autoload 的 class map。

而此时,Laravel 又将这些内容映射到 extra.laravel 的部分,比如:


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

Laravel 首先将以上内容直接读取下来放到一个 collection 中,然后在去检查下面这个部分的定义,在决定是否需要执行 Package Discover 动作:


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

如果你不想执行 @php artisan package:discover 的发现效果,可以直接在 dont-discover 的数组里面填上 *。

这样判断完成之后,Laravel 将 collection 中需要发现的 Package 内容保存到一个缓存文件中bootstrap/cache/packages.php:


<?php return array (
 &#39;barryvdh/laravel-debugbar&#39; => array (
 &#39;providers&#39; => array (
  0 => &#39;Barryvdh\\Debugbar\\ServiceProvider&#39;,
 ),
 &#39;aliases&#39; => array (
  &#39;Debugbar&#39; => &#39;Barryvdh\\Debugbar\\Facade&#39;,
 ),
 ),
);
Copy after login

Laravel 怎么拿到 Package 信息?

这个时候,我们又需要看看 Laravel 项目启动的时候,主要启动下面的两个服务:

  • \Illuminate\Foundation\Bootstrap\RegisterFacades

  • \Illuminate\Foundation\Bootstrap\RegisterProvider

而 上面的第一个服务会使用 Illuminate\Foundation\AliasLoader 加载所有的 alias ,在 AliasLoader 中:


// in RegisterFacades::bootstrap()

AliasLoader::getInstance(array_merge(
 $app->make(&#39;config&#39;)->get(&#39;app.aliases&#39;, []),
 $app->make(PackageManifest::class)->aliases()
))->register();
Copy after login

你可以看到,它首先读取 app.php 的 aliases 数组,然后与 bootstrap/cache/packages.php 的 packages 数组合并,这个时候,就可以获取到所有的 packages 信息进行发现和自带加载了。这样合并的好处就是,你还是可以直接在 app.php中定义你的 alias 来覆盖自动发现的 package,从而保证你的项目还是可以运行得很流畅。

总结

本文将 Laravel 5.5 Package Auto Discovery 原理和源码梳理的一遍,希望对你了解 Laravel 5.5 Package Auto Discovery 有所帮助。

The above is the detailed content of Detailed introduction to Package Auto Discovery in Laravel5.5. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1272
29
C# Tutorial
1252
24
Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

How does PHP type hinting work, including scalar types, return types, union types, and nullable types? How does PHP type hinting work, including scalar types, return types, union types, and nullable types? Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

See all articles