Last active
November 29, 2020 11:51
-
-
Save driskell/353b93ee80981f7d66f363da264df3d7 to your computer and use it in GitHub Desktop.
Brew helper for switching php versions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Requires ZSH | |
# Install your PHP versions via: brew install [email protected] | |
# Where x.x is the version such as 7.4 | |
# Do not need to link them it's harmless | |
# Add the below to your .profile or something | |
# Then run setphp x.x when you need to change php version - it will update php version in all terminal sessions | |
# Add to PATH to prefer over any system installed | |
export PATH="$HOME/bin/php:$PATH" | |
# Configure latest PHP version | |
setphp() { | |
local SELECTED PHPPATH UTILITY | |
SELECTED=$1 | |
if [ -z "$SELECTED" ]; then | |
SELECTED=$(xargs -n 1 echo <<<"$(echo /usr/local/opt/php@*)" | sort -n | tail -1) | |
SELECTED=${SELECTED#/usr/local/opt/php@} | |
fi | |
if [ -z "$SELECTED" ]; then | |
echo 'No PHP versions could be found' >&2 | |
return 1 | |
fi | |
PHPPATH="/usr/local/opt/php@$SELECTED" | |
if [ ! -d "$PHPPATH/bin" ]; then | |
echo 'Requested PHP version is not installed' >&2 | |
return 1 | |
fi | |
mkdir -p ~/bin/php | |
for UTILITY in ~/bin/php/*(N); do | |
command rm -f "$UTILITY" >/dev/null 2>&1 | |
done | |
for UTILITY in "$PHPPATH"/*bin/*; do | |
if [ "$UTILITY" = "$PHPPATH/bin/php-config" ]; then | |
# Stop brew doctor complaints | |
continue | |
fi | |
if [ "$UTILITY" = "$PHPPATH/bin/php" ]; then | |
# Adjust this as needed to add ini modifications that will affect any version | |
# Easier than having to mess with ini files in brew home | |
cat <<EOF >~"/bin/php/$(basename "$UTILITY")" | |
#!/bin/bash | |
"$UTILITY" -d memory_limit=4G "\$@" | |
EOF | |
chmod a+x ~"/bin/php/$(basename "$UTILITY")" | |
continue | |
fi | |
ln -nsf "$UTILITY" ~/bin/php/ | |
done | |
# Cleanup cached paths | |
hash -r | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment