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











There are four main error types in PHP: 1.Notice: the slightest, will not interrupt the program, such as accessing undefined variables; 2. Warning: serious than Notice, will not terminate the program, such as containing no files; 3. FatalError: the most serious, will terminate the program, such as calling no function; 4. ParseError: syntax error, will prevent the program from being executed, such as forgetting to add the end tag.

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.

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 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.

HTTP request methods include GET, POST, PUT and DELETE, which are used to obtain, submit, update and delete resources respectively. 1. The GET method is used to obtain resources and is suitable for read operations. 2. The POST method is used to submit data and is often used to create new resources. 3. The PUT method is used to update resources and is suitable for complete updates. 4. The DELETE method is used to delete resources and is suitable for deletion operations.

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

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

In PHPOOP, self:: refers to the current class, parent:: refers to the parent class, static:: is used for late static binding. 1.self:: is used for static method and constant calls, but does not support late static binding. 2.parent:: is used for subclasses to call parent class methods, and private methods cannot be accessed. 3.static:: supports late static binding, suitable for inheritance and polymorphism, but may affect the readability of the code.
