Skip to content

Instantly share code, notes, and snippets.

@kjbrum
Last active March 21, 2018 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kjbrum/4a974ce54d36028366cdcbc797bcec57 to your computer and use it in GitHub Desktop.
Save kjbrum/4a974ce54d36028366cdcbc797bcec57 to your computer and use it in GitHub Desktop.
Easily switch between PHP versions with Homebrew and Laravel Valet.
#!/usr/bin/env bash
# PHP Switcher
# Easily switch between PHP versions with Homebrew and Laravel Valet.
# Copyright (C) Kyle Brumm <http://kylebrumm.com>
#
# Credit/Resources:
# https://gist.github.com/bgarrant/b9a2f7fb8ff06c9a45086359ded7a95e
# https://raw.githubusercontent.com/conradkleinespel/sphp-osx/master/sphp
# -------------------------------------------------------
# VARIABLES
# -------------------------------------------------------
# Colors
NC='\033[0m' # No Color
RED='\033[0;31m'
BLACK="\033[0;30m"
RED="\033[0;31m"
GREEN="\033[0;32m"
YELLOW="\033[0;33m"
BLUE="\033[0;34m"
PURPLE="\033[0;35m"
CYAN="\033[0;36m"
WHITE="\033[0;37m"
# Versions
CURRENT_VERSION="$(php -r "error_reporting(0); echo str_replace('.', '', substr(phpversion(), 0, 3));")"
NEW_VERSION="$1"
INSTALLED_VERSIONS=$(brew list | grep '^php[0-9]\{2,\}$' | grep -o -E '[0-9]+' | while read -r line ; do
echo " $line";
done;)
# -------------------------------------------------------
# HELPER FUNCTIONS
# -------------------------------------------------------
# Display the help text
help() {
cat <<EOF
PHP Switcher
Easily switch between PHP versions with Homebrew and Laravel Valet.
Installed Versions:
${INSTALLED_VERSIONS}
Usage:
php-switcher <version>
Example:
php-switcher 71
EOF
exit 0
}
# Throw an error
error() {
printf "${RED}Error:${WHITE} ${1}${NC}\n\n"
exit 1
}
# Throw a notice
notice() {
printf "${YELLOW}Notice:${WHITE} ${1}${NC}\n\n"
exit 0
}
# -------------------------------------------------------
# MAIN FUNCTIONALITY
# -------------------------------------------------------
# Check if help info should be displayed
if [ "$1" = "-h" ] || [ "$1" = "--help" ] || [ -z "$1" ]; then
help
fi
# Check if command was ran as root
if [[ $(id -u) -eq 0 ]]; then
error "This script should not be ran as root."
fi
# Check that the desired version is installed
if [ ! "$(brew list | grep php$1)" ] ; then
error "php$1 is not installed."
fi
# Check that we aren't switching to the same version
if [ "$NEW_VERSION" == "$CURRENT_VERSION" ]; then
notice "php$NEW_VERSION is already being used."
fi
# Let the user know what's happening
echo "Switching from php$CURRENT_VERSION to php$NEW_VERSION:"
# Stop Valet
echo "Stopping Valet..."
valet stop
# Unlink current version
echo "Unlinking php$CURRENT_VERSION..."
brew unlink "php$CURRENT_VERSION"
# Link new version
echo "Linking php$NEW_VERSION..."
brew link "php$NEW_VERSION"
# Start Valet
echo "Starting Valet..."
valet start
# Update global Composer packages
echo "Updating global Composer packages..."
composer global update
# Install Valet
echo "Installing Valet..."
valet install
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment