Skip to content

Instantly share code, notes, and snippets.

@Synchro
Forked from rozsival/sphp
Last active June 8, 2023 13:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Synchro/58dc96a30527136478cde2dfcc13557b to your computer and use it in GitHub Desktop.
Save Synchro/58dc96a30527136478cde2dfcc13557b to your computer and use it in GitHub Desktop.
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