Skip to content

Instantly share code, notes, and snippets.

@montchr
Created August 4, 2021 15:30
Show Gist options
  • Save montchr/b019f7737a1efbceb5cdbac47c7bb4e8 to your computer and use it in GitHub Desktop.
Save montchr/b019f7737a1efbceb5cdbac47c7bb4e8 to your computer and use it in GitHub Desktop.
phpvm :: A rudimentary version manager for Homebrew-installed PHP versions.
#!/usr/bin/env bash
#
# phpvm
#
# A rudimentary version manager for Homebrew-installed PHP versions.
#
# @TODO run composer update?
# @TODO consider offerring to attempt a patch version upgrade
VERSION="$1"
[[ -z "$VERSION" ]] && echo 'Version not specified!' && exit 1
recipe="php@${VERSION}"
conf_src="${XDG_CONFIG_HOME:-${HOME}/.config}/php/conf.d"
brew_prefix="$(brew --prefix)"
current=$(php-version)
semver() {
"${XDG_BIN_HOME}/semver" "$@"
}
# The semver tool expects a valid semver number, but brew php versions only
# include the major and minor version numbers. To handle this discrepancy, we
# append a patch version number to the version argument.
inferred_semver="${VERSION}.0"
echo "Current PHP version is ${current}"
case $(semver diff "${current}" "${inferred_semver}") in
major|minor)
echo "Changing PHP version to ${VERSION}"
;;
*)
echo "Requested PHP version ${VERSION} is already active!"
exit 1
;;
esac
brew upgrade "${recipe}"
brew unlink php && brew link "${recipe}"
if [[ -d "${conf_src}" ]]; then
echo "Linking custom ini files..."
fd --type f --extension ini . "${conf_src}" \
-x ln -sv {} "${brew_prefix}/etc/php/${VERSION}/conf.d"
echo "Done linking files."
else
echo "Custom conf file directory ${conf_src} not found. Skipping linking step..."
fi
printf "\nSuccess!\n\n"
which php
echo ""
php --version
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment