Composer软件包,自动加载非基于类的文件 [英] Composer packages, autoloading non-class based files

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

问题描述

当我在github上的上查找Composer软件包的源代码时,我注意到有 php文件与之匹配命名空间名称,但前面带有下划线.感到困惑的是,我(通过Composer)拉下了包,并注意到Composer生成的类加载器显式生成了这些带下划线的文件,而不是我期望的那样自动加载.

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包中,有一个名为 Crunch\RegularExpression:

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

-- 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软件包,自动加载非基于类的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆