-
Notifications
You must be signed in to change notification settings - Fork 3
/
switch-php
74 lines (61 loc) · 2.26 KB
/
switch-php
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
#!/bin/bash
# Check if command was ran as root.
if [[ $(id -u) -eq 0 ]]; then
echo "The command \"sphp\" should not be executed as root or via sudo directly."
echo "When a service requires root access, you will be prompted for a password as needed."
exit 1
fi
# Usage
if [ $# -ne 1 ]; then
echo "Usage: sphp [phpversion]"
echo "Versions installed:"
brew list | grep '^php[0-9]\{2,\}$' | grep -o -E '[0-9]+' | while read -r line ; do
echo " - phpversion: $line"
done
exit 1
fi
currentversion="`php -r \"error_reporting(0); echo str_replace('.', '', substr(phpversion(), 0, 3));\"`"
newversion="$1"
majorOld=${currentversion:0:1}
minorOld=${currentversion:1:1}
majorNew=${newversion:0:1}
minorNew=${newversion:1:1}
brew list php$newversion 2> /dev/null > /dev/null
if [ $? -eq 0 ]; then
if [ "${newversion}" == "${currentversion}" ]; then
echo "PHP version [${newversion}] is already being used"
exit 1
fi
echo "PHP version [$newversion] found"
echo "Switching from [php${currentversion}] to [php${newversion}] ... "
printf "Unlink php${currentversion} ... "
brew unlink php$currentversion 2> /dev/null > /dev/null
printf "[OK] and "
printf "Link php${newversion} ... "
brew link php$newversion 2> /dev/null > /dev/null
printf "[OK]\n"
apache_http_conf_file=`httpd -V | grep -i server_config_file | cut -d '"' -f 2`
printf "Updating Apache2.4 Configuration ${apache_http_conf_file} ... "
# unload module of current version
sed -i -e "s/LoadModule php${majorOld}_module/#LoadModule php${majorOld}_module/g" $apache_http_conf_file
# unload module of old version
sed -i -e "s/#LoadModule php${majorNew}_module/LoadModule php${majorNew}_module/g" $apache_http_conf_file
printf "[OK]\n"
# check Apache2 config
result="`apachectl configtest 2>&1`"
if [ "${result}" == "Syntax OK" ]; then
printf "Restarting Apache2.4 ... "
sudo apachectl -k restart
echo "[OK]"
php -v
else
echo "Httpd Configuration Failed. Run 'apachectl configtest' to see details"
echo "1. Please check PHP version 'php -v'"
echo "2. Please check ${apache_http_conf_file} near by LoadModule ..."
fi
else
echo "PHP version $majorNew.$minorNew was not found."
echo "Try \`brew install php${newversion}\` first."
exit 1
fi
echo "DONE!"