Skip to content

Instantly share code, notes, and snippets.

@jasny
Created April 2, 2017 21:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasny/087ba0c1648c2b1fc6718cedf9838763 to your computer and use it in GitHub Desktop.
Save jasny/087ba0c1648c2b1fc6718cedf9838763 to your computer and use it in GitHub Desktop.
phpbrew-php automatically runs the correct PHP version for a project
#!/bin/bash
# phpbrew-php will determine the PHP version of the project from the NetBeans project or composer.json.
command_exists () {
type "$1" &> /dev/null ;
}
vergte() {
[ "$1" = "`echo -e "$1\n$2" | sort -V | tail -n1`" ]
}
if ! command_exists jq; then
echo "Please install jq:"
echo " sudo apt-get install jq"
echo
echo "jq is a lightweight and flexible command-line JSON processor: https://stedolan.github.io/jq/"
exit 1
fi
if [ -f "nbproject/project.properties" ] && (grep -q "php.version=" "nbproject/project.properties"); then
VERSION=$(grep "php.version=" "nbproject/project.properties" | sed -r 's/php.version=PHP_([0-9])([0-9]+)/\1.\2/')
elif [ -f "composer.json" ] ; then
REQUIRE=$(cat "composer.json" | jq ".require.php")
VERSION=$(echo -en $REQUIRE | sed -r 's/.*(>?=|~|\^)([0-9]+\.[0-9*]+(\.[0-9*]+)?).*/\2/' | sed 's/*/0/')
fi
[ -z "$VERSION" ] && VERSION=$PHPBREW_DEFAULT
for DIR in $(ls "$HOME/.phpbrew/php/" -1); do
if vergte "$DIR" "php-$VERSION"; then
PHP="$HOME/.phpbrew/php/$DIR/bin/php"
break;
fi
done
if [ -z "$PHP" ]; then
echo "No phpbrew PHP version found >= $VERSION"
exit 1
fi
exec "$PHP" "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment