沃梦达 / 编程问答 / php问题 / 正文

Composer 包,自动加载非基于类的文件

Composer packages, autoloading non-class based files(Composer 包,自动加载非基于类的文件)

本文介绍了Composer 包,自动加载非基于类的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在 在 github 上挖掘 Composer 包的源代码时,我注意到有 php 匹配命名空间名称的文件,但前面带有下划线.困惑的我将包拉下来(通过 Composer),并注意到 Composer 生成的类加载器 require 明确地对这些带下划线的文件进行了处理,而不是像我预期的那样自动加载.

When I was digging into the source of a Composer package on github I noticed that there were php files that matched namespace names but were preceded with an underscore. Puzzled I pulled the package down (via Composer) and noticed that the class loader that Composer generates required these underscored files explicitly, not autoloading as I'd expected.

例如,在 crunch/regular-expression 包中有一个名为紧缩正则表达式:

For instance, in the crunch/regular-expression package there is a namespace called CrunchRegularExpression:

-- src
---- Crunch
------- RegularExpression       <-- folder containing classes
------- _RegularExpression.php  <-- file namespace to Crunch/RegularExpression
                                    containing functions and constants 
                                    (instead of a class)

最初我认为这些带下划线的文件是我错过的 PSR-0 的一个特性,但后来我查看了 Composer 生成的 autoload_real.php 并看到 _RegularExpression.php(其中包括)被明确要求:

Initially I thought these underscored files were a feature of PSR-0 that I had missed, but then I looked at the Composer generated autoload_real.php and saw that _RegularExpression.php (amongst others) was being required explicitly:

…
$loader->register(true);

require $baseDir . '/src/Crunch/_RegularExpression.php';
require $baseDir . '/src/Crunch/RegularExpression/_Modifier.php';
require $baseDir . '/src/Crunch/RegularExpression/Pattern/_Modifier.php';
require $baseDir . '/src/Crunch/RegularExpression/Pattern/_Assertion.php';

return $loader;
…

尚未找到任何有关 Composer 的此功能的有意义的文档.是不是一个好的标准"?用于导出基于非类的命名空间依赖项,例如函数和常量?

Haven't been able to find any meaningful documentation about this feature of Composer. Is it a good "standard" for exporting non-class based namespaced dependencies, like functions and constants?

结果我的问题有点用词不当.选择的答案让我发现可以显式声明非基于类的资产以在 composer.json 中加载:

My question turned out to be a slight misnomer. The selected answer lead me to discover that non-class based assets can be explicitly declared for loading in composer.json:

"autoload": {
    "psr-0": { "Crunch\RegularExpression": "src" },
    "files": [
        "src/Crunch/_RegularExpression.php",
        "src/Crunch/RegularExpression/_Modifier.php",
        "src/Crunch/RegularExpression/Pattern/_Modifier.php",
        "src/Crunch/RegularExpression/Pattern/_Assertion.php"
    ]
}

文件上的下划线是用于从类定义中划定它们的约定,在自动加载时没有特殊用途.

The underscores on the files were a convention used to delineate them from class definitions and have no special purposes in autoloading.

推荐答案

Composer 不会以任何特殊方式处理这些文件.在这种情况下,包作者使用它作为某种约定来存储它看起来的函数.

Composer doesn't treat those files in any special way. The package author in this case used this as some sort of convention to stores functions it seems.

Composer 需要这些文件,因为它们在composer.json,不是因为文件名上的一些黑魔法.

The files are required by Composer because they're defined as "files" autoload in the composer.json, not because of some black magic on filenames.

这篇关于Composer 包,自动加载非基于类的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Composer 包,自动加载非基于类的文件