Composer 不为库生成自动加载

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

问题描述

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.

Sample composer.json from the Library:

{
    "name": "lxp/library",
    "description": "A test library",
    "autoload": {
        "psr-0": {
            "LXP\Library": "src/"
        }
    }        
}

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"
    }
}

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

The project that requires the library contains the class LXPInitNow in the file src/LXP/Init/Now.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.

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.

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?

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.

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 - 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:

  • You forgot to update your satis repo so the autoload config for lxp/init is not up to date in there
  • You are running composer install from a lock file, and that means composer just reads the info from the composer.lock file and does not update package metadata to the latest version available in satis. To solve this you should run composer update instead.

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)