Skip to content

Instantly share code, notes, and snippets.

@Thijzer
Created December 16, 2015 14:47
Show Gist options
  • Save Thijzer/66ad816215f2b5ee9af4 to your computer and use it in GitHub Desktop.
Save Thijzer/66ad816215f2b5ee9af4 to your computer and use it in GitHub Desktop.
.files php version switcher
#!/bin/bash
if [ $# -ne 1 ]; then
        echo "Usage: sphp [phpversion]"
        exit 1
fi
currentversion="`php -r \"echo str_replace('.', '', substr(phpversion(), 0, 3));\"`"
newversion="$1"
brew list php$newversion 2> /dev/null > /dev/null
if [ $? -eq 0 ]; then
        echo "PHP version $newversion found"
        echo "Unlinking PHP version $currentversion... "
        brew unlink php$currentversion 2> /dev/null > /dev/null
        echo "Linking new binaries..."
        brew link --overwrite php$newversion
        echo "Linking new modphp addon..."
        sudo ln -sf `brew list php$newversion | grep libphp` /usr/local/lib/libphp5.so
        echo "Updating version file..."
        pgrep -f /usr/sbin/httpd 2> /dev/null > /dev/null
        if [ $? -eq 0 ]; then
                echo "Restarting system Apache..."
                sudo pkill -9 -f /usr/sbin/httpd
                sudo /usr/sbin/apachectl -k restart > /dev/null 2>&1
        fi
        pgrep -f /usr/local/Cellar/*/httpd 2> /dev/null > /dev/null
        if [ $? -eq 0 ]; then
                echo "Restarting homebrew Apache..."
                sudo pkill -9 -f /usr/local/Cellar/*/httpd
                sudo /usr/local/bin/apachectl -k restart > /dev/null 2>&1
        fi
        echo "Done."
else
        echo "PHP version $newversion was not found."
        exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment