Skip to content

Instantly share code, notes, and snippets.

@x86demon
Last active January 30, 2023 11:55
Show Gist options
  • Save x86demon/37999cfd46e9d4079293462f1704a0e7 to your computer and use it in GitHub Desktop.
Save x86demon/37999cfd46e9d4079293462f1704a0e7 to your computer and use it in GitHub Desktop.
Automatic PHP version selector based on composer.json for Gentoo
#!/bin/sh
if [[ -f .php-version ]]
then
PHP_VERSION=$(<.php-version)
else
PHP_VERSION=''
if [[ -f composer.json ]]
then
PHP_VERSION=$(cat composer.json | egrep -o '"php"\s*:\s*"[^0-9]*[7-8]\.[0-9]+' | egrep -o '[5-8]\.[0-9]+')
else
PHP_VERSION='8.2'
CURRENT_GIT_BRANCH=$(git branch --show-current 2> /dev/null)
if [[ $CURRENT_GIT_BRANCH =~ "5.0" || $CURRENT_GIT_BRANCH =~ "_50" ]]
then
PHP_VERSION='8.1'
fi
if [[ $CURRENT_GIT_BRANCH =~ "4.2" || $CURRENT_GIT_BRANCH =~ "_42" || $CURRENT_GIT_BRANCH =~ "4.1" || $CURRENT_GIT_BRANCH =~ "_41" || $CURRENT_GIT_BRANCH =~ "3.1" || $CURRENT_GIT_BRANCH =~ "_31" || $CURRENT_GIT_BRANCH =~ "2.6" ]]
then
PHP_VERSION='7.4'
fi
fi
fi
PHP="/usr/bin/php${PHP_VERSION}"
${PHP} $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment