├── .gitignore ├── README.md └── sphp /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PHP version switcher for OSX 2 | 3 | If you're on OSX with PHP installed via Brew, you may be looking for an easy way to switch between PHP versions (5.6, 7.0, 7.1, 7.2 etc). Well, this package is it. 4 | 5 | Installation: 6 | ``` 7 | git clone git@github.com:jschaedl/sphp-osx.git 8 | ``` 9 | 10 | Add `/usr/local/bin` to your `$PATH`. If you use the Bash shell, you can do this by running this command: 11 | ``` 12 | echo 'export PATH="/usr/local/bin:$PATH"' >> $HOME/.bashrc 13 | ``` 14 | You may need to restart your shell for this to take effect. 15 | 16 | Usage: 17 | ``` 18 | ./sphp-osx/sphp 5.6 19 | ./sphp-osx/sphp 7.0 20 | ./sphp-osx/sphp 7.1 21 | ./sphp-osx/sphp 7.2 22 | ./sphp-osx/sphp 7.3 23 | ./sphp-osx/sphp 8.0 24 | ``` 25 | 26 | ## Troubleshooting 27 | 28 | ### PHP doesn't work anymore when I switch versions in Bash 29 | 30 | Bash has an executable path cache. It saves the paths of executables it has previously run. If Brew changes the path to the `php` executable, you may encounter 31 | this error. You have 2 options: 32 | - Add `set +h` in your `~/.bashrc` or `~/.profile` file. This will disable the executable path cache in Bash. However, this might slow down your Bash. 33 | - After you use `sphp`, enter the command `hash -r` in your shell. This will clear the executable path cache only once. 34 | 35 | ## Contributors 36 | 37 | * [@alefi87](https://github.com/alefi87) 38 | * [@conradkleinespel](https://github.com/conradkleinespel) 39 | * [@michaelburtonray](https://github.com/michaelburtonray) 40 | * [@sebastienbarre](https://github.com/sebastienbarre) 41 | * [@uzyn](https://github.com/uzyn) 42 | * [@w00fz](https://github.com/w00fz) 43 | * [@markcarver](https://github.com/markcarver) 44 | * [@maztch](https://github.com/maztch) 45 | * [@@sgotre](https://github.com/sgotre) 46 | * [@anararo](https://github.com/anararo) 47 | -------------------------------------------------------------------------------- /sphp: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Creator: Phil Cook 3 | # Email: phil@phil-cook.com 4 | # Twitter: @p_cook 5 | brew_prefix=$(brew --prefix | sed 's#/#\\\/#g') 6 | 7 | brew_array=("5.6","7.0","7.1","7.2","7.3","7.4","8.0","8.1","8.2") 8 | php_array=("php@5.6" "php@7.0" "php@7.1" "php@7.2" "php@7.3" "php@7.4" "php@8.0" "php@8.1" "php@8.2") 9 | valet_support_php_version_array=("php@5.6" "php@7.0" "php@7.1" "php@7.2" "php@7.3" "php@7.4" "php@8.0" "php@8.1" "php@8.2") 10 | php_installed_array=() 11 | php_version="php@$1" 12 | php_opt_path="$brew_prefix\/opt\/" 13 | 14 | php5_module="php5_module" 15 | apache_php5_lib_path="\/lib\/httpd\/modules\/libphp5.so" 16 | php7_module="php7_module" 17 | apache_php7_lib_path="\/lib\/httpd\/modules\/libphp7.so" 18 | php8_module="php_module" 19 | apache_php8_lib_path="\/lib\/httpd\/modules\/libphp.so" 20 | native_osx_php_apache_module="LoadModule php5_module libexec\/apache2\/libphp5.so" 21 | 22 | php_module="$php5_module" 23 | apache_php_lib_path="$apache_php5_lib_path" 24 | 25 | # Has the user submitted a version required 26 | if [[ -z "$1" ]] 27 | then 28 | echo "usage: sphp version [-s|-s=*] [-c=*]"; echo; 29 | echo " version one of:" ${brew_array[@]}; 30 | echo " -s skip change of mod_php on apache"; 31 | echo " -s=* skip change of mod_php on apache or valet restart i.e (apache|valet,apache|valet)"; 32 | echo " -c=* switch a specific config (apache|valet,apache|valet"; echo; 33 | exit 34 | fi 35 | 36 | if [ $(echo "$php_version" | sed 's/^php@//' | sed 's/\.//') -ge 70 ]; then 37 | php_module="$php7_module" 38 | apache_php_lib_path="$apache_php7_lib_path" 39 | fi 40 | 41 | apache_change=1 42 | apache_conf_path="/usr/local/etc/httpd/httpd.conf" 43 | apache_php_mod_path="$php_opt_path$php_version$apache_php_lib_path" 44 | 45 | valet_restart=0 46 | # Check if valet is already install 47 | hash valet 2>/dev/null && valet_installed=1 || valet_installed=0 48 | 49 | POSITIONAL=() 50 | 51 | # Check for skip & change flag 52 | while [[ $# -gt 0 ]]; do 53 | key="$1" 54 | case "$key" in 55 | # This is a flag type option. Will catch either -s or --skip 56 | -s|-s=*|--skip=*) 57 | if [[ "${1#*=}" == "-s" || "${1#*=}" == *"apache"* ]]; then 58 | apache_change=0 59 | elif [ "${1#*=}" == "valet" ]; then 60 | valet_restart=0 61 | fi 62 | ;; 63 | # This is a flag type option. Will catch either -c or --change 64 | -c=*|--change=*) 65 | [[ "$1" == *"apache"* ]] && apache_change=1 || apache_change=0 66 | [[ "$1" == *"valet"* ]] && valet_restart=1 || valet_restart=0 67 | ;; 68 | *) 69 | POSITIONAL+=("$1") # save it in an array for later 70 | ;; 71 | esac 72 | # Shift after checking all the cases to get the next option 73 | shift 74 | done 75 | 76 | # What versions of php are installed via brew 77 | for i in ${php_array[*]} 78 | do 79 | if [[ -n "$(brew ls --versions "$i")" ]] 80 | then 81 | php_installed_array+=("$i") 82 | fi 83 | done 84 | 85 | # Check if php version support via valet 86 | if [[ (" ${valet_support_php_version_array[*]} " != *"$php_version"*) && ($valet_restart -eq 1) ]] 87 | then 88 | echo "Sorry, but $php_version is not support via valet"; 89 | exit; 90 | fi 91 | 92 | # Check that the requested version is supported 93 | if [[ " ${php_array[*]} " == *"$php_version"* ]] 94 | then 95 | # Check that the requested version is installed 96 | if [[ " ${php_installed_array[*]} " == *"$php_version"* ]] 97 | then 98 | 99 | # Stop valet service 100 | if [[ ($valet_installed -eq 1) && ($valet_restart -eq 1) ]]; then 101 | echo "Stop Valet service"; 102 | valet stop; 103 | fi 104 | 105 | # Switch Shell 106 | echo "Switching to $php_version" 107 | echo "Switching your shell" 108 | for i in ${php_installed_array[@]} 109 | do 110 | if [[ -n $(brew ls --versions $i) ]] 111 | then 112 | brew unlink $i 113 | fi 114 | done 115 | brew link --force "$php_version" 116 | 117 | # Switch apache 118 | if [[ $apache_change -eq 1 ]]; then 119 | echo "Switching your apache conf" 120 | 121 | for j in ${php_installed_array[@]} 122 | do 123 | loop_php_module="$php5_module" 124 | loop_apache_php_lib_path="$apache_php5_lib_path" 125 | if [ $(echo "$j" | sed 's/^php@//' | sed 's/\.//') -ge 70 ]; then 126 | loop_php_module="$php7_module" 127 | loop_apache_php_lib_path="$apache_php7_lib_path" 128 | fi 129 | apache_module_string="LoadModule $loop_php_module $php_opt_path$j$loop_apache_php_lib_path" 130 | comment_apache_module_string="#$apache_module_string" 131 | 132 | # If apache module string within apache conf 133 | if grep -q "$apache_module_string" "$apache_conf_path"; then 134 | # If apache module string not commented out already 135 | if ! grep -q "$comment_apache_module_string" "$apache_conf_path"; then 136 | sed -i.bak "s/$apache_module_string/$comment_apache_module_string/g" $apache_conf_path 137 | fi 138 | # Else the string for the php module is not in the apache config then add it 139 | else 140 | sed -i.bak "/$native_osx_php_apache_module/a\\ 141 | $comment_apache_module_string\\ 142 | " $apache_conf_path 143 | fi 144 | done 145 | sed -i.bak "s/\#LoadModule $php_module $apache_php_mod_path/LoadModule $php_module $apache_php_mod_path/g" $apache_conf_path 146 | echo "Restarting apache" 147 | brew services restart httpd 148 | fi 149 | 150 | 151 | # Switch valet 152 | if [[ $valet_restart -eq 1 ]]; then 153 | if [[ valet_installed -eq 1 ]]; then 154 | valet restart 155 | else 156 | echo "valet doesn't installed in your system, will skip restarting valet service"; 157 | fi 158 | fi 159 | 160 | echo "" 161 | php -v 162 | echo "" 163 | 164 | echo "All done!" 165 | else 166 | echo "Sorry, but $php_version is not installed via brew. Install by running: brew install $php_version" 167 | fi 168 | else 169 | echo "Unknown version of PHP. PHP Switcher can only handle arguments of:" ${brew_array[@]} 170 | fi 171 | --------------------------------------------------------------------------------