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.

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

报告相同问题?

悬赏问题

  • ¥20 全书网Java爬取数据
  • ¥15 SAP HANA SQL Script 。SUM OVER 怎么加where
  • ¥15 怎么获取红包封面的原始链接,并且获取红包封面序列号
  • ¥100 微信小程序跑脚本授权的问题
  • ¥60 为什么使用python对地震数据进行umap降维后,数据成图会出现不连续的现象
  • ¥100 房产抖音小程序苹果搜不到安卓可以付费悬赏
  • ¥15 STM32串口接收问题
  • ¥15 腾讯IOA系统怎么在文件夹里修改办公网络的连接
  • ¥15 filenotfounderror:文件是存在的,权限也给了,但还一直报错
  • ¥15 安卓qpython向ksweb服务器post文件失败