Skip to content

Instantly share code, notes, and snippets.

@iamEAP
Last active Aug 29, 2015
Embed
What would you like to do?
Shell function for switching between PHP versions (CLI/FPM)
#!/bin/bash
#
# Useful for switching between PHP versions (CLI and NGINX PHP-FPM) on OS X. Assumes
# you've installed PHP via brew, maybe like so:
# - brew install php56 && cp /path/to/cellar/php56/5.6.*/*.plist ~/Library/LaunchAgents/
#
# Installation:
# - curl https://gist.githubusercontent.com/iamEAP/c7464c7b42583baba98b/raw/.usephp > ~/.usephp
# - echo "[[ -s \"\$HOME/.usephp\" ]] && source \"\$HOME/.usephp\" # Load usephp as a function" >> ~/.bash_profile
# - You may also need to edit PHP paths/plist locations below...
#
# Usage/examples:
# - usephp 5.3
# - usephp 5.4
#
usephp() {
if [ "$1" != "5.6" ] && [ "$1" != "5.5" ] && [ "$1" != "5.4" ] && [ "$1" != "5.3" ]; then
echo "Unknown PHP version."
return
fi
# Helper vars.
CURPHP=$HOME/.usephp_current
# Save the original path for use later.
if [ -z "$ORIGINAL_PATH" ]; then
export ORIGINAL_PATH=$PATH
fi
# If not currently known, default to the system's PHP 5.3.
if [ ! -e "$CURPHP" ]; then
echo "$HOME/Library/LaunchAgents/homebrew-php.josegonzalez.php53.plist" > $CURPHP
fi
if [ "$1" == "5.6" ]; then
launchctl unload $(cat $CURPHP)
echo "$HOME/Library/LaunchAgents/homebrew.mxcl.php56.plist" > $CURPHP
launchctl load -w $(cat $CURPHP)
export PATH=/usr/local/Cellar/php56/5.6.3/bin:$ORIGINAL_PATH
fi
if [ "$1" == "5.5" ]; then
launchctl unload $(cat $CURPHP)
echo "$HOME/Library/LaunchAgents/homebrew.mxcl.php55.plist" > $CURPHP
launchctl load -w $(cat $CURPHP)
export PATH=/usr/local/Cellar/php55/5.5.12/bin:$ORIGINAL_PATH
fi
if [ "$1" == "5.4" ]; then
launchctl unload $(cat $CURPHP)
echo "$HOME/Library/LaunchAgents/homebrew.mxcl.php54.plist" > $CURPHP
launchctl load -w $(cat $CURPHP)
export PATH=/usr/local/Cellar/php54/5.4.28/bin:$ORIGINAL_PATH
fi
if [ "$1" == "5.3" ]; then
launchctl unload $(cat $CURPHP)
echo "$HOME/Library/LaunchAgents/homebrew-php.josegonzalez.php53.plist" > $CURPHP
launchctl load -w $(cat $CURPHP)
export PATH=/usr/local/Cellar/php53/5.3.28/bin:$ORIGINAL_PATH
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment