Code analysis of Autoloader module in Laravel framework
本篇文章给大家分享的内容是关于Laravel框架中Autoloader模块的代码分析,有一定的参考价值,希望可以帮助到有需要的朋友。
首先是中文注释:
<?php namespace Laravel; class Autoloader { /** * 类名到文件名得映射 * * @var array */ public static $mappings = array(); /** * PSR-0命名转换目录 * * @var array */ public static $directories = array(); /** * 命名空间和目录的映射 * * @var array */ public static $namespaces = array(); /** * 下划线类库和目录映射 * * @var array */ public static $underscored = array(); /** * 自动加载类的别名 * * @var array */ public static $aliases = array(); /** * Load the file corresponding to a given class. * * This method is registered in the bootstrap file as an SPL auto-loader. * * @param string $class * @return void */ public static function load($class) { // 尝试类是否有别名 if (isset(static::$aliases[$class])) { return class_alias(static::$aliases[$class], $class); } // 查找映射 elseif (isset(static::$mappings[$class])) { require static::$mappings[$class]; return; } // 加载这个新的类 foreach (static::$namespaces as $namespace => $directory) { # 支持函数 是否命名空间开头 在helpers.php中 if (starts_with($class, $namespace)) { return static::load_namespaced($class, $namespace, $directory); } } static::load_psr($class); } /** * 从给定的目录加载命名空间 * * @param string $class * @param string $namespace * @param string $directory * @return void */ protected static function load_namespaced($class, $namespace, $directory) { return static::load_psr(substr($class, strlen($namespace)), $directory); } /** * 使用PSR-0标准来试图解析一个类 * * @param string $class * @param string $directory * @return void */ protected static function load_psr($class, $directory = null) { // 用PSR-0来解析类 使之变成路径字符串 $file = str_replace(array('\\', '_'), '/', $class); $directories = $directory ?: static::$directories; // 获得类路径 $lower = strtolower($file); # 默认全部小写 // 尝试解析 foreach ((array) $directories as $directory) { if (file_exists($path = $directory.$lower.EXT)) { return require $path; } elseif (file_exists($path = $directory.$file.EXT)) { return require $path; } } } /** * 注册一个数组 包含类路径映射 * * @param array $mappings * @return void */ public static function map($mappings) { static::$mappings = array_merge(static::$mappings, $mappings); } /** * 注册类的别名 * * @param string $class * @param string $alias * @return void */ public static function alias($class, $alias) { static::$aliases[$alias] = $class; } /** * 注册目录 * * @param string|array $directory * @return void */ public static function directories($directory) { $directories = static::format($directory); static::$directories = array_unique(array_merge(static::$directories, $directories)); } /** * 映射命名空间和目录 * * @param array $mappings * @param string $append * @return void */ public static function namespaces($mappings, $append = '\\') { $mappings = static::format_mappings($mappings, $append); static::$namespaces = array_merge($mappings, static::$namespaces); # 合并之后: (array "命名空间", array "命名空间","路径") } /** * 注册下划线命名空间 * * @param array $mappings * @return void */ public static function underscored($mappings) { static::namespaces($mappings, '_'); # 下划线风格 } /** * 格式目录映射 * * @param array $mappings * @param string $append * @return array */ protected static function format_mappings($mappings, $append) { foreach ($mappings as $namespace => $directory) { # 清理命名空间 $namespace = trim($namespace, $append).$append; unset(static::$namespaces[$namespace]); # 去除之前的 如果存在的话 $namespaces[$namespace] = head(static::format($directory)); # 一个命名空间只能对应一个目录 } return $namespaces; } /** * 格式化一个目录数组 * * @param array $directories * @return array */ protected static function format($directories) { return array_map(function($directory) { return rtrim($directory, DS).DS;# 清理目录 }, (array) $directories); // 用map遍历目录数组 } }
改类被自动装在到spl中:
spl_autoload_register(array('Laravel\\Autoloader', 'load')); # spl_autoload_register array 命名空间,具体方法
注册好之后,就载入一些预先设置好的配置:
定义系统root
Autoloader::namespaces(array('Laravel' => path('sys'))); # 定义Laravel系统根目录映射
然后是默认使用的ORM框架
# 定义EloquentORM框架 Autoloader::map(array( 'Laravel\\Database\\Eloquent\\Relationships\\Belongs_To' => path('sys').'database/eloquent/relationships/belongs_to'.EXT, 'Laravel\\Database\\Eloquent\\Relationships\\Has_Many' => path('sys').'database/eloquent/relationships/has_many'.EXT, 'Laravel\\Database\\Eloquent\\Relationships\\Has_Many_And_Belongs_To' => path('sys').'database/eloquent/relationships/has_many_and_belongs_to'.EXT, 'Laravel\\Database\\Eloquent\\Relationships\\Has_One' => path('sys').'database/eloquent/relationships/has_one'.EXT, 'Laravel\\Database\\Eloquent\\Relationships\\Has_One_Or_Many' => path('sys').'database/eloquent/relationships/has_one_or_many'.EXT, ));
随后是Symfony的HTTP组件和Console组件
# Symfony组件加载 Autoloader::namespaces(array( 'Symfony\Component\Console' => path('sys').'vendor/Symfony/Component/Console', 'Symfony\Component\HttpFoundation' => path('sys').'vendor/Symfony/Component/HttpFoundation', ));
当然,不要忘记了application.php中的配置
'aliases' => array( 'Auth' => 'Laravel\\Auth', 'Authenticator' => 'Laravel\\Auth\\Drivers\\Driver', 'Asset' => 'Laravel\\Asset', 'Autoloader' => 'Laravel\\Autoloader', 'Blade' => 'Laravel\\Blade', 'Bundle' => 'Laravel\\Bundle', 'Cache' => 'Laravel\\Cache', 'Config' => 'Laravel\\Config', 'Controller' => 'Laravel\\Routing\\Controller', 'Cookie' => 'Laravel\\Cookie', 'Crypter' => 'Laravel\\Crypter', 'DB' => 'Laravel\\Database', 'Eloquent' => 'Laravel\\Database\\Eloquent\\Model', 'Event' => 'Laravel\\Event', 'File' => 'Laravel\\File', 'Filter' => 'Laravel\\Routing\\Filter', 'Form' => 'Laravel\\Form', 'Hash' => 'Laravel\\Hash', 'HTML' => 'Laravel\\HTML', 'Input' => 'Laravel\\Input', 'IoC' => 'Laravel\\IoC', 'Lang' => 'Laravel\\Lang', 'Log' => 'Laravel\\Log', 'Memcached' => 'Laravel\\Memcached', 'Paginator' => 'Laravel\\Paginator', 'Profiler' => 'Laravel\\Profiling\\Profiler', 'URL' => 'Laravel\\URL', 'Redirect' => 'Laravel\\Redirect', 'Redis' => 'Laravel\\Redis', 'Request' => 'Laravel\\Request', 'Response' => 'Laravel\\Response', 'Route' => 'Laravel\\Routing\\Route', 'Router' => 'Laravel\\Routing\\Router', 'Schema' => 'Laravel\\Database\\Schema', 'Section' => 'Laravel\\Section', 'Session' => 'Laravel\\Session', 'Str' => 'Laravel\\Str', 'Task' => 'Laravel\\CLI\\Tasks\\Task', 'URI' => 'Laravel\\URI', 'Validator' => 'Laravel\\Validator', 'View' => 'Laravel\\View', ),
基本上流程就出来了。
牵扯的重要的文件地址:
laravel/core.php
laravel/autoloader.php
application/config/application.php
配合Ioc,够用了。下次分析一下laravel的Ioc,不过个人感觉功能太少。使用仿spring的Ding更好
以上就是本篇文章的全部内容了,更多laravel内容请关注laravel框架入门教程。
相关文章推荐:
实时聊天室:基于Laravel+Pusher+Vue通过事件广播实现
laravel框架中TokenMismatchException的异常处理内容
相关课程推荐:
The above is the detailed content of Code analysis of Autoloader module in Laravel framework. 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

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

Laravel provides a comprehensive Auth framework for implementing user login functions, including: Defining user models (Eloquent model), creating login forms (Blade template engine), writing login controllers (inheriting Auth\LoginController), verifying login requests (Auth::attempt) Redirecting after login is successful (redirect) considering security factors: hash passwords, anti-CSRF protection, rate limiting and security headers. In addition, the Auth framework also provides functions such as resetting passwords, registering and verifying emails. For details, please refer to the Laravel documentation: https://laravel.com/doc

How does Laravel play a role in backend logic? It simplifies and enhances backend development through routing systems, EloquentORM, authentication and authorization, event and listeners, and performance optimization. 1. The routing system allows the definition of URL structure and request processing logic. 2.EloquentORM simplifies database interaction. 3. The authentication and authorization system is convenient for user management. 4. The event and listener implement loosely coupled code structure. 5. Performance optimization improves application efficiency through caching and queueing.

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

The Laravel framework has built-in methods to easily view its version number to meet the different needs of developers. This article will explore these methods, including using the Composer command line tool, accessing .env files, or obtaining version information through PHP code. These methods are essential for maintaining and managing versioning of Laravel applications.

Want to learn the Laravel framework, but suffer from no resources or economic pressure? This article provides you with free learning of Laravel, teaching you how to use resources such as online platforms, documents and community forums to lay a solid foundation for your PHP development journey from getting started to master.

In the Laravel framework version selection guide for beginners, this article dives into the version differences of Laravel, designed to assist beginners in making informed choices among many versions. We will focus on the key features of each release, compare their pros and cons, and provide useful advice to help beginners choose the most suitable version of Laravel based on their skill level and project requirements. For beginners, choosing a suitable version of Laravel is crucial because it can significantly impact their learning curve and overall development experience.

Laravel and ThinkPHP are both popular PHP frameworks and have their own advantages and disadvantages in development. This article will compare the two in depth, highlighting their architecture, features, and performance differences to help developers make informed choices based on their specific project needs.
