1
1
# ###############################################################################
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
5
3
# ###############################################################################
6
4
7
5
function php-version {
8
6
local PROGRAM_APPNAME=' php-version'
9
- local PROGRAM_VERSION=0.9.10
7
+ local PROGRAM_VERSION=0.9.11
10
8
11
9
# colors
12
10
COLOR_NORMAL=$( tput sgr0)
13
11
COLOR_REVERSE=$( tput smso)
14
12
15
13
# target version
16
14
local _TARGET_VERSION=$1
17
- local _TARGET_VERSION_FUZZY
18
15
19
16
# PHP installation paths
20
17
local _PHP_VERSIONS=" "
21
18
22
- # Set Local _PHP_ROOT_LOCAL
23
- local _PHP_ROOT_LOCAL
24
-
25
19
# add ~/.phps if it exists (default)
26
20
if [[ -d $HOME /.phps ]]; then
27
21
export _PHP_VERSIONS=" $_PHP_VERSIONS $HOME /.phps"
28
22
fi
29
23
30
24
# add default Homebrew directories if brew is installed
31
- if [[ -n $( command -v brew) ]]; then
25
+ if [[ -n $( which brew) ]]; then
32
26
export _PHP_VERSIONS=" $_PHP_VERSIONS $( echo $( find $( brew --cellar) -maxdepth 1 -type d | grep -E ' php[0-9]+$' ) ) "
33
27
fi
34
28
@@ -37,27 +31,8 @@ function php-version {
37
31
export _PHP_VERSIONS=" $_PHP_VERSIONS $PHP_VERSIONS "
38
32
fi
39
33
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
61
36
echo ' Sorry, but you do not seem to have any PHP versions installed.' >&2
62
37
echo ' See https://github.com/wilmoore/php-version#install for assistance.' >&2
63
38
return 1
@@ -105,11 +80,7 @@ function php-version {
105
80
106
81
_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)
107
82
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
113
84
local selected=" "
114
85
local color=$COLOR_NORMAL
115
86
@@ -136,54 +107,24 @@ function php-version {
136
107
137
108
# try a fuzzy match since we were unable to find a PHP matching given version
138
109
if [[ -z $_PHP_ROOT ]]; then
139
-
140
110
_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)
141
111
142
112
for _PHP_REPOSITORY in $( echo $_PHP_VERSIONS ) ; do
143
113
if [[ -n " $_TARGET_VERSION_FUZZY " && -d $_PHP_REPOSITORY /$_TARGET_VERSION_FUZZY ]]; then
144
- _TARGET_VERSION=$_TARGET_VERSION_FUZZY
145
114
local _PHP_ROOT=$_PHP_REPOSITORY /$_TARGET_VERSION_FUZZY
146
115
break ;
147
116
fi
148
117
done
149
118
fi
150
119
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
-
175
120
# bail-out if we were unable to find a PHP matching given version
176
121
if [[ -z $_PHP_ROOT ]]; then
177
122
echo " Sorry, but $PROGRAM_APPNAME was unable to find version '$1 ' under '$_PHP_VERSIONS '." >&2
178
123
return 1
179
124
fi
180
125
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=" "
187
128
export PHP_ROOT=$_PHP_ROOT
188
129
[[ -f $_PHP_ROOT /etc/php.ini ]] && export PHPRC=$_PHP_ROOT /etc/php.ini
189
130
[[ -d $PHP_ROOT /bin ]] && export PATH=" $PHP_ROOT /bin:$PATH "
@@ -193,8 +134,5 @@ function php-version {
193
134
local _MANPATH=$( php-config --man-dir)
194
135
[[ -z $_MANPATH ]] && _MANPATH=$PHP_ROOT /share/man
195
136
[[ -d $_MANPATH ]] && export MANPATH=" $_MANPATH :$MANPATH "
196
-
197
- echo " SWITCHED PHP VERSION TO: $PHP_VERSION "
198
- echo " NEW PHP ROOT DIRECTORY : $PHP_ROOT "
199
137
}
200
138
0 commit comments