forked from phpbrew/phpbrew
-
Notifications
You must be signed in to change notification settings - Fork 0
/
phpbrew.sh
210 lines (185 loc) · 5.12 KB
/
phpbrew.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/bin/bash
# Brought from gugod's perlbrew.
# Author: Yo-An Lin
# NOTICE: This script is for local testing, to release updated script,
# please also modify the src/PhpBrew/Command/InitCommand.php
# default phpbrew root and phpbrew home path
[[ -z "$PHPBREW_HOME" ]] && export PHPBREW_HOME="$HOME/.phpbrew"
if [[ -z "$PHPBREW_SKIP_INIT" ]]; then
# load user-defined config
if [[ -f $PHPBREW_HOME/init ]]; then
. $PHPBREW_HOME/init
export PATH=$PHPBREW_PATH:$PATH
fi
fi
[[ -z "$PHPBREW_ROOT" ]] && export PHPBREW_ROOT="$HOME/.phpbrew"
function phpbrew ()
{
if [[ -e bin/phpbrew ]] ; then
BIN='bin/phpbrew'
else
BIN='phpbrew'
fi
local exit_status
local short_option
export SHELL
if [[ `echo $1 | awk 'BEGIN{FS=""}{print $1}'` = '-' ]]
then
short_option=$1
shift
else
short_option=""
fi
case $1 in
use) if [[ -z "$2" ]]
then
if [[ -z "$PHPBREW_PHP" ]]
then
echo "Currently using system php"
else
echo "Currently using $PHPBREW_PHP"
fi
else
if [[ $2 =~ ^php- ]]
then
_PHP_VERSION=$2
else
_PHP_VERSION="php-$2"
fi
# checking php version exists?
NEW_PHPBREW_PHP_PATH="$PHPBREW_ROOT/php/$_PHP_VERSION"
if [ -d $NEW_PHPBREW_PHP_PATH ]; then
code=$(command $BIN env $_PHP_VERSION)
if [ -z "$code" ]
then
exit_status=1
else
eval $code
__phpbrew_set_path
fi
else
echo "php version: $_PHP_VERSION not exists."
fi
fi
;;
switch)
if [[ -z "$2" ]]
then
echo "Please specify the php version."
else
__phpbrew_reinit $2
fi
;;
off)
unset PHPBREW_PHP
unset PHPBREW_PATH
eval `$BIN env`
__phpbrew_set_path
echo "phpbrew is turned off."
;;
switch-off)
unset PHPBREW_PHP
unset PHPBREW_PATH
eval `$BIN env`
__phpbrew_set_path
__phpbrew_reinit
echo "phpbrew is switched off."
;;
remove)
if [[ -z "$2" ]]
then
command $BIN help
else
__phpbrew_remove_purge $2
fi
;;
rehash)
echo "Rehashing..."
. ~/.phpbrew/bashrc
;;
purge)
if [[ -z "$2" ]]
then
command $BIN help
else
__phpbrew_remove_purge $2 purge
fi
;;
*)
command $BIN $short_option "$@"
exit_status=$?
;;
esac
hash -r
return ${exit_status:-0}
}
function __phpbrew_set_path ()
{
[[ -n $(alias php 2>/dev/null) ]] && unalias php 2> /dev/null
if [[ -n $PHPBREW_ROOT ]] ; then
export PATH_WITHOUT_PHPBREW=$(perl -e 'print join ":", grep { index($_,$ENV{PHPBREW_ROOT}) } split/:/,$ENV{PATH};')
fi
if [[ -z "$PHPBREW_PATH" ]]
then
export PHPBREW_PATH="$PHPBREW_ROOT/bin"
fi
export PATH=$PHPBREW_PATH:$PATH_WITHOUT_PHPBREW
# echo "PATH => $PATH"
}
function __phpbrew_reinit ()
{
if [[ $1 =~ ^php- ]]
then
_PHP_VERSION=$1
else
_PHP_VERSION="php-$1"
fi
if [[ ! -d "$PHPBREW_HOME" ]]
then
mkdir -p -p "$PHPBREW_HOME"
fi
echo '# DO NOT EDIT THIS FILE' >| "$PHPBREW_HOME/init"
command $BIN env $_PHP_VERSION >> "$PHPBREW_HOME/init"
. "$PHPBREW_HOME/init"
__phpbrew_set_path
}
function __phpbrew_remove_purge ()
{
if [[ $1 =~ ^php- ]]
then
_PHP_VERSION=$1
else
_PHP_VERSION="php-$1"
fi
if [[ "$_PHP_VERSION" = "$PHPBREW_PHP" ]]
then
echo "php version: $_PHP_VERSION is already in used."
return 1
fi
_PHP_BIN_PATH=$PHPBREW_HOME/php/$_PHP_VERSION
_PHP_SOURCE_FILE=$PHPBREW_HOME/build/$_PHP_VERSION.tar.bz2
_PHP_BUILD_PATH=$PHPBREW_HOME/build/$_PHP_VERSION
if [ -d $_PHP_BIN_PATH ]; then
if [[ "$2" = "purge" ]]
then
rm -f $_PHP_SOURCE_FILE
rm -fr $_PHP_BUILD_PATH
rm -fr $_PHP_BIN_PATH
echo "php version: $_PHP_VERSION is removed and purged."
else
rm -f $_PHP_SOURCE_FILE
rm -fr $_PHP_BUILD_PATH
for FILE1 in $_PHP_BIN_PATH/*
do
if [[ "$FILE1" != "$_PHP_BIN_PATH/etc" ]] && [[ "$FILE1" != "$_PHP_BIN_PATH/var" ]]
then
rm -fr $FILE1;
fi
done
echo "php version: $_PHP_VERSION is removed."
fi
else
echo "php version: $_PHP_VERSION not installed."
fi
return 0
}