Skip to content

Commit

Permalink
Merge branch 'yemenifree-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
philcook committed Nov 27, 2017
2 parents c6b76b4 + 428dc49 commit 6fe9167
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 24 deletions.
31 changes: 28 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
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, 7.1, and 7.2 only.
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, 7.1 and 7.2 only.

Your Apache config must have native osx PHP module commented out.
```sh
Expand Down Expand Up @@ -37,15 +37,40 @@ Installation
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 **56** exists, please replace with syntax of **53**, **54**, **55**, **56**, **70** or **71** or **72** depending on which version is required.
```sh
brew-php-switcher 56
```

> 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 56 -s

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

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

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

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

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

License
----
Expand Down
87 changes: 66 additions & 21 deletions phpswitch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ brew_prefix=$(brew --prefix | sed 's#/#\\\/#g')

brew_array=("53","54","55","56","70","71","72")
php_array=("php53" "php54" "php55" "php56" "php70" "php71" "php72")
valet_support_php_version_array=("php56" "php70" "php71" "php72")
php_installed_array=()
php_version="php$1"
php_opt_path="$brew_prefix\/opt\/"
Expand All @@ -18,6 +19,18 @@ native_osx_php_apache_module="LoadModule php5_module libexec\/apache2\/libphp5.s

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: 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//') -ge 70 ]; then
php_module="$php7_module"
apache_php_lib_path="$apache_php7_lib_path"
Expand All @@ -27,28 +40,35 @@ 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 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
# 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

# What versions of php are installed via brew
Expand All @@ -60,12 +80,26 @@ for i in ${php_array[*]}
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

# 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"
Expand Down Expand Up @@ -111,6 +145,17 @@ $comment_apache_module_string\\
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"
Expand Down

0 comments on commit 6fe9167

Please sign in to comment.