Created
July 31, 2025 19:40
-
-
Save Zxce3/713562a9a973a70caee21038cc7e834c to your computer and use it in GitHub Desktop.
PHP Extension Manager for Debian-based systems
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 Extension Manager for Debian-based systems | |
| # Author: zxce3 (Memet) | |
| set -e | |
| # Colors | |
| RED="\e[31m" | |
| GREEN="\e[32m" | |
| CYAN="\e[36m" | |
| RESET="\e[0m" | |
| # Detect PHP version | |
| EXT_VERSION=$(php -r 'echo PHP_MAJOR_VERSION.".".PHP_MINOR_VERSION;') | |
| # Get installed extensions | |
| get_installed_extensions() { | |
| dpkg -l | grep -oP "php${EXT_VERSION}-\S+" | sort -u | |
| } | |
| # Get available extensions from apt | |
| get_available_extensions() { | |
| apt-cache search php$EXT_VERSION- | awk '{print $1}' | sort -u | |
| } | |
| # Install extension | |
| install_extension() { | |
| read -p "Enter extension name (without php$EXT_VERSION- prefix): " ext | |
| sudo apt install -y php$EXT_VERSION-"$ext" | |
| } | |
| # Remove extension | |
| remove_extension() { | |
| read -p "Enter extension name (without php$EXT_VERSION- prefix): " ext | |
| sudo apt remove -y php$EXT_VERSION-"$ext" | |
| } | |
| # Show info | |
| show_extension_info() { | |
| read -p "Enter extension name (without php$EXT_VERSION- prefix): " ext | |
| apt show php$EXT_VERSION-"$ext" 2>/dev/null || echo -e "${RED}Extension not found.${RESET}" | |
| } | |
| # Install all available extensions | |
| install_all_extensions() { | |
| echo -e "${CYAN}Fetching all available PHP extensions...${RESET}" | |
| all_exts=$(get_available_extensions) | |
| if [[ -z "$all_exts" ]]; then | |
| echo -e "${RED}No extensions found.${RESET}" | |
| return | |
| fi | |
| echo -e "${CYAN}Filtering known conflicts...${RESET}" | |
| declare -A conflicts | |
| conflicts["php$EXT_VERSION-apcu"]="php$EXT_VERSION-yac" | |
| conflicts["php$EXT_VERSION-yac"]="php$EXT_VERSION-apcu" | |
| conflicts["php$EXT_VERSION-gmagick"]="php$EXT_VERSION-imagick" | |
| conflicts["php$EXT_VERSION-imagick"]="php$EXT_VERSION-gmagick" | |
| selected_exts=() | |
| excluded_exts=() | |
| for ext in $all_exts; do | |
| conflict=${conflicts[$ext]} | |
| if [[ -n "$conflict" && " ${selected_exts[@]} " =~ " $conflict " ]]; then | |
| echo -e "${YELLOW}Skipping $ext due to conflict with $conflict${RESET}" | |
| excluded_exts+=("$ext") | |
| continue | |
| fi | |
| selected_exts+=("$ext") | |
| done | |
| # Check transitive conflicts (e.g. depends on excluded) | |
| echo -e "${CYAN}Resolving indirect dependencies on excluded extensions...${RESET}" | |
| for ext in "${selected_exts[@]}"; do | |
| for ex in "${excluded_exts[@]}"; do | |
| if apt-cache depends "$ext" | grep -q "$ex"; then | |
| echo -e "${YELLOW}Skipping $ext because it depends on excluded $ex${RESET}" | |
| selected_exts=("${selected_exts[@]/$ext}") | |
| break | |
| fi | |
| done | |
| done | |
| echo -e "${GREEN}Installing ${#selected_exts[@]} extensions...${RESET}" | |
| sudo apt install -y "${selected_exts[@]}" || { | |
| echo -e "${RED}Some packages failed to install. Attempting to continue...${RESET}" | |
| } | |
| } | |
| # Menu | |
| while true; do | |
| echo -e "${CYAN}" | |
| echo "PHP Extension Manager" | |
| echo "---------------------" | |
| echo "Detected PHP version: $EXT_VERSION" | |
| echo -e "${RESET}" | |
| echo "1) List installed extensions" | |
| echo "2) List available extensions" | |
| echo "3) Install extension" | |
| echo "4) Remove extension" | |
| echo "5) Show extension info" | |
| echo "6) Exit" | |
| echo "7) Install ALL extensions (auto-resolve conflicts)" | |
| echo -n "Choose an option [1-7]: " | |
| read choice | |
| case $choice in | |
| 1) | |
| echo -e "${GREEN}Installed extensions:${RESET}" | |
| get_installed_extensions | |
| ;; | |
| 2) | |
| echo -e "${GREEN}Available extensions:${RESET}" | |
| get_available_extensions | |
| ;; | |
| 3) | |
| install_extension | |
| ;; | |
| 4) | |
| remove_extension | |
| ;; | |
| 5) | |
| show_extension_info | |
| ;; | |
| 6) | |
| echo -e "${CYAN}Bye!${RESET}" | |
| exit 0 | |
| ;; | |
| 7) | |
| install_all_extensions | |
| ;; | |
| *) | |
| echo -e "${RED}Invalid option.${RESET}" | |
| ;; | |
| esac | |
| echo "" | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment