Detailed introduction to Package Auto Discovery in Laravel5.5
众所周知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" ] }
对于 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" } } }
Laravel 首先将以上内容直接读取下来放到一个 collection 中,然后在去检查下面这个部分的定义,在决定是否需要执行 Package Discover 动作:
"extra": { "laravel": { "dont-discover": [ "barryvdh/laravel-debugbar" ] } }
如果你不想执行 @php artisan package:discover
的发现效果,可以直接在 dont-discover 的数组里面填上 *。
这样判断完成之后,Laravel 将 collection 中需要发现的 Package 内容保存到一个缓存文件中bootstrap/cache/packages.php:
<?php return array ( 'barryvdh/laravel-debugbar' => array ( 'providers' => array ( 0 => 'Barryvdh\\Debugbar\\ServiceProvider', ), 'aliases' => array ( 'Debugbar' => 'Barryvdh\\Debugbar\\Facade', ), ), );
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('config')->get('app.aliases', []), $app->make(PackageManifest::class)->aliases() ))->register();
你可以看到,它首先读取 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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Alipay PHP...

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...
