0

I am looking for a way to simplify the different php versions and also paths of Composer. I found an approach in this answer that looks very appropriate for me.

I have tried to implement the following for understanding

function composer ()
{
    if [ "$PWD" == "/home/vhosts/domainName/httpdocs" ]; then
       /usr/bin/php7.3 lib/composer
    elif [ "$PWD" == "/home/vhosts/domainName2/httpdocs" ]; then
        /usr/bin/php5.6 composer.phar
    else
       composer
    fi;
}

This works fine, but I am now looking for a way to transfer the stdin, so that a "composer install" is possible. I hope the question is understandable 😅

1 Answer 1

1

Pass the arguments of the function on to the program you call from the function:

composer () {
    if [ "$PWD" = "/home/vhosts/domainName/httpdocs" ]; then
       /usr/bin/php7.3 lib/composer "$@"
    elif [ "$PWD" = "/home/vhosts/domainName2/httpdocs" ]; then
        /usr/bin/php5.6 composer.phar "$@"
    else
       command composer "$@"
    fi
}

The "$@" will expand to the arguments of the function, individually quoted (the double quotes are essential).

Also note that when calling just composer, you will have to use command composer as you would otherwise call the function recursively.

I have also fixed some minor syntax things to make the function portable.

With this function, doing

composer install

in /home/vhosts/domainName/httpdocs would result in

/usr/bin/php7.3 lib/composer install

Another variant of the function:

composer () {
    case $PWD in
        "/home/vhosts/domainName/httpdocs")
            /usr/bin/php7.3 lib/composer "$@"
            ;;
        "/home/vhosts/domainName2/httpdocs")
            /usr/bin/php5.6 composer.phar "$@"
            ;;
        *)
            command composer "$@"
    esac
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .