-
-
Save schoenkaft/dc89bbb673c06ff6b63b09642be87e58 to your computer and use it in GitHub Desktop.
PHP switcher for homebrew/core
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Check if command was ran as root. | |
if [[ $(id -u) -eq 0 ]]; then | |
echo "The command \"sphp\" should not be executed as root or via sudo directly." | |
echo "When a service requires root access, you will be prompted for a password as needed." | |
exit 1 | |
fi | |
# Usage | |
if [ $# -ne 1 ]; then | |
echo "Usage: sphp [phpversion]" | |
echo "Versions installed:" | |
brew list | grep '^php@[0-9].[0-9]$' | grep -o -E '[0-9].[0-9]' | while read -r line ; do | |
echo " - phpversion: $line" | |
done | |
brew list | grep '^php$' | while read -r line ; do | |
echo " - phpversion: latest" | |
done | |
exit 1 | |
fi | |
currentversion="`php -r \"error_reporting(0); echo substr(phpversion(), 0, 3);\"`" | |
newversion="$1" | |
# Add dot if not provided | |
if [[ $newversion != *"."* ]]; then | |
newversion="${newversion:0:1}.${newversion:1:2}" | |
fi | |
majorOld=${currentversion:0:1} | |
majorNew=${newversion:0:1} | |
brew list php@$newversion 2> /dev/null > /dev/null | |
if [ $? -eq 0 ]; then | |
echo -e "New version: $newversion" | |
echo -e "Old version: $currentversion \n" | |
# Check if new version is already the current version. | |
# if [ "${newversion}" == "${currentversion}" ]; then | |
# echo -n "PHP version ${newversion} is already being used. Continue by reloading? (y/n) " | |
# while true; do | |
# read -n 1 yn | |
# case $yn in | |
# [Yy]* ) echo && break;; | |
# [Nn]* ) echo && exit 1;; | |
# esac | |
# done | |
# fi | |
echo "Unlinking old binaries..." | |
brew unlink php@$currentversion 2> /dev/null > /dev/null | |
echo "Linking new binaries..." | |
brew link php@$newversion --force --overwrite | |
echo "Stopping old FPM process" | |
brew services stop php@$currentversion | |
echo "Starting new FPM process" | |
brew services start php@$newversion | |
echo "Linking new modphp addon..." | |
ln -sf `brew list php@$newversion | grep libphp` /usr/local/lib/libphp${majorNew}.so | |
echo /usr/local/lib/libphp${majorNew}.so | |
echo "Fixing LoadModule..." | |
apacheConf=`httpd -V | grep -i server_config_file | cut -d '"' -f 2` | |
sed -i -e "/LoadModule php${majorOld}_module/s/^#*/#/" $apacheConf | |
if grep "LoadModule php${majorNew}_module .*php@${newversion}" $apacheConf > /dev/null | |
then | |
sed -i -e "/LoadModule php${majorNew}_module .*php@${newversion}/s/^#//" $apacheConf | |
else | |
sed -i -e "/LoadModule php${majorNew}_module/s/^#//" $apacheConf | |
fi | |
echo "Updating version file..." | |
pgrep -f /usr/local/"Cellar|opt"/*/httpd 2> /dev/null > /dev/null | |
if [ $? -eq 0 ]; then | |
echo -e "Restarting homebrew Apache...\n" | |
sudo /usr/local/bin/apachectl -k restart > /dev/null 2>&1 | |
else | |
echo -e "\nHomebrew Apache not found, not restarting Apache.\n" | |
echo -e "Try \`brew install httpd\` first.\n" | |
fi | |
# Show PHP CLI version for verification. | |
echo "Now running..." && php -v | sed '1q' && echo "💪 💪 💪 " | |
else | |
echo "PHP version $newversion was not found." | |
echo "Try \`brew install php@${newversion}\` first." | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Only works with
homebrew/core
PHP versions and Homebrew Apache (httpd
). Also (re)starts and switches FPM processes inbrew services list
.To have a completely passwordless switch, add this to your
sudo visudo
: