Skip to content

Instantly share code, notes, and snippets.

@rozsival
Created March 3, 2021 16:16
Show Gist options
  • Save rozsival/10289d1e2006c68009ace0478306ecd2 to your computer and use it in GitHub Desktop.
Save rozsival/10289d1e2006c68009ace0478306ecd2 to your computer and use it in GitHub Desktop.
Easy Brew PHP-FPM switch
#!/usr/bin/env bash
latest="8.0"
versions=("7.0" "7.1" "7.2" "7.4" "$latest")
valid=$(printf "|%s" "${versions[@]}")
switch="$1"
ERROR=$(tput setaf 1)
SUCCESS=$(tput setaf 2)
if [ -z "$switch" ]
then
echo "${ERROR}✖ PHP version required (${valid:1})"
exit 1
fi
if [[ ! " ${versions[@]} " =~ " ${switch} " ]]
then
printf "${ERROR}✖ Invalid PHP version (valid: ${valid:1})"
exit 1
fi
printf "⇆ Switching PHP to version $switch\n\n"
php="php@$switch"
if [ "$switch" == "$latest" ] ; then php="php" ; fi
for v in ${versions[*]}
do
service="php@$v"
pattern="$service"
if [ "$v" == "$latest" ] ; then pattern="php[^@]" ; fi
status=$(brew services | grep "$pattern" | grep "started")
if [ ! -z "$status" ] ; then brew services stop "$service" ; fi
brew unlink "$service"
done
brew link --overwrite --force "$php"
brew services start "$php"
printf "\n${SUCCESS}✔ PHP switched to version $switch"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment