From cd3385d28bdd515fa21634c0b4ee9a9a88b2b007 Mon Sep 17 00:00:00 2001 From: Salah Alkhwlani Date: Sat, 14 Oct 2017 10:30:47 +0300 Subject: [PATCH 1/2] add support valet config change & add support php 72 this PR to add support auto change `laravel/valet` config when switch php version --- .travis/clean.sh | 2 +- .travis/install.sh | 2 ++ .travis/test.sh | 2 +- README.md | 32 +++++++++++++++-- phpswitch.sh | 89 ++++++++++++++++++++++++++++++++++------------ 5 files changed, 99 insertions(+), 28 deletions(-) diff --git a/.travis/clean.sh b/.travis/clean.sh index 91311c0..432ac4b 100755 --- a/.travis/clean.sh +++ b/.travis/clean.sh @@ -1,6 +1,6 @@ #!/bin/bash -for version in 53 54 55 56 70 71; +for version in 53 54 55 56 70 71 72; do brew unlink php$version; done diff --git a/.travis/install.sh b/.travis/install.sh index 6519520..1c4d7af 100755 --- a/.travis/install.sh +++ b/.travis/install.sh @@ -17,6 +17,8 @@ if [[ $TRAVIS_OS_NAME == 'osx' ]]; then brew unlink php70 brew install php71 brew unlink php71 + brew install php72 + brew unlink php72 echo 'Installed all PHP versions.' fi \ No newline at end of file diff --git a/.travis/test.sh b/.travis/test.sh index de17503..2812ef8 100755 --- a/.travis/test.sh +++ b/.travis/test.sh @@ -1,7 +1,7 @@ #!/bin/bash error=0 -for version in 53 54 55 56 70 71 +for version in 53 54 55 56 70 71 72 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/\.//') diff --git a/README.md b/README.md index d3b5ddb..b1aed5d 100644 --- a/README.md +++ b/README.md @@ -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.3, 5.4, 5.5, 5.6, 7.0, 7.1 and 72 only. Your Apache config must have native osx PHP module commented out. ```sh @@ -23,6 +23,7 @@ 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 php7_module /usr/local/opt/php72/libexec/apache2/libphp7.so ``` Version @@ -36,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 ---- diff --git a/phpswitch.sh b/phpswitch.sh index 979c3b6..307a31c 100644 --- a/phpswitch.sh +++ b/phpswitch.sh @@ -4,8 +4,9 @@ # Twitter: @p_cook 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=("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\/" @@ -18,14 +19,6 @@ native_osx_php_apache_module="LoadModule php5_module libexec\/apache2\/libphp5.s 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" -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" ]] @@ -36,19 +29,44 @@ then exit 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 +if [ $(echo "$php_version" | sed 's/^php//') -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" + +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 # What versions of php are installed via brew @@ -60,12 +78,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" @@ -111,6 +143,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" From cc033fcf68050c5368d0fcb586e77e3bd2da3d20 Mon Sep 17 00:00:00 2001 From: Salah Alkhwlani Date: Sat, 14 Oct 2017 10:54:42 +0300 Subject: [PATCH 2/2] update usage information --- phpswitch.sh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/phpswitch.sh b/phpswitch.sh index 307a31c..200034a 100644 --- a/phpswitch.sh +++ b/phpswitch.sh @@ -23,9 +23,11 @@ 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]"; echo; + 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; + 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