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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16273746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:49:35  来源:igfitidea点击:

Composer packages, autoloading non-class based files

phpnamespacescomposer-php

提问by Mark Fox

When I was digging into the source of a Composer package on githubI noticed that there were php files that matched namespace namesbut 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.

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

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

例如,在crunch/regular-expression包中有一个名为 的命名空间 Crunch\RegularExpression

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

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.phpand saw that _RegularExpression.php(amongst others) was being required explicitly:

最初我认为这些带下划线的文件是我错过的 PSR-0 的一个功能,但后来我查看了生成的 Composerautoload_real.php并看到_RegularExpression.php(除其他外)明确要求:

…
$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;
…

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 的此功能的任何有意义的文档。它是导出基于非类的命名空间依赖项(如函数和常量)的好“标准”吗?

Update

更新

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:

我的问题被证明是一个轻微的用词不当。选择的答案让我发现可以显式声明非基于类的资产以加载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.

文件上的下划线是一种约定,用于将它们与类定义分开,在自动加载中没有特殊用途。

采纳答案by Seldaek

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 不会以任何特殊方式处理这些文件。在这种情况下,包作者使用它作为某种约定来存储看起来的功能。

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.json 中被定义为“文件”自动加载,而不是因为文件名上的一些黑魔法。