0
votes

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

1 Answers

1
votes

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.