Skip to content

Instantly share code, notes, and snippets.

@kroell
Last active January 25, 2022 09:30
Show Gist options
  • Save kroell/2752a52554a21eef76ee88794f5cdec2 to your computer and use it in GitHub Desktop.
Save kroell/2752a52554a21eef76ee88794f5cdec2 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Creator: Phil Cook
# Modified: Andy Miller
# Original => https://gist.github.com/rhukster/f4c04f1bf59e0b74e335ee5d186a98e2
brew_prefix=$(brew --prefix | sed 's#/#\\\/#g')
brew_array=("5.5","5.6","7.0","7.1","7.2", "7.3", "7.4", "8.0")
php_array=("php@5.5" "php@5.6" "php@7.0" "php@7.1" "php@7.2" "php@7.3" "php@7.4" "php@8.0")
valet_support_php_version_array=("php@5.5" "php@5.6" "php@7.0" "php@7.1" "php@7.2" "php@7.3" "php@7.4" "php@8.0")
php_installed_array=()
php_version="php@$1"
php_opt_path="$brew_prefix\/opt\/"
php5_module="php5_module"
apache_php5_lib_path="\/lib\/httpd\/modules\/libphp5.so"
php7_module="php7_module"
apache_php7_lib_path="\/lib\/httpd\/modules\/libphp7.so"
native_osx_php_apache_module="LoadModule php5_module libexec\/apache2\/libphp5.so"
php_module="$php5_module"
apache_php_lib_path="$apache_php5_lib_path"
# Has the user submitted a version required
if [[ -z "$1" ]]
then
echo "usage: sphp version [-s|-s=*] [-c=*]"; echo;
echo " version one of:" ${brew_array[@]};
echo " -s skip change of mod_php on apache";
echo " -s=* skip change of mod_php on apache or valet restart i.e (apache|valet,apache|valet)";
echo " -c=* switch a specific config (apache|valet,apache|valet"; echo;
exit
fi
if [ $(echo "$php_version" | sed 's/^php@//' | sed 's/\.//') -ge 70 ]; then
php_module="$php7_module"
apache_php_lib_path="$apache_php7_lib_path"
fi
apache_change=0
valet_restart=0
# Check if valet is already install
hash valet 2>/dev/null && valet_installed=1 || valet_installed=0
POSITIONAL=()
# Check for skip & change flag
while [[ $# -gt 0 ]]; do
key="$1"
case "$key" in
# This is a flag type option. Will catch either -s or --skip
-s|-s=*|--skip=*)
if [[ "${1#*=}" == "-s" || "${1#*=}" == *"apache"* ]]; then
apache_change=0
elif [ "${1#*=}" == "valet" ]; then
valet_restart=0
fi
;;
# This is a flag type option. Will catch either -c or --change
-c=*|--change=*)
[[ "$1" == *"apache"* ]] && apache_change=1 || apache_change=0
[[ "$1" == *"valet"* ]] && valet_restart=1 || valet_restart=0
;;
*)
POSITIONAL+=("$1") # save it in an array for later
;;
esac
# Shift after checking all the cases to get the next option
shift
done
# What versions of php are installed via brew
for i in ${php_array[*]}
do
if [[ -n "$(brew ls --versions "$i")" ]]
then
php_installed_array+=("$i")
fi
done
# Check if php version support via valet
if [[ (" ${valet_support_php_version_array[*]} " != *"$php_version"*) && ($valet_restart -eq 1) ]]
then
echo "Sorry, but $php_version is not support via valet";
exit;
fi
# Check that the requested version is supported
if [[ " ${php_array[*]} " == *"$php_version"* ]]
then
# Check that the requested version is installed
if [[ " ${php_installed_array[*]} " == *"$php_version"* ]]
then
# Stop valet service
if [[ ($valet_installed -eq 1) && ($valet_restart -eq 1) ]]; then
echo "Stop Valet service";
valet stop;
fi
# Switch Shell
echo "Switching to $php_version"
echo "Switching your shell"
for i in ${php_installed_array[@]}
do
if [[ -n $(brew ls --versions $i) ]]
then
brew unlink $i
fi
done
brew link --force "$php_version"
# Switch valet
if [[ $valet_restart -eq 1 ]]; then
if [[ valet_installed -eq 1 ]]; then
valet restart
else
echo "valet doesn't installed in your system, will skip restarting valet service";
fi
fi
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
@kroell
Copy link
Author

kroell commented Aug 23, 2019

# install php version via brew
brew tap exolnet/homebrew-deprecated

brew install php@5.6
brew install php@7.0
brew install php@7.1
brew install php@7.2
brew install php@7.3
brew install php@7.4
# list all php version installed via brew
brew list | grep php
# install this script
curl -L https://gist.githubusercontent.com/kroell/2752a52554a21eef76ee88794f5cdec2/raw/d2423eecf354753dccdb2a6080c2ec03285f2b7d/sphp.sh > /usr/local/bin/sphp
chmod +x /usr/local/bin/sphp
# switch php version
sphp 7.1

@kroell
Copy link
Author

kroell commented Jan 25, 2022

Update: use this tap: https://github.com/shivammathur/homebrew-php

# install php version via brew
brew tap shivammathur/php

brew install shivammathur/php/php@5.6
brew install shivammathur/php/php@7.0
brew install shivammathur/php/php@7.1
brew install shivammathur/php/php@7.2
brew install shivammathur/php/php@7.3
brew install shivammathur/php/php@7.4
brew install shivammathur/php/php@8.0
brew install shivammathur/php/php@8.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment