Composer不会为库生成自动加载 [英] Composer Not Generating Autoloads For Library
问题描述
我已经建立了两个项目,一个"init"和一个库,这是init必需的.它们都设置了PSR-0自动加载,但是库中的自动加载值未添加到init项目中的vendor/composer/autoload_namespaces.php中.
I've set up two projects, an 'init' and a library, which is required by the init. They both have PSR-0 autoloads set, but the autoload values from the library are not added to the vendor/composer/autoload_namespaces.php in the init project.
从库中采样composer.json:
Sample composer.json from the Library:
{
"name": "lxp/library",
"description": "A test library",
"autoload": {
"psr-0": {
"LXP\\Library": "src/"
}
}
}
从需要该库的项目中采样composer.json:
Sample composer.json from the project that requires that library:
{
"name": "lxp/init",
"name": "A test init",
"autoload": {
"psr-0": {
"LXP\\Init": "src/"
}
},
"repositories": [
{
"type": "composer",
"url": "http://satis.repo.redacted/"
}
],
"require": {
"lxp/library": "dev-master"
}
}
该库在文件src/LXP/Library/Something.php中包含类LXP \ Library \ Something.
The library contains the class LXP\Library\Something in the file src/LXP/Library/Something.php.
需要该库的项目在文件src/LXP/Init/Now.php中包含类LXP \ Init \ Now.
The project that requires the library contains the class LXP\Init\Now in the file src/LXP/Init/Now.php.
在"init"项目中运行composer install时,它将下载库项目并将其正确放置在供应商中,但是vendor/composer/autoload_namespaces.php不包含库的自动加载信息,仅包含当前项目的信息.
When running composer install in the 'init' project, it downloads the library project and puts it in vendor correctly, but vendor/composer/autoload_namespaces.php doesn't contain the library's autoload information, only that of the current project.
我做错了什么?如果我在库项目中运行dump-autoload,则autoload_namespaces.php文件是正确的,并且快速引导脚本确认它确实可以拾取该类.
What am I doing wrong? If I run dump-autoload in the library project then the autoload_namespaces.php file is correct, and a quick bootstrap script confirms that it does indeed pick up the class.
编辑-这是由satis生成的packages.json的问题.要解决此问题,我必须将库的composer.json中的自动加载信息添加到我提供给satis的json文件中,这似乎是不必要的重复,因此我可能做错了.是否可以在单个位置存储用于satis库的自动加载信息?例如,能否满意地读取其扫描的库中存在的composer.json文件?
EDIT - This is a problem with the satis-generated packages.json. To fix it, I had to add the autoload information from the library's composer.json into the json file I supply to satis, which seems like an unnecessary duplication and so I'm probably doing it wrong. Is there a single place that autoload information can be stored for satis libraries? For example, can satis read the composer.json files that exist in the libraries it scans?
编辑#2-Satis不会从指定为包"类型的存储库中读取composer.jsons.在事后看来,这很明显,因为包"用于没有composer.json的库,并且是一种将类似于composer的依赖项管理包装在它们周围的方式.
EDIT #2 - Satis does not read the composer.jsons from repositories specified as 'package' type. This is obvious in hindsight, because 'package' is used for libraries that do not have a composer.json, and is a way to wrap composer-like dependency management around them.
将satis.json的存储库更改为"vcs"类型,意味着已读取composer.json,并且已解析了信息(包括自动加载规范)并将其存储在packages.json中.
Changing the satis.json's repository to 'vcs' type meant that the composer.json was read, and the information (including the autoload specification) was parsed and stored in the packages.json.
@Seldaek-感谢您提出我的satis配置问题,我希望这可以澄清我所处位置的其他人的satis/作曲家行为.
@Seldaek - thank you for suggesting that my satis config was the problem, and I hope that this clarifies satis / composer behaviour for anyone else in my position.
推荐答案
我看到您可能犯了两个可能导致此错误的错误:
I see two possible mistakes you may have done that would cause this:
- 您忘了更新satis存储库,因此lxp/init的自动加载配置不是最新的
- 您正在从锁定文件运行composer安装,这意味着composer只会从composer.lock文件中读取信息,而不会将软件包元数据更新为satis中可用的最新版本.为了解决这个问题,您应该改为运行作曲家 update .
这篇关于Composer不会为库生成自动加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!