Skip to content

Instantly share code, notes, and snippets.

@esolitos
Created July 22, 2017 11:28
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 esolitos/3d9235902f04a8264d158351ecb1c42a to your computer and use it in GitHub Desktop.
Save esolitos/3d9235902f04a8264d158351ecb1c42a to your computer and use it in GitHub Desktop.
MacOS X: Automatically swap php-fpm versions from brew
#!/bin/sh
#
# Swaps between php-fpm versions.
# This function assumes php binaries inslatted with brew
#
# @parameter Semplified php version number
# (eg. PHP 5.6 is simply 56, PHP7.0 is 70 and so on...)
#
php-fpm.swap-to() {
if [ -z "$1" ]; then
echo "\nUsage: $0 \$php-ver\n\t(eg. $0 56 # switches to version 5.6)\n" 1>&2
return -1;
fi
# FPM_CURRENT=$(launchctl list | grep php | awk -F'[\s\.]' '{print $3}')
FPM_CURRENT=$(launchctl list | grep php | awk -F'[^0-9]*' '{print $3}')
FPM_NEW="$1"
if [ -z "$FPM_CURRENT" ]; then
echo "\nWARNING: No current php version loaded" 1>&2
elif [ "$FPM_CURRENT" = "$1" ]; then
echo "Nothing to do, current version is already php$FPM_CURRENT"
return 0;
else
echo "Attempting to swap between php$FPM_CURRENT and php$FPM_NEW"
fi
CUR_USR=$(id -u -n)
LAUNCHAGENTS_DIR="/Users/$CUR_USR/Library/LaunchAgents"
OPT_DIR='/usr/local/opt'
CUR_PLIST_LINK="$LAUNCHAGENTS_DIR/homebrew.mxcl.php$FPM_CURRENT.plist"
NEW_PLIST_FILE="homebrew.mxcl.php$FPM_NEW.plist"
if [ ! -f "$OPT_DIR/php$FPM_NEW/$NEW_PLIST_FILE" ]; then
echo "\nERROR: Missing php-fpm launchtl file for version: $FPM_NEW\n" 1>&2
return -2;
fi
if [ ! -z $FPM_CURRENT ]; then
echo "Unloading php-fpm $FPM_CURRENT service."
launchctl unload -w "$CUR_PLIST_LINK"
fi
if [ -L "$CUR_PLIST_LINK" ]; then
/bin/rm "$CUR_PLIST_LINK"
fi
echo "Loading php-fpm $FPM_NEW service."
/bin/ln -s "$OPT_DIR/php$FPM_NEW/$NEW_PLIST_FILE" "$LAUNCHAGENTS_DIR/$NEW_PLIST_FILE"
launchctl load -w "$LAUNCHAGENTS_DIR/$NEW_PLIST_FILE"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment