Last active
January 3, 2025 05:25
-
-
Save yukimunet/ebb234038593c26f82ac17206918fdf7 to your computer and use it in GitHub Desktop.
Link PHP installed via Homebrew to phpenv
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
| #!/usr/bin/env bash | |
| # | |
| # Link PHP installed via Homebrew to phpenv | |
| set -euC | |
| readonly PHP_VERSIONS=(5.6 7.0 7.1 7.2 7.3 7.4 8.0 8.1 8.2 8.3 8.4 8.5) | |
| # Log a message to stdout or stderr | |
| # Arguments: | |
| # $1: The log level (INFO, WARNING, ERROR) | |
| # $2: The message to log | |
| # Outputs: | |
| # The log message | |
| function log_message() { | |
| local -r level="$1" | |
| local -r message="$2" | |
| if [[ "${level}" == "ERROR" ]]; then | |
| echo "[${level}] ${message}" >&2 | |
| else | |
| echo "[${level}] ${message}" | |
| fi | |
| } | |
| # Check if all dependencies are installed | |
| # Returns | |
| # 0 if all dependencies are installed, 1 otherwise | |
| function check_dependencies() { | |
| local -r dependencies=("brew" "phpenv") | |
| local cmd | |
| for cmd in "${dependencies[@]}"; do | |
| if ! command -v "${cmd}" &>/dev/null; then | |
| log_message "ERROR" "${cmd} not found. Please install it." | |
| return 1 | |
| fi | |
| done | |
| return 0 | |
| } | |
| # Setup environment variables | |
| # Globals: | |
| # PHPENV_VERSIONS_DIR | |
| # BREW_CELLAR_DIR | |
| function setup_environment() { | |
| PHPENV_VERSIONS_DIR=$(realpath "$(dirname "$(command -v phpenv)")/../versions") | |
| BREW_CELLAR_DIR=$(brew --prefix)/Cellar | |
| } | |
| # Remove broken links in a directory | |
| # Arguments: | |
| # $1: The directory to search for broken links | |
| # Returns | |
| # 0 if all broken links are removed, 1 otherwise | |
| function remove_broken_links_in_dir() { | |
| local -r dir="$1" | |
| find -L "${dir}" -type l -exec echo "Removing broken link: {}" \; -exec rm {} + | |
| return $? | |
| } | |
| # Create a symbolic link if it doesn't exist | |
| # Arguments: | |
| # $1: The target of the symbolic link | |
| # $2: The symbolic link to create | |
| # Returns | |
| # 0 if the symbolic link is created, 1 otherwise | |
| function safe_create_symlink() { | |
| local -r target="$1" | |
| local -r link="$2" | |
| if [ -e "${link}" ] && [ ! -L "${link}" ]; then | |
| log_message "WARNING" "${link} already exists and is not a symbolic link" | |
| return 0 | |
| fi | |
| [ -e "${link}" ] && rm "${link}" | |
| if ln -s "${target}" "${link}"; then | |
| log_message "INFO" "Linked ${target} to ${link}" | |
| return 0 | |
| else | |
| log_message "ERROR" "Failed to link ${target} to ${link}" | |
| return 1 | |
| fi | |
| } | |
| # Link all PHP versions in a directory | |
| # Globals: | |
| # PHPENV_VERSIONS_DIR | |
| # Arguments: | |
| # $1: The directory containing PHP versions | |
| function link_php_versions() { | |
| local -r brew_php_dir="$1" | |
| local php_dir | |
| find "$brew_php_dir" -mindepth 1 -maxdepth 1 -type d | while read -r php_dir; do | |
| local -r php_version=$(basename "${php_dir}" | cut -d _ -f 1) | |
| safe_create_symlink "${php_dir}" "${PHPENV_VERSIONS_DIR}/${php_version}" | |
| done | |
| } | |
| # Link the latest PHP version in a directory | |
| # Globals: | |
| # PHPENV_VERSIONS_DIR | |
| # Arguments: | |
| # $1: The directory containing PHP versions | |
| # $2: The PHP version to link | |
| function link_latest_php_version() { | |
| local -r brew_php_dir="$1" | |
| local -r php_version="$2" | |
| local -r latest_dir=$(find "${brew_php_dir}" -mindepth 1 -maxdepth 1 -type d | sort -r | head -n 1) | |
| if [[ -n "${latest_dir}" ]]; then | |
| safe_create_symlink "${latest_dir}" "$PHPENV_VERSIONS_DIR/$php_version" | |
| else | |
| log_message "WARNING" "No valid directories found in ${brew_php_dir} for PHP ${php_version}" | |
| fi | |
| } | |
| # Process a PHP version | |
| # Globals: | |
| # BREW_CELLAR_DIR | |
| # Arguments: | |
| # $1: The PHP version to process | |
| function process_php_version() { | |
| local -r php_version="$1" | |
| local -r brew_php_dir="${BREW_CELLAR_DIR}/php@${php_version}" | |
| if [[ -d "${brew_php_dir}" ]]; then | |
| log_message "INFO" "PHP ${php_version} installed" | |
| link_php_versions "${brew_php_dir}" | |
| link_latest_php_version "${brew_php_dir}" "${php_version}" | |
| else | |
| log_message "INFO" "PHP ${php_version} not installed" | |
| fi | |
| } | |
| # Process a PHP (latest version) | |
| # Globals: | |
| # BREW_CELLAR_DIR | |
| function process_php_latest_version() { | |
| local -r brew_php_dir="${BREW_CELLAR_DIR}/php" | |
| if [[ -d "${brew_php_dir}" ]]; then | |
| local -r latest_dir=$(find "${brew_php_dir}" -mindepth 1 -maxdepth 1 -type d | sort -r | head -n 1) | |
| local -r php_bin="${latest_dir}/bin/php" | |
| local -r php_version=$(${php_bin} -r "echo PHP_VERSION . PHP_EOL;") | |
| local -r php_short_version=$(echo "$php_version" | cut -d '.' -f 1,2) | |
| log_message "INFO" "PHP ${php_version} installed" | |
| link_php_versions "${brew_php_dir}" | |
| link_latest_php_version "${brew_php_dir}" "${php_short_version}" | |
| else | |
| log_message "INFO" "PHP(no version) not installed" | |
| fi | |
| } | |
| # Main function | |
| # Globals: | |
| # PHP_VERSIONS | |
| # PHPENV_VERSIONS_DIR | |
| # BREW_CELLAR_DIR | |
| function main() { | |
| check_dependencies || exit 1 | |
| setup_environment | |
| log_message "INFO" "Removing broken links in ${PHPENV_VERSIONS_DIR}" | |
| remove_broken_links_in_dir "${PHPENV_VERSIONS_DIR}" | |
| local php_version | |
| for php_version in "${PHP_VERSIONS[@]}"; do | |
| process_php_version "${php_version}" | |
| done | |
| process_php_latest_version | |
| } | |
| if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then | |
| main | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment