Skip to content

Instantly share code, notes, and snippets.

@Synchro
Forked from rozsival/sphp
Last active Jan 12, 2022
Embed
What would you like to do?
Easy Brew PHP-FPM switch
#!/usr/bin/env bash
latest="8.1"
versions=("7.4" "8.0" "$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