Last active
December 3, 2025 13:28
-
-
Save faytranevozter/f9ab62885446654b99dc52012385462a to your computer and use it in GitHub Desktop.
sphp
This file contains hidden or 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
| #!/bin/bash | |
| # PHP Version Switcher for shivammathur/homebrew-php | |
| # Author: Fahrur Rifai | |
| # Usage: ./sphp.sh <version> [variant] | |
| # Example: ./sphp.sh 8.2 | |
| # Example: ./sphp.sh 8.2 zts | |
| # Example: ./sphp.sh 8.2 debug | |
| set -e | |
| # Color codes for output | |
| RED='\033[0;31m' | |
| GREEN='\033[0;32m' | |
| YELLOW='\033[1;33m' | |
| NC='\033[0m' # No Color | |
| # Function to print colored output | |
| print_status() { | |
| printf "${GREEN}[INFO]${NC} %s\n" "$1" | |
| } | |
| print_error() { | |
| printf "${RED}[ERROR]${NC} %s\n" "$1" | |
| } | |
| print_warning() { | |
| printf "${YELLOW}[WARNING]${NC} %s\n" "$1" | |
| } | |
| # Check if version argument is provided | |
| if [ -z "$1" ]; then | |
| print_error "No PHP version specified!" | |
| echo "Usage: $0 <version> [variant]" | |
| echo "Example: $0 8.2" | |
| echo "Example: $0 8.2 zts" | |
| echo "Example: $0 8.2 debug" | |
| echo "" | |
| echo "Available variants: zts, debug, debug-zts" | |
| echo "" | |
| echo "Installed PHP versions:" | |
| brew list --formula | grep "^php@" | sed 's/^/ /' | |
| exit 1 | |
| fi | |
| VERSION=$1 | |
| VARIANT=${2:-""} | |
| # Construct the package name | |
| # PHP 8.5 is available as both 'php' and 'php@8.5' | |
| if [ -z "$VARIANT" ]; then | |
| if [ "$VERSION" = "8.5" ]; then | |
| PHP_PACKAGE="php" | |
| FULL_PACKAGE="shivammathur/php/php" | |
| else | |
| PHP_PACKAGE="php@${VERSION}" | |
| FULL_PACKAGE="shivammathur/php/${PHP_PACKAGE}" | |
| fi | |
| else | |
| if [ "$VERSION" = "8.5" ]; then | |
| PHP_PACKAGE="php-${VARIANT}" | |
| FULL_PACKAGE="shivammathur/php/${PHP_PACKAGE}" | |
| else | |
| PHP_PACKAGE="php@${VERSION}-${VARIANT}" | |
| FULL_PACKAGE="shivammathur/php/${PHP_PACKAGE}" | |
| fi | |
| fi | |
| # Check if Homebrew is installed | |
| if ! command -v brew &> /dev/null; then | |
| print_error "Homebrew is not installed. Please install it first." | |
| exit 1 | |
| fi | |
| # Check if shivammathur/php tap is added | |
| if ! brew tap | grep -q "shivammathur/php"; then | |
| print_warning "shivammathur/php tap is not added." | |
| read -p "Would you like to add it? (y/n) " -n 1 -r | |
| echo | |
| if [[ $REPLY =~ ^[Yy]$ ]]; then | |
| print_status "Adding shivammathur/php tap..." | |
| brew tap shivammathur/php | |
| else | |
| print_error "Tap addition cancelled." | |
| exit 1 | |
| fi | |
| fi | |
| # Check if the requested PHP version is installed | |
| PACKAGE_TO_CHECK="${PHP_PACKAGE}" | |
| # For PHP 8.5, also check for php@8.5 as an alternative | |
| if [ "$VERSION" = "8.5" ] && [ -z "$VARIANT" ]; then | |
| if brew list --formula | grep -q "^php$"; then | |
| PACKAGE_TO_CHECK="php" | |
| elif brew list --formula | grep -q "^php@8.5$"; then | |
| PACKAGE_TO_CHECK="php@8.5" | |
| PHP_PACKAGE="php@8.5" | |
| FULL_PACKAGE="shivammathur/php/php@8.5" | |
| fi | |
| fi | |
| if ! brew list --formula | grep -q "^${PACKAGE_TO_CHECK}$"; then | |
| print_warning "PHP ${VERSION}${VARIANT:+ ($VARIANT)} is not installed." | |
| read -p "Would you like to install it? (y/n) " -n 1 -r | |
| echo | |
| if [[ $REPLY =~ ^[Yy]$ ]]; then | |
| print_status "Installing ${FULL_PACKAGE}..." | |
| brew install ${FULL_PACKAGE} | |
| else | |
| print_error "Installation cancelled." | |
| exit 1 | |
| fi | |
| fi | |
| # Unlink all PHP versions | |
| print_status "Unlinking all PHP versions..." | |
| for php_ver in $(brew list --formula | grep "^php"); do | |
| brew unlink ${php_ver} 2>/dev/null || true | |
| done | |
| # Link the requested PHP version | |
| print_status "Linking PHP ${VERSION}${VARIANT:+ ($VARIANT)}..." | |
| brew link --overwrite --force ${FULL_PACKAGE} | |
| # Verify the switch | |
| print_status "Verifying PHP version..." | |
| CURRENT_VERSION=$(php -v | head -n 1) | |
| echo "" | |
| print_status "Successfully switched to:" | |
| echo "${CURRENT_VERSION}" | |
| print_status "Done! PHP CLI has been switched." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment