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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。