Improve installing php on macOS

This commit is contained in:
Shivam Mathur
2025-10-18 09:53:32 +05:30
parent 6f153798c8
commit f210bb3904

View File

@ -148,9 +148,9 @@ get_brewed_php() {
cellar="$brew_prefix"/Cellar cellar="$brew_prefix"/Cellar
php_cellar="$cellar"/php php_cellar="$cellar"/php
if [ -d "$cellar" ] && ! [[ "$(find "$cellar" -maxdepth 1 -name "php@$version*" | wc -l 2>/dev/null)" -eq 0 ]]; then if [ -d "$cellar" ] && ! [[ "$(find "$cellar" -maxdepth 1 -name "php@$version*" | wc -l 2>/dev/null)" -eq 0 ]]; then
php_semver | cut -c 1-3 php_semver
elif [ -d "$php_cellar" ] && ! [[ "$(find "$php_cellar" -maxdepth 1 -name "$version*" | wc -l 2>/dev/null)" -eq 0 ]]; then elif [ -d "$php_cellar" ] && ! [[ "$(find "$php_cellar" -maxdepth 1 -name "$version*" | wc -l 2>/dev/null)" -eq 0 ]]; then
php_semver | cut -c 1-3 php_semver
else else
echo 'false'; echo 'false';
fi fi
@ -160,17 +160,24 @@ get_brewed_php() {
add_php() { add_php() {
action=$1 action=$1
existing_version=$2 existing_version=$2
add_brew_tap "$php_tap"
update_dependencies
suffix="$(get_php_formula_suffix)" suffix="$(get_php_formula_suffix)"
php_formula="shivammathur/php/php@$version$suffix" php_keg="php@$version$suffix"
php_formula="shivammathur/php/$php_keg"
if [[ "$existing_version" = "false" || -n "$suffix" || "$action" = "upgrade" ]]; then
update_dependencies
add_brew_tap
fi
if [[ "$existing_version" != "false" && -z "$suffix" ]]; then if [[ "$existing_version" != "false" && -z "$suffix" ]]; then
([ "$action" = "upgrade" ] && brew upgrade -f --overwrite "$php_formula") || brew unlink "$php_formula" if [ "$action" = "upgrade" ]; then
brew upgrade -f --overwrite "$php_formula"
else
brew unlink "$php_keg"
fi
else else
brew install -f --overwrite "$php_formula" brew install -f --overwrite "$php_formula"
fi fi
sudo chown -R "$(id -un)":"$(id -gn)" "$brew_prefix" sudo chown -R "$(id -un)":"$(id -gn)" "$brew_prefix"
brew link --force --overwrite "$php_formula" brew link --force --overwrite "$php_keg"
} }
# Function to get formula suffix # Function to get formula suffix
@ -222,18 +229,18 @@ setup_php() {
update=true update=true
check_pre_installed check_pre_installed
existing_version=$(get_brewed_php) existing_version=$(get_brewed_php)
status="Found"
if [[ "$version" =~ ${old_versions:?} ]]; then if [[ "$version" =~ ${old_versions:?} ]]; then
run_script "php5-darwin" "${version/./}" >/dev/null 2>&1 run_script "php5-darwin" "${version/./}" >/dev/null 2>&1
status="Installed" status="Installed"
elif [ "$existing_version" != "$version" ]; then elif [ "${existing_version:0:3}" != "$version" ]; then
add_php "install" "$existing_version" >/dev/null 2>&1 add_php "install" "$existing_version" >/dev/null 2>&1
status="Installed" status="Installed"
elif [ "$existing_version" = "$version" ]; then elif [[ "${existing_version:0:3}" = "$version" && "${update:?}" = "true" ]]; then
if [ "${update:?}" = "true" ]; then brew_php_version="$(brew info --json "php@$version" 2>/dev/null | jq -r '.[].versions.stable')"
if [ "$brew_php_version" != "$existing_version" ]; then
add_php "upgrade" "$existing_version" >/dev/null 2>&1 add_php "upgrade" "$existing_version" >/dev/null 2>&1
status="Updated to" status="Upgraded"
else
status="Found"
fi fi
fi fi
php_config="$(command -v php-config)" php_config="$(command -v php-config)"