-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathsphp
More file actions
executable file
·200 lines (174 loc) · 5.65 KB
/
sphp
File metadata and controls
executable file
·200 lines (174 loc) · 5.65 KB
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
#!/usr/bin/env bash
################################################################################
#
# MacOS Homebrew-based PHP version switcher.
#
################################################################################
# Define read-only script variables
# -------------------------------------------------------------------------------
declare -r SPHP_BREW_PREFIX="$(brew --prefix | sed 's#/#\\\/#g')"
declare -r SPHP_BREW_ARRAY=("5.6" "7.0" "7.1" "7.2" "7.3" "7.4")
declare -r SPHP_PHP_ARRAY=("php@5.6" "php@7.0" "php@7.1" "php@7.2" "php@7.3" "php")
declare -r SPHP_PROFILE="${HOME}/.zshrc"
declare SPHP_INSTALLED_ARRAY=()
# Program usage
# -------------------------------------------------------------------------------
function _usage() {
echo -e "
$(tput bold)Usage:$(tput sgr0)
sphp [options...] <php_version>
$(tput bold)Options:$(tput sgr0)
-l, --list List available PHP versions
-h, --help Display this help and exit
"
}
# Available versions
# -------------------------------------------------------------------------------
function _versions() {
echo -e "Available versions of PHP: ${SPHP_BREW_ARRAY[@]}"
}
# Trap Function
# -------------------------------------------------------------------------------
# Any actions that should be taken if the script is prematurely
# exited. Always call this function at the top of your script.
# -------------------------------------------------------------------------------
function _trap() {
exit 1
}
# Exit Function
# -------------------------------------------------------------------------------
# Non-destructive exit for when script exits naturally.
# Add this function at the end of the script.
#
# Arguments:
# $1 (optional) - Exit code (defaults to 0)
# -------------------------------------------------------------------------------
function clean_exit() {
trap - INT TERM EXIT
exit ${1:-0}
}
# Parse Program Options
# -------------------------------------------------------------------------------
function parse_options() {
# iterate over options
# breaking -ab into -a -b when needed and --foo=bar into --foo bar
local opt_string=h
unset options
while (($#)); do
case $1 in
# if option is of type -ab
-[!-]?*)
# loop over each character starting with the second
for ((i = 1; i < ${#1}; i++)); do
c=${1:i:1}
options+=("-$c") # add current char to options
# if option takes a required argument, and it's not the last char make
# the rest of the string its argument
if [[ "${opt_string}" == *"$c:"* && ${1:i+1} ]]; then
options+=("${1:i+1}")
break
fi
done
;;
# if option is of type --foo=bar
--?*=*) options+=("${1%%=*}" "${1#*=}") ;;
# add --endopts for --
--) options+=(--endopts) ;;
# otherwise, nothing special
*) options+=("${1}") ;;
esac
shift
done
set -- "${options[@]}"
unset options
# read the options and set stuff
while [[ ${1-} == -?* ]]; do
case $1 in
-h | --help)
_usage >&2
clean_exit
;;
-l | --list)
_versions >&2
clean_exit
;;
--endopts)
shift
break
;;
*)
echo "sphp: invalid option '${1}'"
exit 1
;;
esac
shift
done
# store the remaining user input as arguments
ARGS+=("$@")
}
# Main Program
# -------------------------------------------------------------------------------
function main_run() {
local REQUESTED_VERSION="php@${ARGS}"
if [[ -z "${ARGS}" ]]; then
_usage
clean_exit
fi
# check that ZSH is being used
if [ -z "`$SHELL -c 'echo $ZSH_VERSION'`" ]; then
echo "sphp: sorry, this script assumes you are using ZSH."
clean_exit 1
fi
# get which versions the user has installed
for i in ${SPHP_PHP_ARRAY[*]}; do
if [[ -n "$(brew ls --versions "${i}")" ]]; then
SPHP_INSTALLED_ARRAY+=("${i}")
fi
done
# check the specified version is both supported and installed
if [[ " ${SPHP_PHP_ARRAY[*]} " == *"${REQUESTED_VERSION}"* ]] && [[ "${SPHP_INSTALLED_ARRAY[*]}" == *"${REQUESTED_VERSION}"* ]]; then
echo "Switching to ${REQUESTED_VERSION}"
echo "Stopping brew services..."
for i in ${SPHP_INSTALLED_ARRAY[@]}; do
brew unlink "${i}" >/dev/null 2>&1
brew services stop "${i}" >/dev/null 2>&1
done
brew link --force "${PHP_VERSION}" >/dev/null 2>&1
echo "Switching your shell variables..."
if [[ ! -f "${SPHP_PROFILE}" ]]; then
echo "sphp: sorry, this script assumes your profile is ${SPHP_PROFILE}."
clean_exit 1
else
sed -i "/^path=(\/usr\/local\/opt\/php.*/s/^/#/" "${SPHP_PROFILE}"
sed -i "/path=(\/usr\/local\/opt\/${REQUESTED_VERSION}/s/^#//" "${SPHP_PROFILE}"
fi
echo "Starting brew services..."
brew services start "${REQUESTED_VERSION}" >/dev/null 2>&1
echo "You're good to go!"
exec /bin/zsh
source "${SPHP_PROFILE}"
else
echo "sphp: ${REQUESTED_VERSION} is not available."
echo ""
echo "Try installing it by running: brew install ${REQUESTED_VERSION}"
clean_exit 1
fi
}
################################################################################
#
# RUN THE SCRIPT
# Nothing should be edited below this block.
#
################################################################################
# trap bad exits with your cleanup function
trap _trap EXIT INT TERM SIGINT SIGQUIT
# trap errors in subshells and functions
set -o errtrace
# bash will remember & return the highest exitcode in a chain of pipes
set -o pipefail
# parse arguments passed to script
parse_options "$@"
# main script run
main_run
# clean exit
clean_exit