commit:     b363dda0a3d3bdab2874b3f12c64c9fc8beeef4c
Author:     Michael Orlitzky <mjo <AT> gentoo <DOT> org>
AuthorDate: Sat Dec 19 23:29:40 2015 +0000
Commit:     Michael Orlitzky <mjo <AT> gentoo <DOT> org>
CommitDate: Sat Dec 19 23:29:40 2015 +0000
URL:        https://gitweb.gentoo.org/proj/eselect-php.git/commit/?id=b363dda0

Factor our the active SAPI target getter functions.

We had five functions doing essentially the same thing:

  1. get_active_cli()
  2. get_active_cgi()
  3. get_active_fpm()
  4. get_active_phpdbg()
  5. get_active_apache2()

Now that we have the sapi_active_link_path() function taking a SAPI
name as an argument, these have been refactored. One new function
get_sapi_active_target() takes a SAPI name as an argument and returns
the name of the active target (using sapi_active_link_path).

 src/php.eselect.in | 62 +++++++++++++++---------------------------------------
 1 file changed, 17 insertions(+), 45 deletions(-)

diff --git a/src/php.eselect.in b/src/php.eselect.in
index 6c1f803..fff7784 100644
--- a/src/php.eselect.in
+++ b/src/php.eselect.in
@@ -195,33 +195,6 @@ find_targets_phpdbg() {
        done | @SORT@ | @UNIQ@
 }
 
-get_active_cli() {
-       # See get_active_apache2() for an explanation of the sed call.
-       local target=$(canonicalise "$(sapi_active_link_path cli)")
-       local ver="s:.*/usr/.*/\(php[0-9]\.[0-9][0-9]*\)/bin/php:\1:p"
-       [[ -a "${target}" ]] && echo "${target}" | @SED@ -ne "${ver}"
-}
-
-get_active_cgi() {
-       # See get_active_apache2() for an explanation of the sed call.
-       local target=$(canonicalise "$(sapi_active_link_path cgi)")
-       local ver="s:.*/usr/.*/\(php[0-9]\.[0-9][0-9]*\)/bin/php-cgi:\1:p"
-       [[ -a "${target}" ]] && echo "${target}" | @SED@ -ne "${ver}"
-}
-
-get_active_fpm() {
-       # See get_active_apache2() for an explanation of the sed call.
-       local target=$(canonicalise "$(sapi_active_link_path fpm)")
-       local ver="s:.*/usr/.*/\(php[0-9]\.[0-9][0-9]*\)/bin/php-fpm:\1:p"
-       [[ -a "${target}" ]] && echo "${target}" | @SED@ -ne "${ver}"
-}
-
-get_active_phpdbg() {
-       # See get_active_apache2() for an explanation of the sed call.
-       local target=$(canonicalise "$(sapi_active_link_path dbg)")
-       local ver="s:.*/usr/.*/\(php[0-9]\.[0-9][0-9]*\)/bin/phpdbg:\1:p"
-       [[ -a "${target}" ]] && echo "${target}" | @SED@ -ne "${ver}"
-}
 
 # Find the active (selected) version of the apache2 module. Used to
 # decorate the output of the `eselect php list apache2` command.
@@ -235,21 +208,20 @@ get_active_phpdbg() {
 # The "display name" of the active apache2 module. For example,
 # "php5.6" or "php7.0".
 #
-get_active_apache2() {
-       local active_symlink target ver
-
-       # The symlink to our active module.
-       active_symlink="$(sapi_active_link_path apache2)"
-
-       # This sed expression finds the "display name" of the PHP version
-       # corresponding to a copy of libphp. For example, it parses the
-       # string "php5.6" out of "/usr/lib64/php5.6/apache2/libphp5.so".
-       ver="s:.*/usr/.*/\(php[0-9]\.[0-9]\)/apache2/libphp[57].so:\1:p"
+get_sapi_active_target() {
+       local sapi="${1}"
+       local active_symlink=$(sapi_active_link_path "${sapi}")
 
        if [[ -L "${active_symlink}" ]] ; then
-               target=$(canonicalise "${active_symlink}")
-               if [[ -a "${target}" ]] ; then
-                       echo "${target}" | @SED@ -ne "${ver}"
+               local active_file=$(canonicalise "${active_symlink}")
+               if [[ -a "${active_file}" ]] ; then
+                       # This sed command (regular expression) finds a target 
name
+                       # contained in a filesystem path. For example, it parses
+                       # "php5.6" from "/usr/lib64/php5.6/apache2/libphp5.so".
+                       # The curly braces are an attempt to avoid '+' which is
+                       # a GNU extension.
+                       local sed_cmd='s:.*/\(php[0-9]\.[0-9]\{1,\}\)/.*:\1:p'
+                       echo "${active_file}" | @SED@ -ne "${sed_cmd}"
                fi
        fi
 }
@@ -305,7 +277,7 @@ list_apache2() {
        local targets
        local a
        targets=( $(find_targets_apache2) )
-       a=$(get_active_apache2)
+       a=$(get_sapi_active_target apache2)
        for (( i = 0; i < ${#targets[@]}; i++ )) ; do
                if [[ $a == ${targets[i]} ]] ; then
                        targets[i]=$(highlight_marker "${targets[i]}")
@@ -318,7 +290,7 @@ list_cli() {
        local targets
        local a
        targets=( $(find_targets_cli) )
-       a=$(get_active_cli)
+       a=$(get_sapi_active_target cli)
        for (( i = 0; i < ${#targets[@]}; i++ )) ; do
                if [[ $a == ${targets[i]} ]] ; then
                        targets[i]=$(highlight_marker "${targets[i]}")
@@ -331,7 +303,7 @@ list_cgi() {
        local targets
        local a
        targets=( $(find_targets_cgi) )
-       a=$(get_active_cgi)
+       a=$(get_sapi_active_target cgi)
        for (( i = 0; i < ${#targets[@]}; i++ )) ; do
                if [[ $a == ${targets[i]} ]] ; then
                        targets[i]=$(highlight_marker "${targets[i]}")
@@ -344,7 +316,7 @@ list_fpm() {
        local targets
        local a
        targets=( $(find_targets_fpm) )
-       a=$(get_active_fpm)
+       a=$(get_sapi_active_target fpm)
        for (( i = 0; i < ${#targets[@]}; i++ )) ; do
                if [[ $a == ${targets[i]} ]] ; then
                        targets[i]=$(highlight_marker "${targets[i]}")
@@ -357,7 +329,7 @@ list_phpdbg() {
        local targets
        local a
        targets=( $(find_targets_phpdbg) )
-       a=$(get_active_phpdbg)
+       a=$(get_sapi_active_target dbg)
        for (( i = 0; i < ${#targets[@]}; i++ )) ; do
                if [[ $a == ${targets[i]} ]] ; then
                        targets[i]=$(highlight_marker "${targets[i]}")

Reply via email to