Skip to content

Instantly share code, notes, and snippets.

@bingogg14
Last active January 11, 2024 17:28
Show Gist options
  • Save bingogg14/8a38ffa90ebad8676f8df9c898a91860 to your computer and use it in GitHub Desktop.
Save bingogg14/8a38ffa90ebad8676f8df9c898a91860 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Shell script for switching between Brew-installed PHP versions
#
# Copyright (c) 2023 Andy Miller
# Released under the MIT License
#
# Creator: Phil Cook
# Modified: Andy Miller
#
# More information: https://github.com/rhukster/sphp.sh
script_version=1.1.0
# Supported PHP versions
brew_array=("5.6" "7.0" "7.1" "7.2" "7.3" "7.4" "8.0" "8.1" "8.2" "8.3" "8.4")
# Reference arrays defining PHP module and Apache library path per PHP version
php_modules[5]="php5_module"
php_modules[7]="php7_module"
php_modules[8]="php_module"
# ----------------------------------------------------------------------------
# Helper functions
#
# Returns the following values based on PHP version
# - PHP module
# - Apache PHP Library path
#
# Parameters:
# $1 = PHP version ('X.Y' or '[email protected]')
#
# Example usage (to assign 'module' and 'libpath' variables):
# read -r module libpath < <(apache_module_and_lib "<PHP version>")
# Make sure we call macOS native grep.
# This avoids warnings when GNU grep >=3.8+ is installed (see #1)
grep() {
/usr/bin/grep "$@"
}
# Escaping a path for use in shell command
# Adds a '\' before '/'
sed_escape() {
string="$*"
echo "${string//\//\\/}"
}
# ----------------------------------------------------------------------------
# Main script
#
target_version=$1
php_version="php@$target_version"
# Display help and exit if the user did not specify a version
if [[ -z "$target_version" ]]; then
echo "PHP Switcher - v$script_version"
echo
echo "Switch between Brew-installed PHP versions."
echo
echo "usage: $(basename "$0") version [-s|-s=*] [-c=*]"
echo
echo " version one of:" "${brew_array[@]}"
echo
exit
fi
homebrew_path=$(brew --prefix)
php_opt_path="$homebrew_path/opt/"
# From the list of supported PHP versions, build array of PHP versions actually
# installed on the system via brew
for version in ${brew_array[*]}; do
if [[ -d "$homebrew_path/etc/php/$version" ]]; then
php_installed_array+=("$version")
fi
done
# Check that the requested version is supported
if [[ " ${brew_array[*]} " == *"$target_version"* ]]; then
# Check that the requested version is installed
if [[ " ${php_installed_array[*]} " == *"$target_version"* ]]; then
# Switch Shell
echo "Switching to $php_version"
echo "Switching your shell"
for i in "${php_installed_array[@]}"; do
brew unlink "php@$i"
done
brew link --force "$php_version"
echo
php -v
echo
echo "All done!"
else
echo "Sorry, but $php_version is not installed via brew. Install by running: brew install $php_version"
fi
else
echo "Unknown version of PHP. PHP Switcher can only handle arguments of:" "${brew_array[@]}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment