Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
philcook committed Nov 13, 2018
2 parents 877453c + eab0e6d commit 294edaa
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 124 deletions.
5 changes: 2 additions & 3 deletions .travis/clean.sh
@@ -1,6 +1,5 @@
#!/bin/bash

for version in 53 54 55 56 70 71;
do
brew unlink php$version;
for version in '5.6' '7.0' '7.1' '7.2'; do
brew unlink php@$version
done
25 changes: 10 additions & 15 deletions .travis/install.sh
Expand Up @@ -2,21 +2,16 @@

if [[ $TRAVIS_OS_NAME == 'osx' ]]; then
brew tap homebrew/dupes
brew tap homebrew/versions
brew tap homebrew/homebrew-php
brew update
brew install php53
brew unlink php53
brew install php54
brew unlink php54
brew install php55
brew unlink php55
brew install php56
brew unlink php56
brew install php70
brew unlink php70
brew install php71
brew unlink php71
brew install php@5.6
brew unlink php@5.6
brew install php@7.0
brew unlink php@7.0
brew install php@7.1
brew unlink php@7.1
brew install php@7.2
brew link --overwrite php@7.2
brew unlink php@7.2

echo 'Installed all PHP versions.'
fi
fi
10 changes: 4 additions & 6 deletions .travis/test.sh
@@ -1,17 +1,15 @@
#!/bin/bash

error=0
for version in 53 54 55 56 70 71
do
for version in '5.6' '7.0' '7.1' '7.2'; do
. `echo $(dirname $0)"/../phpswitch.sh"` $version -s > /dev/null
switched=$(php -v | grep -e '^PHP' | cut -d' ' -f2 | cut -d. -f1,2 | sed 's/\.//')
if [ $version -ne $switched ];
then
switched=$(php -v | grep -e '^PHP' | cut -d' ' -f2 | cut -d. -f1,2)
if [ "$version" != "$switched" ]; then
error=1
echo -n 'E'
fi
echo -n '.'
done
echo ''
echo

exit $error
45 changes: 34 additions & 11 deletions README.md
Expand Up @@ -8,7 +8,7 @@ If you support multiple products/projects that are built using either brand new
Caveats
-------

For users of OSX only who have installed PHP via [Homebrew] and for PHP version 5.3, 5.4, 5.5, 5.6, 7.0 and 7.1 only.
For users of OSX only who have installed PHP via [Homebrew] and for PHP version 5.6, 7.0, 7.1 and 7.2 only.

Your Apache config must have native osx PHP module commented out.
```sh
Expand All @@ -17,34 +17,57 @@ Your Apache config must have native osx PHP module commented out.

Brew PHP Switcher will automatically add the [Homebrew]'s PHP module location in the Apache config in the following format.
```sh
LoadModule php5_module /usr/local/opt/php53/libexec/apache2/libphp5.so
LoadModule php5_module /usr/local/opt/php54/libexec/apache2/libphp5.so
LoadModule php5_module /usr/local/opt/php55/libexec/apache2/libphp5.so
LoadModule php5_module /usr/local/opt/php56/libexec/apache2/libphp5.so
LoadModule php7_module /usr/local/opt/php70/libexec/apache2/libphp7.so
LoadModule php7_module /usr/local/opt/php71/libexec/apache2/libphp7.so
#LoadModule php5_module /usr/local/opt/php@5.6/lib/httpd/modules/libphp5.so
#LoadModule php7_module /usr/local/opt/php@7.0/lib/httpd/modules/libphp7.so
#LoadModule php7_module /usr/local/opt/php@7.1/lib/httpd/modules/libphp7.so
#LoadModule php7_module /usr/local/opt/php@7.2/lib/httpd/modules/libphp7.so
```

Version
----

1.5
2.1

Installation
--------------
```sh
brew install brew-php-switcher
```

Where **56** exists, please replace with syntax of **53**, **54**, **55**, **56**, **70** or **71** depending on which version is required.
Where **5.6** exists, please replace with syntax of **5.6**, **7.0**, **7.1**, or **7.2** depending on which version is required.
```sh
brew-php-switcher 56
brew-php-switcher 5.6
```

> by default will switch apache config
Options
--------------

-s Skips apache config switch
- `-s|-s=*` Skips apache & valet config switch for i.e

```sh
# skip apache only
brew-php-switcher 5.6 -s

# skip valet only
brew-php-switcher 5.6 -s=valet

# skip valet & apache
brew-php-switcher 5.6 -s=valet,apache
```
- `-c=*` switch a specific config for i.e

```sh
# switch valet config only
brew-php-switcher 5.6 -c=valet

# switch valet & apache config only
brew-php-switcher 5.6 -c=valet,apache

# switch apache config only
brew-php-switcher 5.6 -c=apache
```

License
----
Expand Down
1 change: 1 addition & 0 deletions _config.yml
@@ -0,0 +1 @@
theme: jekyll-theme-minimal
225 changes: 136 additions & 89 deletions phpswitch.sh
Expand Up @@ -2,119 +2,166 @@
# Creator: Phil Cook
# Email: phil@phil-cook.com
# Twitter: @p_cook
osx_major_version=$(sw_vers -productVersion | cut -d. -f1)
osx_minor_version=$(sw_vers -productVersion | cut -d. -f2)
osx_patch_version=$(sw_vers -productVersion | cut -d. -f3)
osx_version=$((${osx_major_version} * 10000 + ${osx_minor_version} * 100 + ${osx_patch_version}))

brew_prefix=$(brew --prefix | sed 's#/#\\\/#g')

brew_array=("53","54","55","56","70","71")
php_array=("php53" "php54" "php55" "php56" "php70" "php71")
brew_array=("5.6","7.0","7.1","7.2")
php_array=("php@5.6" "php@7.0" "php@7.1" "php@7.2")
valet_support_php_version_array=("php@5.6" "php@7.0" "php@7.1" "php@7.2")
php_installed_array=()
php_version="php$1"
php_version="php@$1"
php_opt_path="$brew_prefix\/opt\/"

php5_module="php5_module"
apache_php5_lib_path="\/libexec\/apache2\/libphp5.so"
apache_php5_lib_path="\/lib\/httpd\/modules\/libphp5.so"
php7_module="php7_module"
apache_php7_lib_path="\/libexec\/apache2\/libphp7.so"
native_osx_php_apache_module="LoadModule php5_module libexec\/apache2\/libphp5.so"
apache_php7_lib_path="\/lib\/httpd\/modules\/libphp7.so"

native_osx_php_apache_module="LoadModule ${php5_module} libexec\/apache2\/libphp5.so"
if [ ${osx_version} > 101300 ]; then
native_osx_php_apache_module="LoadModule ${php7_module} libexec\/apache2\/libphp7.so"
fi

php_module="$php5_module"
apache_php_lib_path="$apache_php5_lib_path"
if [[ $(echo "$php_version" | sed 's/^php//') -ge 70 ]]; then
php_module="$php7_module"
apache_php_lib_path="$apache_php7_lib_path"

# Has the user submitted a version required
if [[ -z "$1" ]]; then
echo "usage: brew-php-switcher version [-s|-s=*] [-c=*]"
echo
echo " version one of:" ${brew_array[@]}
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

if [[ $(echo "$php_version" | sed 's/^php@//' | sed 's/\.//') -ge 70 ]]; then
php_module="$php7_module"
apache_php_lib_path="$apache_php7_lib_path"
fi

apache_change=1
apache_conf_path="/etc/apache2/httpd.conf"
apache_php_mod_path="$php_opt_path$php_version$apache_php_lib_path"

# Has the user submitted a version required
if [[ -z "$1" ]]
then
echo "usage: brew-php-switcher version [-s]"; echo;
echo " version one of:" ${brew_array[@]};
echo " -s skip change of mod_php on apache"; echo;
exit
fi
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
;;

# Check for skip apache
while [[ ${2:0:1} = '-' ]] ; do
N=1
L=${#1}
while [[ $N -lt $L ]] ; do
case ${2:$N:1} in
's') apache_change=0 ;;
*) echo $USAGE
exit 1 ;;
esac
N=$(($N+1))
done
shift
# 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

# What versions of php are installed via brew
for i in ${php_array[*]}
do
if [[ -n "$(brew ls --versions "$i")" ]]
then
php_installed_array+=("$i")
fi
for i in ${php_array[*]}; do
if [[ -n "$(brew ls --versions "$i")" ]]; then
php_installed_array+=("$i")
fi
done

# Check if php version support via valet
if [[ (" ${valet_support_php_version_array[*]} " != *"$php_version"*) && ($valet_restart -eq 1) ]]; then
echo "Sorry, but $php_version is not support via valet"
exit
fi

# Check that the requested version is supported
if [[ " ${php_array[*]} " == *"$php_version"* ]]
then
# Check that the requested version is installed
if [[ " ${php_installed_array[*]} " == *"$php_version"* ]]
then
# Switch Shell
echo "Switching to $php_version"
echo "Switching your shell"
for i in ${php_installed_array[@]}
do
if [[ -n $(brew ls --versions $i) ]]
then
brew unlink $i
fi
done
brew link "$php_version"

# Switch apache
if [[ $apache_change -eq 1 ]]; then
echo "You will need sudo power from now on"
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 [ $(echo "$j" | sed 's/^php//') -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
sudo 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
sudo sed -i.bak "/$native_osx_php_apache_module/a\\
if [[ " ${php_array[*]} " == *"$php_version"* ]]; then
# 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

# Switch Shell
echo "Switching to $php_version"
echo "Switching your shell"
for i in ${php_installed_array[@]}; do
brew unlink $i
done
brew link --force "$php_version"

# Switch apache
if [[ $apache_change -eq 1 ]]; then
echo "You will need sudo power from now on"
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 [ $(echo "$j" | sed 's/^php@//' | sed 's/\.//') -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
sudo 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
sudo sed -i.bak "/$native_osx_php_apache_module/a\\
$comment_apache_module_string\\
" $apache_conf_path
fi
done
sudo 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"
sudo apachectl restart
fi
echo "All done!"
else
echo "Sorry, but $php_version is not installed via brew. Install by running: brew install $php_version"
fi
fi
done
sudo 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"
sudo apachectl restart
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 "All done!"
else
echo "Sorry, but $php_version is not installed via brew. Install by running: brew install $php_version"
fi
else
echo "Unknown version of PHP. PHP Switcher can only handle arguments of:" ${brew_array[@]}
echo "Unknown version of PHP. PHP Switcher can only handle arguments of:" ${brew_array[@]}
fi

0 comments on commit 294edaa

Please sign in to comment.