Skip to content

Commit 072f514

Browse files
committed
fixes #20 and #21
1 parent 8497be0 commit 072f514

File tree

1 file changed

+8
-70
lines changed

1 file changed

+8
-70
lines changed

php-version.sh

Lines changed: 8 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,28 @@
11
################################################################################
2-
# name: php-version
3-
# what: function that allows switching between compiled PHP versions
4-
# why : there is nothing wrong with trying to keep it simple...
2+
# php-version: function allowing one to switch between PHP versions
53
################################################################################
64

75
function php-version {
86
local PROGRAM_APPNAME='php-version'
9-
local PROGRAM_VERSION=0.9.10
7+
local PROGRAM_VERSION=0.9.11
108

119
# colors
1210
COLOR_NORMAL=$(tput sgr0)
1311
COLOR_REVERSE=$(tput smso)
1412

1513
# target version
1614
local _TARGET_VERSION=$1
17-
local _TARGET_VERSION_FUZZY
1815

1916
# PHP installation paths
2017
local _PHP_VERSIONS=""
2118

22-
# Set Local _PHP_ROOT_LOCAL
23-
local _PHP_ROOT_LOCAL
24-
2519
# add ~/.phps if it exists (default)
2620
if [[ -d $HOME/.phps ]]; then
2721
export _PHP_VERSIONS="$_PHP_VERSIONS $HOME/.phps"
2822
fi
2923

3024
# add default Homebrew directories if brew is installed
31-
if [[ -n $(command -v brew) ]]; then
25+
if [[ -n $(which brew) ]]; then
3226
export _PHP_VERSIONS="$_PHP_VERSIONS $(echo $(find $(brew --cellar) -maxdepth 1 -type d | grep -E 'php[0-9]+$'))"
3327
fi
3428

@@ -37,27 +31,8 @@ function php-version {
3731
export _PHP_VERSIONS="$_PHP_VERSIONS $PHP_VERSIONS"
3832
fi
3933

40-
# get list of all php versions currently in $PATH
41-
if [[ -n $(command -v php) ]]; then
42-
local _LINKED_PHP_PATHS
43-
local _LINKED_PHP_VERSIONS
44-
local _LINKED_PHP_PATH_DUPLICATE
45-
_LINKED_PHP_PATHS=$(which -a php 2>/dev/null | sort -u)
46-
# Removed Paths already in $_PHP_VERSIONS
47-
_LINKED_PHP_PATHS=$(echo "$_LINKED_PHP_PATHS $_PHP_VERSIONS $_PHP_VERSIONS" | tr " " "\n" | sort | uniq -u)
48-
49-
# Get versions for linked php binaries
50-
for linkedPHP in $(echo $_LINKED_PHP_PATHS); do
51-
_LINKED_PHP_VERSIONS="$_LINKED_PHP_VERSIONS $($linkedPHP --version 2>/dev/null | grep --only-matching --max-count=1 -E "[0-9]*\.[0-9]*\.[0-9]*")"
52-
done
53-
fi
54-
55-
# clean up leading and trailing whitespace
56-
_PHP_VERSIONS=$(echo $_PHP_VERSIONS | sed -e 's/^[[:space:]]*//')
57-
_LINKED_PHP_VERSIONS=$(echo $_LINKED_PHP_VERSIONS | sed -e 's/^[[:space:]]*//')
58-
59-
# bail-out if _PHP_VERSIONS and _LINKED_PHP_PATHS are empty
60-
if [[ -z $_PHP_VERSIONS && -z $_LINKED_PHP_PATHS ]]; then
34+
# bail-out if _PHP_VERSIONS is empty
35+
if [[ -z $_PHP_VERSIONS ]]; then
6136
echo 'Sorry, but you do not seem to have any PHP versions installed.' >&2
6237
echo 'See https://github.com/wilmoore/php-version#install for assistance.' >&2
6338
return 1
@@ -105,11 +80,7 @@ function php-version {
10580

10681
_PHP_REPOSITORY=$(find $(echo $_PHP_VERSIONS) -maxdepth 1 -mindepth 1 -type d -exec basename {} \; 2>/dev/null | sort -r -t . -k 1,1n -k 2,2n -k 3,3n)
10782

108-
# add system php to list
109-
_PHP_REPOSITORY=$(echo " $_LINKED_PHP_VERSIONS $_PHP_REPOSITORY" | tr " " "\n" | sort -r -u -t . -k 1,1n -k 2,2n -k 3,3n)
110-
111-
for version in $(echo $_PHP_REPOSITORY); do
112-
83+
for version in $_PHP_REPOSITORY; do
11384
local selected=" "
11485
local color=$COLOR_NORMAL
11586

@@ -136,54 +107,24 @@ function php-version {
136107

137108
# try a fuzzy match since we were unable to find a PHP matching given version
138109
if [[ -z $_PHP_ROOT ]]; then
139-
140110
_TARGET_VERSION_FUZZY=$(find $(echo $_PHP_VERSIONS) -maxdepth 1 -mindepth 1 -type d -exec basename {} \; 2>/dev/null | sort -r -t . -k 1,1n -k 2,2n -k 3,3n | grep ^$_TARGET_VERSION 2>/dev/null | tail -1)
141111

142112
for _PHP_REPOSITORY in $(echo $_PHP_VERSIONS); do
143113
if [[ -n "$_TARGET_VERSION_FUZZY" && -d $_PHP_REPOSITORY/$_TARGET_VERSION_FUZZY ]]; then
144-
_TARGET_VERSION=$_TARGET_VERSION_FUZZY
145114
local _PHP_ROOT=$_PHP_REPOSITORY/$_TARGET_VERSION_FUZZY
146115
break;
147116
fi
148117
done
149118
fi
150119

151-
152-
# if the selected version is in the system list, set its path [exact match]
153-
local _LINKED_PHP_VERSION_MATCH
154-
if [[ -n $(echo "$_TARGET_VERSION $_LINKED_PHP_VERSIONS" | tr " " "\n" | sort | uniq -d) ]]; then
155-
for linkedPHP in $(echo $_LINKED_PHP_PATHS); do
156-
_LINKED_PHP_VERSION_MATCH_TEST=$($linkedPHP --version 2>/dev/null | grep --only-matching --max-count=1 -E "[0-9]*\.[0-9]*\.[0-9]*")
157-
if [[ "$_LINKED_PHP_VERSION_MATCH_TEST" == "$_TARGET_VERSION" && -z $_PHP_ROOT ]]; then
158-
local _PHP_ROOT="$linkedPHP"
159-
fi
160-
done
161-
fi
162-
163-
# if the selected version is in the system list, set its path [fuzzy match]
164-
if [[ -z $_PHP_ROOT ]]; then
165-
_TARGET_VERSION_FUZZY=$(echo "$_LINKED_PHP_VERSIONS" | sort -r -t . -k 1,1n -k 2,2n -k 3,3n | tr " " "\n" | grep ^$_TARGET_VERSION 2>/dev/null | tail -1)
166-
for linkedPHP in $(echo $_LINKED_PHP_PATHS); do
167-
_LINKED_PHP_VERSION_MATCH_TEST=$($linkedPHP --version 2>/dev/null | grep --only-matching --max-count=1 -E "[0-9]*\.[0-9]*\.[0-9]*")
168-
if [[ "$_LINKED_PHP_VERSION_MATCH_TEST" == "$_TARGET_VERSION_FUZZY" ]]; then
169-
_TARGET_VERSION=$_TARGET_VERSION_FUZZY
170-
local _PHP_ROOT="$linkedPHP"
171-
fi
172-
done
173-
fi
174-
175120
# bail-out if we were unable to find a PHP matching given version
176121
if [[ -z $_PHP_ROOT ]]; then
177122
echo "Sorry, but $PROGRAM_APPNAME was unable to find version '$1' under '$_PHP_VERSIONS'." >&2
178123
return 1
179124
fi
180125

181-
# Cleanup existing php paths and ini configuration
182-
#PATH=$(echo $PATH | sed -e 's/[^:]*php[0-9]*:*//g' )
183-
PATH=$(echo "$PATH" | sed -e 's/[^:]*php[0-9]*[^:]*\/bin:*//g')
184-
PHPRC=""
185-
186-
export PHP_VERSION=$_TARGET_VERSION
126+
# export current paths
127+
export PHPRC=""
187128
export PHP_ROOT=$_PHP_ROOT
188129
[[ -f $_PHP_ROOT/etc/php.ini ]] && export PHPRC=$_PHP_ROOT/etc/php.ini
189130
[[ -d $PHP_ROOT/bin ]] && export PATH="$PHP_ROOT/bin:$PATH"
@@ -193,8 +134,5 @@ function php-version {
193134
local _MANPATH=$(php-config --man-dir)
194135
[[ -z $_MANPATH ]] && _MANPATH=$PHP_ROOT/share/man
195136
[[ -d $_MANPATH ]] && export MANPATH="$_MANPATH:$MANPATH"
196-
197-
echo "SWITCHED PHP VERSION TO: $PHP_VERSION"
198-
echo "NEW PHP ROOT DIRECTORY : $PHP_ROOT"
199137
}
200138

0 commit comments

Comments
 (0)