Composer 没有为库生成自动加载 [英] Composer Not Generating Autoloads For Library

查看:33
本文介绍了Composer 没有为库生成自动加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经建立了两个项目,一个 '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 中包含类 LXPLibrarySomething.

The library contains the class LXPLibrarySomething in the file src/LXP/Library/Something.php.

需要库的项目在文件 src/LXP/Init/Now.php 中包含类 LXPInitNow.

The project that requires the library contains the class LXPInitNow in the file src/LXP/Init/Now.php.

在 'init' 项目中运行 composer install 时,它会下载库项目并将其正确放入 vendor 中,但 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 库存储自动加载信息?例如,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/composer 行为.

@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 install,这意味着 composer 只是从 composer.lock 文件中读取信息,而不会将包元数据更新到 satis 中可用的最新版本.要解决此问题,您应该运行 composer update.

这篇关于Composer 没有为库生成自动加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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