@@ -127,6 +127,51 @@ setup_cached_versions() {
127127 run_script " php-ubuntu" " $version " " ${debug:? } " " ${ts:? } "
128128}
129129
130+ # Function to get the link path for an alternative.
131+ alternative_link () {
132+ case " $1 " in
133+ php-cgi-bin) echo " /usr/lib/cgi-bin/php" ;;
134+ php-fpm) echo " /usr/sbin/php-fpm" ;;
135+ php-fpm.sock) echo " /run/php/php-fpm.sock" ;;
136+ * ) echo " /usr/bin/$1 " ;;
137+ esac
138+ }
139+
140+ # Function to get the target path for an alternative.
141+ alternative_target () {
142+ case " $1 " in
143+ php-cgi-bin) echo " /usr/lib/cgi-bin/php$version " ;;
144+ php-fpm) echo " /usr/sbin/php-fpm$version " ;;
145+ php-fpm.sock) echo " /run/php/php$version -fpm.sock" ;;
146+ * ) echo " /usr/bin/$1 $version " ;;
147+ esac
148+ }
149+
150+ # Function to register an alternative if the versioned binary exists but is missing.
151+ register_alternative () {
152+ local tool=$1
153+ local link target priority state_file
154+ target=" $( alternative_target " $tool " ) "
155+ [ -e " $target " ] || return 0
156+ state_file=" /var/lib/dpkg/alternatives/$tool "
157+ if sudo test -r " $state_file " && sudo grep -Fxq " $target " " $state_file " ; then
158+ return 0
159+ fi
160+ link=" $( alternative_link " $tool " ) "
161+ priority=" ${version// ./ } "
162+ sudo update-alternatives --install " $link " " $tool " " $target " " $priority " > /dev/null 2>&1
163+ }
164+
165+ # Function to register and switch an alternative.
166+ set_alternative () {
167+ local tool=$1
168+ local target
169+ target=" $( alternative_target " $tool " ) "
170+ [ -e " $target " ] || return 0
171+ register_alternative " $tool " || return 1
172+ sudo update-alternatives --set " $tool " " $target " > /dev/null 2>&1
173+ }
174+
130175# Function to add PECL.
131176add_pecl () {
132177 add_devtools phpize > /dev/null 2>&1
@@ -143,16 +188,11 @@ switch_version() {
143188 tools=(" $@ " )
144189 to_wait=()
145190 if ! (( ${# tools[@]} )) ; then
146- tools+=(pear pecl php phar phar.phar php-cgi php-config phpize phpdbg)
147- [ -e /usr/lib/cgi-bin/php" $version " ] && sudo update-alternatives --set php-cgi-bin /usr/lib/cgi-bin/php" $version " & to_wait+=($! )
148- [ -e /usr/sbin/php-fpm" $version " ] && sudo update-alternatives --set php-fpm /usr/sbin/php-fpm" $version " & to_wait+=($! )
149- [ -e /run/php/php" $version " -fpm.sock ] && sudo update-alternatives --set php-fpm.sock /run/php/php" $version " -fpm.sock & to_wait+=($! )
191+ tools+=(php-cgi-bin php-fpm php-fpm.sock pear pecl php phar phar.phar php-cgi php-config phpize phpdbg)
150192 fi
151193 for tool in " ${tools[@]} " ; do
152- if [ -e " /usr/bin/$tool$version " ]; then
153- sudo update-alternatives --set " $tool " /usr/bin/" $tool$version " &
154- to_wait+=($! )
155- fi
194+ set_alternative " $tool " &
195+ to_wait+=($! )
156196 done
157197 wait " ${to_wait[@]} "
158198}
0 commit comments