drx3157 2016-08-11 09:56
浏览 64
已采纳

Composer不会通过存储库>类型>路径自动加载文件

The Problem

I have two PSR-4 composer projects and I wish to classes from one in the other, the file structure is as follows:

projectfoo
-public
--index.php
-src
--CompanyName
---Foo
----Foo.php
-composer.json
projectbar
-src
--CompanyName
---Bar
----Bar.php
-composer.json

The composer.json files are defined for projectfoo as (note the repositories > type > path dependency):

{
    "name": "companyname/foo",
    "require": {
        "companyname/bar": "*"
    },
    "repositories": [
        {
            "type": "path",
            "url": "../projectbar"
        }
    ],
    "autoload": {
        "psr-4": {
            "CompanyName\\": "src/CompanyName"
        }
    }
}

And projectbar as:

{
    "name": "companyname/bar",
    "version": "1.0.0",
    "autoload": {
        "psr-4": {
            "CompanyName\\": "src/CompanyName"
        }
    }
}

Running composer update correctly produces the vendor folder containing the companyname/foo folder.

In index.php in projectfoo I have:

require_once '../vendor/autoload.php';

$bar = new \CompanyName\Bar\Bar();

However, when run \CompanyName\Bar\Bar is undefined.


The question

Why is Bar not being included in the autoload.php file?


What I've tried

  • Refreshing the autoload file using: composer install composer update composer dump-autoload
  • Changing the vendor name CompanyName to something different.
  • Looking at the installed.json file in the vendor\composer folder the bar project is listed as:

#

{
    "name": "companyname/bar",
    "version": "1.0.0",
    "version_normalized": "1.0.0.0",
    "dist": {
        "type": "path",
        "url": "../companyname/bar",
        "reference": "f35ed0ad82c8280db9b603712dd256074f99e196",
        "shasum": null
    },
    "type": "library",
    "installation-source": "dist",
    "autoload": {
        "psr-4": {
            "CompanyName\\": "src/CompanyName",
        }
    }
}
  • Strangely, when bootstrapping the autoload.php file to phpunit the files autoload correctly and are available in my test classes.
  • 写回答

1条回答 默认 最新

  • douju2053 2016-08-26 15:48
    关注

    The issue revolves around the way composer links "path" projects using symlinks.


    In short

    Because I was developing on a windows system and executing the code on an Ubuntu vagrant box the symlinks set up on windows, when running composer install were resolving to broken paths inside the box. This also explains why tests run on windows managed to locate Bar but not tests run in the virtual box.


    Solutions

    To solve this I initially forced composer to mirror the project rather than symlink it, however, this still caused issues with symlinks in the vendor folder.

    I have now started hosting my libraries in seperate repositories and importing them using the "type": "vcs" tag which works well. Composer also handles updating package when new commits are made which doesn't happen when using the "type": "path" tag with mirroring.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝