-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathsphp
More file actions
executable file
·214 lines (191 loc) · 7.38 KB
/
sphp
File metadata and controls
executable file
·214 lines (191 loc) · 7.38 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/bin/bash
# Creator: Phil Cook
# Email: phil@phil-cook.com
# Twitter: @p_cook
if ! command -v brew &>/dev/null; then
echo "Error: Homebrew is required. Install it from https://brew.sh"
exit 1
fi
brew_prefix_raw=$(brew --prefix)
brew_prefix=$(echo "$brew_prefix_raw" | sed 's#/#\\\/#g')
# Detect installed PHP versions from brew (no hardcoded list to maintain)
brew_installed=$(brew list --formula 2>/dev/null)
php_installed_array=($(echo "$brew_installed" | grep '^php@' | sort -t@ -k2 -V))
# The latest PHP is installed as "php" not "php@X.Y" — detect its version
php_main_ver=""
if echo "$brew_installed" | grep -q '^php$'; then
php_main_ver=$(basename "$(readlink "$brew_prefix_raw/opt/php")" | cut -d. -f1,2)
php_installed_array+=("php")
fi
# Map user-requested version to the correct brew formula name
if [[ "$1" == "$php_main_ver" ]]; then
php_version="php"
else
php_version="php@$1"
fi
php_opt_path="$brew_prefix\/opt\/"
php5_module="php5_module"
apache_php5_lib_path="\/lib\/httpd\/modules\/libphp5.so"
php7_module="php7_module"
apache_php7_lib_path="\/lib\/httpd\/modules\/libphp7.so"
php8_module="php_module"
apache_php8_lib_path="\/lib\/httpd\/modules\/libphp.so"
native_osx_php_apache_module="LoadModule php5_module libexec\/apache2\/libphp5.so"
php_module="$php5_module"
apache_php_lib_path="$apache_php5_lib_path"
# Has the user submitted a version required
if [[ -z "$1" ]]
then
echo "usage: sphp version [-s|-s=*] [-c=*]"; echo;
_versions=""
for _f in "${php_installed_array[@]}"; do
[[ "$_f" == "php" ]] && _versions+="$php_main_ver " || _versions+="${_f#php@} "
done
echo " version one of: $_versions";
echo " -s skip change of mod_php on apache";
echo " -s=* skip change of mod_php on apache or valet restart i.e (apache|valet,apache|valet)";
echo " -c=* switch a specific config (apache|valet,apache|valet"; echo;
exit
fi
# Validate version format
if [[ ! "$1" =~ ^[0-9]+\.[0-9]+$ ]]; then
echo "Error: Invalid version format '$1'. Expected: X.Y (e.g., 8.3)"
exit 1
fi
if [[ "$php_version" == "php" ]]; then
php_version_num=$(echo "$php_main_ver" | sed 's/\.//')
else
php_version_num=$(echo "$php_version" | sed 's/^php@//' | sed 's/\.//')
fi
if [ "$php_version_num" -ge 80 ]; then
php_module="$php8_module"
apache_php_lib_path="$apache_php8_lib_path"
elif [ "$php_version_num" -ge 70 ]; then
php_module="$php7_module"
apache_php_lib_path="$apache_php7_lib_path"
fi
apache_change=1
apache_conf_path="$brew_prefix_raw/etc/httpd/httpd.conf"
apache_php_mod_path="$php_opt_path$php_version$apache_php_lib_path"
valet_restart=0
# Check if valet is already install
hash valet 2>/dev/null && valet_installed=1 || valet_installed=0
POSITIONAL=()
# Check for skip & change flag
while [[ $# -gt 0 ]]; do
key="$1"
case "$key" in
# This is a flag type option. Will catch either -s or --skip
-s|-s=*|--skip=*)
if [[ "${1#*=}" == "-s" || "${1#*=}" == *"apache"* ]]; then
apache_change=0
elif [ "${1#*=}" == "valet" ]; then
valet_restart=0
fi
;;
# This is a flag type option. Will catch either -c or --change
-c=*|--change=*)
[[ "$1" == *"apache"* ]] && apache_change=1 || apache_change=0
[[ "$1" == *"valet"* ]] && valet_restart=1 || valet_restart=0
;;
*)
POSITIONAL+=("$1") # save it in an array for later
;;
esac
# Shift after checking all the cases to get the next option
shift
done
# Check that the requested version is installed
if [[ " ${php_installed_array[*]} " == *" $php_version "* ]]
then
# Stop valet service
if [[ ($valet_installed -eq 1) && ($valet_restart -eq 1) ]]; then
echo "Stop Valet service";
valet stop;
fi
# Detect current PHP for rollback on failure/interruption
original_php=""
if command -v php &>/dev/null; then
current_ver=$(php -r 'echo PHP_MAJOR_VERSION . "." . PHP_MINOR_VERSION;' 2>/dev/null)
if [[ -n "$current_ver" ]]; then
if [[ "$current_ver" == "$php_main_ver" ]]; then
original_php="php"
else
original_php="php@$current_ver"
fi
fi
fi
rollback_php() {
if [[ -n "$original_php" ]]; then
echo ""
echo "Interrupted — re-linking $original_php"
brew link --force "$original_php" &>/dev/null
fi
}
trap rollback_php INT TERM
# Switch Shell
echo "Switching to $php_version"
echo "Switching your shell"
for i in "${php_installed_array[@]}"
do
brew unlink "$i"
done
if ! brew link --force "$php_version"; then
echo "Error: Failed to link $php_version"
rollback_php
exit 1
fi
# Switch apache
if [[ $apache_change -eq 1 ]]; then
echo "Switching your apache conf"
for j in "${php_installed_array[@]}"
do
loop_php_module="$php5_module"
loop_apache_php_lib_path="$apache_php5_lib_path"
if [[ "$j" == "php" ]]; then
loop_ver_num=$(echo "$php_main_ver" | sed 's/\.//')
else
loop_ver_num=$(echo "$j" | sed 's/^php@//' | sed 's/\.//')
fi
if [ "$loop_ver_num" -ge 80 ]; then
loop_php_module="$php8_module"
loop_apache_php_lib_path="$apache_php8_lib_path"
elif [ "$loop_ver_num" -ge 70 ]; then
loop_php_module="$php7_module"
loop_apache_php_lib_path="$apache_php7_lib_path"
fi
apache_module_string="LoadModule $loop_php_module $php_opt_path$j$loop_apache_php_lib_path"
comment_apache_module_string="#$apache_module_string"
# If apache module string within apache conf
if grep -q "$apache_module_string" "$apache_conf_path"; then
# If apache module string not commented out already
if ! grep -q "$comment_apache_module_string" "$apache_conf_path"; then
sed -i.bak "s/$apache_module_string/$comment_apache_module_string/g" "$apache_conf_path"
fi
# Else the string for the php module is not in the apache config then add it
else
sed -i.bak "/$native_osx_php_apache_module/a\\
$comment_apache_module_string\\
" "$apache_conf_path"
fi
done
sed -i.bak "s/\#LoadModule $php_module $apache_php_mod_path/LoadModule $php_module $apache_php_mod_path/g" "$apache_conf_path"
echo "Restarting apache"
brew services restart httpd
fi
# Switch valet
if [[ $valet_restart -eq 1 ]]; then
if [[ $valet_installed -eq 1 ]]; then
valet restart
else
echo "valet doesn't installed in your system, will skip restarting valet service";
fi
fi
echo ""
php -v
echo ""
trap - INT TERM
echo "All done!"
else
echo "Sorry, but $php_version is not installed via brew. Install by running: brew install $php_version"
fi