Skip to content

Instantly share code, notes, and snippets.

@pryley
Created February 8, 2017 03:27
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 pryley/6ad7ba02e28650f88cfd52bc966b5e8e to your computer and use it in GitHub Desktop.
Save pryley/6ad7ba02e28650f88cfd52bc966b5e8e to your computer and use it in GitHub Desktop.
Switch PHP versions for Laravel Valet
#!/bin/bash
# Check that homebrew is installed
if [[ "" == "$(command -v brew)" ]]; then
echo "switch-php requires homebrew to manage installed versions of PHP."
exit 1
fi
# Check if command was ran as root.
if [[ $(id -u) -eq 0 ]]; then
echo "This command 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
currentversion="$(php -r "error_reporting(0); echo str_replace('.', '', substr(phpversion(), 0, 3));")"
newversion="$1"
majorOld=${currentversion:0:1}
majorNew=${newversion:0:1}
minorNew=${newversion:1:1}
brew list php$newversion 2> /dev/null > /dev/null
if [ $? -eq 0 ]; then
# Check if new version is already the current version.
if [ "${newversion}" == "${currentversion}" ]; then
echo -n "PHP version $majorNew.$minorNew 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
sudo brew services stop php$currentversion
brew unlink php$currentversion 2> /dev/null > /dev/null
brew link php$newversion
sudo brew services start php$newversion
echo "Done."
else
echo "PHP version $majorNew.$minorNew 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