maxime
maxime

Reputation: 2315

Use Composer $COMPOSER_VENDOR_DIR in autoload filepath

I am using Symfony for a Dockerized app. I need to move my composer vendors directory, so I did set the environnement variable $COMPOSER_VENDOR_DIR to /root/app-vendors.

The vendors are correctly located to this directory, but it causes troubles with the Symfony VarDump component. By default in Symfony 2, it is loaded from composer like this:

"autoload-dev": {
        "files": [
          "vendor/symfony/symfony/src/Symfony/Component/VarDumper/Resources/functions/dump.php"
        ]
    }

In my case, this path becomes wrong. I'd like to do something like this:

"autoload-dev": {
        "files": [
            "{$COMPOSER_VENDOR_DIR}/symfony/symfony/src/Symfony/Component/VarDumper/Resources/functions/dump.php"
        ]
    },

But I don't see anything in the composer documentation about using env variables in a composer.json filepath.

I want to move the vendors in dev mode, but I want them to be in the default location if $COMPOSER_VENDOR_DIR is not set ; so I cannot put absolute filepath in autoload-dev section like this:

"autoload-dev": {
        "files": [
            "/root/app-vendors/symfony/symfony/src/Symfony/Component/VarDumper/Resources/functions/dump.php"
        ]
    },

Upvotes: 1

Views: 996

Answers (1)

maxime
maxime

Reputation: 2315

A simple solution after a few days of step back:

Create this file on app/dump.php

<?php

$vendorsDir = getenv('COMPOSER_VENDOR_DIR')
    ?: __DIR__.'/../vendor';

require  "$vendorsDir/symfony/symfony/src/Symfony/Component/VarDumper/Resources/functions/dump.php";

Then autoload this file instead of the Symfony one

"autoload-dev": {
        "files": [
            "app/dump.php"
        ]
    },

Note:

Upvotes: 1

Related Questions