PHP管理依賴(dependency)關(guān)系工具 Composer的自動(dòng)加載(autoload)
舉例來說,假設(shè)我們的項(xiàng)目想要使用 monolog 這個(gè)日志工具,就需要在composer.json里告訴composer我們需要它:
{
"require": {
"monolog/monolog": "1.*"
}
}
之后執(zhí)行:
php composer.phar install
好,現(xiàn)在安裝完了,該怎么使用呢?Composer自動(dòng)生成了一個(gè)autoload文件,你只需要引用它
require '/path/to/vendor/autoload.php';
然后就可以非常方便的去使用第三方的類庫了,是不是感覺很棒??!對(duì)于我們需要的monolog,就可以這樣用了:
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
// create a log channel
$log = new Logger('name');
$log->pushHandler(new StreamHandler('/path/to/log/log_name.log', Logger::WARNING));
// add records to the log
$log->addWarning('Foo');
$log->addError('Bar');
在這個(gè)過程中,Composer做了什么呢?它生成了一個(gè)autoloader,再根據(jù)各個(gè)包自己的autoload配置,從而幫我們進(jìn)行自動(dòng)加載的工作。(如果對(duì)autoload這部分內(nèi)容不太了解,可以看我之前的 一篇文章
)接下來讓我們看看Composer是怎么做的吧。
對(duì)于第三方包的自動(dòng)加載,Composer提供了四種方式的支持,分別是 PSR-0和PSR-4的自動(dòng)加載(我的一篇文章也有介紹過它們),生成class-map,和直接包含files的方式。
PSR-4是composer推薦使用的一種方式,因?yàn)樗资褂貌⒛軒砀啙嵉哪夸浗Y(jié)構(gòu)。在composer.json里是這樣進(jìn)行配置的:
{
"autoload": {
"psr-4": {
"Foo\\": "src/",
}
}
}
key和value就定義出了namespace以及到相應(yīng)path的映射。按照PSR-4的規(guī)則,當(dāng)試圖自動(dòng)加載 "Foo\\Bar\\Baz" 這個(gè)class時(shí),會(huì)去尋找 "src/Bar/Baz.php" 這個(gè)文件,如果它存在則進(jìn)行加載。注意, "Foo\\"
并沒有出現(xiàn)在文件路徑中,這是與PSR-0不同的一點(diǎn),如果PSR-0有此配置,那么會(huì)去尋找
"src/Foo/Bar/Baz.php"
這個(gè)文件。
另外注意PSR-4和PSR-0的配置里,"Foo\\"結(jié)尾的命名空間分隔符必須加上并且進(jìn)行轉(zhuǎn)義,以防出現(xiàn)"Foo"匹配到了"FooBar"這樣的意外發(fā)生。
在composer安裝或更新完之后,psr-4的配置換被轉(zhuǎn)換成namespace為key,dir path為value的Map的形式,并寫入生成的 vendor/composer/autoload_psr4.php 文件之中。
{
"autoload": {
"psr-0": {
"Foo\\": "src/",
}
}
}
最終這個(gè)配置也以Map的形式寫入生成的
vendor/composer/autoload_namespaces.php
文件之中。
Class-map方式,則是通過配置指定的目錄或文件,然后在Composer安裝或更新時(shí),它會(huì)掃描指定目錄下以.php或.inc結(jié)尾的文件中的class,生成class到指定file path的映射,并加入新生成的 vendor/composer/autoload_classmap.php 文件中,。
{
"autoload": {
"classmap": ["src/", "lib/", "Something.php"]
}
}
例如src/下有一個(gè)BaseController類,那么在autoload_classmap.php文件中,就會(huì)生成這樣的配置:
'BaseController' => $baseDir . '/src/BaseController.php'
Files方式,就是手動(dòng)指定供直接加載的文件。比如說我們有一系列全局的helper functions,可以放到一個(gè)helper文件里然后直接進(jìn)行加載
{
"autoload": {
"files": ["src/MyLibrary/functions.php"]
}
}
它會(huì)生成一個(gè)array,包含這些配置中指定的files,再寫入新生成的
vendor/composer/autoload_files.php
文件中,以供autoloader直接進(jìn)行加載。
下面來看看composer autoload的代碼吧
<?php
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit73612b48e6c3d0de8d56e03dece61d11
{
private static $loader;
public static function loadClassLoader($class)
{
if ('Composer\Autoload\ClassLoader' === $class) {
require __DIR__ . '/ClassLoader.php';
}
}
public static function getLoader()
{
if (null !== self::$loader) {
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit73612b48e6c3d0de8d56e03dece61d11', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader();
spl_autoload_unregister(array('ComposerAutoloaderInit73612b48e6c3d0de8d56e03dece61d11', 'loadClassLoader'));
$vendorDir = dirname(__DIR__); //verdor第三方類庫提供者目錄
$baseDir = dirname($vendorDir); //整個(gè)應(yīng)用的目錄
$includePaths = require __DIR__ . '/include_paths.php';
array_push($includePaths, get_include_path());
set_include_path(join(PATH_SEPARATOR, $includePaths));
$map = require __DIR__ . '/autoload_namespaces.php';
foreach ($map as $namespace => $path) {
$loader->set($namespace, $path);
}
$map = require __DIR__ . '/autoload_psr4.php';
foreach ($map as $namespace => $path) {
$loader->setPsr4($namespace, $path);
}
$classMap = require __DIR__ . '/autoload_classmap.php';
if ($classMap) {
$loader->addClassMap($classMap);
}
$loader->register(true);
$includeFiles = require __DIR__ . '/autoload_files.php';
foreach ($includeFiles as $file) {
composerRequire73612b48e6c3d0de8d56e03dece61d11($file);
}
return $loader;
}
}
function composerRequire73612b48e6c3d0de8d56e03dece61d11($file)
{
require $file;
}
首先初始化ClassLoader類,然后依次用上面提到的4種加載方式來注冊(cè)/直接加載,ClassLoader的一些核心代碼如下:
/**
* @param array $classMap Class to filename map
*/
public function addClassMap(array $classMap)
{
if ($this->classMap) {
$this->classMap = array_merge($this->classMap, $classMap);
} else {
$this->classMap = $classMap;
}
}
/**
* Registers a set of PSR-0 directories for a given prefix,
* replacing any others previously set for this prefix.
*
* @param string $prefix The prefix
* @param array|string $paths The PSR-0 base directories
*/
public function set($prefix, $paths)
{
if (!$prefix) {
$this->fallbackDirsPsr0 = (array) $paths;
} else {
$this->prefixesPsr0[$prefix[0]][$prefix] = (array) $paths;
}
}
/**
* Registers a set of PSR-4 directories for a given namespace,
* replacing any others previously set for this namespace.
*
* @param string $prefix The prefix/namespace, with trailing '\\'
* @param array|string $paths The PSR-4 base directories
*
* @throws \InvalidArgumentException
*/
public function setPsr4($prefix, $paths)
{
if (!$prefix) {
$this->fallbackDirsPsr4 = (array) $paths;
} else {
$length = strlen($prefix);
if ('\\' !== $prefix[$length - 1]) {
throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator.");
}
$this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
$this->prefixDirsPsr4[$prefix] = (array) $paths;
}
}
/**
* Registers this instance as an autoloader.
*
* @param bool $prepend Whether to prepend the autoloader or not
*/
public function register($prepend = false)
{
spl_autoload_register(array($this, 'loadClass'), true, $prepend);
}
/**
* Loads the given class or interface.
*
* @param string $class The name of the class
* @return bool|null True if loaded, null otherwise
*/
public function loadClass($class)
{
if ($file = $this->findFile($class)) {
includeFile($file);
return true;
}
}
/**
* Finds the path to the file where the class is defined.
*
* @param string $class The name of the class
*
* @return string|false The path if found, false otherwise
*/
public function findFile($class)
{
//這是PHP5.3.0 - 5.3.2的一個(gè)bug 詳見https://bugs.php.net/50731
if ('\\' == $class[0]) {
$class = substr($class, 1);
}
// class map 方式的查找
if (isset($this->classMap[$class])) {
return $this->classMap[$class];
}
//psr-0/4方式的查找
$file = $this->findFileWithExtension($class, '.php');
// Search for Hack files if we are running on HHVM
if ($file === null && defined('HHVM_VERSION')) {
$file = $this->findFileWithExtension($class, '.hh');
}
if ($file === null) {
// Remember that this class does not exist.
return $this->classMap[$class] = false;
}
return $file;
}
private function findFileWithExtension($class, $ext)
{
// PSR-4 lookup
$logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext;
$first = $class[0];
if (isset($this->prefixLengthsPsr4[$first])) {
foreach ($this->prefixLengthsPsr4[$first] as $prefix => $length) {
if (0 === strpos($class, $prefix)) {
foreach ($this->prefixDirsPsr4[$prefix] as $dir) {
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $length))) {
return $file;
}
}
}
}
}
// PSR-4 fallback dirs
foreach ($this->fallbackDirsPsr4 as $dir) {
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) {
return $file;
}
}
// PSR-0 lookup
if (false !== $pos = strrpos($class, '\\')) {
// namespaced class name
$logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1)
. strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR);
} else {
// PEAR-like class name
$logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . $ext;
}
if (isset($this->prefixesPsr0[$first])) {
foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) {
if (0 === strpos($class, $prefix)) {
foreach ($dirs as $dir) {
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
return $file;
}
}
}
}
}
// PSR-0 fallback dirs
foreach ($this->fallbackDirsPsr0 as $dir) {
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
return $file;
}
}
// PSR-0 include paths.
if ($this->useIncludePath && $file = stream_resolve_include_path($logicalPathPsr0)) {
return $file;
}
}
/**
* Scope isolated include.
*
* Prevents access to $this/self from included files.
*/
function includeFile($file)
{
include $file;
}
- Laravel框架中composer自動(dòng)加載的實(shí)現(xiàn)分析
- Laravel 解決composer相關(guān)操作提示php相關(guān)異常的問題
- laravel 實(shí)現(xiàn)向公共模板中傳值 (view composer)
- 淺談laravel 5.6 安裝 windows上使用composer的安裝過程
- 使用composer 安裝 laravel框架的方法圖文詳解
- 一次因composer錯(cuò)誤使用引發(fā)的問題與解決
- tp5框架使用composer實(shí)現(xiàn)日志記錄功能示例
- windows環(huán)境下使用Composer安裝ThinkPHP5
- PHP創(chuàng)建自己的Composer包方法
- 分析Composer實(shí)現(xiàn)自動(dòng)加載原理
相關(guān)文章
PHP中使用xmlreader讀取xml數(shù)據(jù)示例
這篇文章主要介紹了PHP中使用xmlreader讀取xml數(shù)據(jù)示例,本文示例相對(duì)簡單,只包含了一個(gè)讀取功能,需要的朋友可以參考下2014-12-12
PHP 如何獲取二維數(shù)組中某個(gè)key的集合
PHP 獲取二維數(shù)組中某個(gè)key的集合2014-06-06
php的mail函數(shù)發(fā)送UTF-8編碼中文郵件時(shí)標(biāo)題亂碼的解決辦法
這篇文章主要介紹了php的mail函數(shù)發(fā)送UTF-8編碼中文郵件時(shí)標(biāo)題亂碼的解決辦法,需要的朋友可以參考下2015-10-10
將FCKeditor導(dǎo)入PHP+SMARTY的實(shí)現(xiàn)方法
這篇文章主要介紹了將FCKeditor導(dǎo)入PHP+SMARTY的實(shí)現(xiàn)方法,涉及整合FCKeditor與SMARTY的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-01-01
php中替換字符串函數(shù)strtr()和str_repalce()的用法與區(qū)別
在php中替換函數(shù)主要有strtr(),str_repalce()這兩個(gè)函數(shù),下面這篇文中主要給大家介紹下這兩者之間的區(qū)別和用法,文中通過示例代碼介紹的很詳細(xì),有需要的朋友們可以參考借鑒,下面跟著小編一起來學(xué)習(xí)學(xué)習(xí)吧。2016-11-11

