├── LICENSE ├── README.md ├── bug-report.sh ├── changelog.md └── php-version.sh /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 Wil Moore III 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # simple PHP version switching 2 | 3 | **php-version** exposes a `php-version` command allowing developers to switch between versions of PHP. 4 | 5 | ![](https://i.cloudup.com/Rl7FXze6ra.png) 6 | 7 | 8 | ## This _IS_ for you if 9 | 10 | - You are not satisifed with heavy handed *AMP or PPA-based installers. 11 | - You [use multiple][homebrew-php] [versions][php-build] of PHP on Linux or Mac. 12 | - You download [pre-compiled PHP binaries for Windows][windows-bin] (**NOTE**: this is a [bash] script so you'll need [WSL]). 13 | - You want to run your automated tests against multiple PHP versions. 14 | - You are a developer that works on a variety of PHP projects each requiring different versions of PHP. 15 | - You want to work on the latest PHP, but expect to support prior work that was done on older PHP versions. 16 | 17 | 18 | ## This is _NOT_ for you if 19 | 20 | - You are content with heavy handed *AMP installers. 21 | - You are provisioning a production server so you only need a single PHP install. 22 | - You **NEVER** work on more than one PHP project at a time. 23 | - You don't plan on supporting prior work that was done on other PHP versions. 24 | 25 | 26 | ## Rationale 27 | 28 | **php-version** attempts to stick to the classic UNIX notion that tools should do one thing well. 29 | 30 | > While there are [smart](https://github.com/c9s/phpbrew) [alternative](https://github.com/CHH/phpenv) 31 | > [tools](https://sourceforge.net/p/phpfarm/wiki/Home/) that attempt to [solve](https://github.com/convissor/php_version_solution) 32 | > this problem, [none](https://www.gnu.org/s/stow/) of the tools I've found were simple enough for me. 33 | 34 | 35 | ## Features 36 | 37 | - [Homebrew installed PHP versions][homebrew-php] are picked up automatically. 38 | - PHP versions installed [into `~/.phps`][build-php-vers] are picked up automatically. 39 | - PHP versions listed in the `$PHP_VERSIONS` shell variable are picked up automatically. 40 | - **snap versioning**: Use a partial version number (i.e. `php-version 5`) to automatically use the latest 5.x version. 41 | - **per version `php.ini`**: we `export PHPRC` if a version-specific `php.ini` exists. 42 | - **configurable**: `php-version --help` for details. 43 | - **[bash], [zsh], and [fish]** shells actively supported; though care has been taken such that other shells are _likely_ to work as well. 44 | - **tiny**: less than 200 LOC; a single function sourced via your shell's initialization file. 45 | 46 | 47 | ## Non-Features 48 | 49 | - no [shims][], sub-shells, symlinks or `cd` [hooks][]. 50 | - we won't leave files and symlinks all over the place. 51 | - does not attempt to manage Apache, MySQL, etc. 52 | - does not attempt to compile, build, or install PHP. 53 | - does not attempt to support OS package manager installed (i.e. ppa, etc.) PHP versions. 54 | 55 | 56 | ## Usage Examples 57 | 58 | ### Switch to a specific PHP version 59 | 60 | % php-version 61 | 62 | ### List installed and active (*) PHP version(s) 63 | 64 | % php-version 65 | 5.3.9 66 | 5.3.10 67 | 5.4.0RC8 68 | 5.4.0RC6 69 | 5.4.0 70 | * 5.4.8 71 | 72 | 73 | ## Install 74 | 75 | **[homebrew](https://brew.sh/)** (recommended for OSX users) 76 | 77 | % brew tap wilmoore/formulae 78 | % brew install php-version 79 | 80 | **cURL** (for non-OSX users or those that prefer not to use `homebrew`): 81 | 82 | % mkdir -p $HOME/local/php-version # or your place of choice 83 | % cd !$ 84 | % curl -# -L https://github.com/wilmoore/php-version/tarball/master | tar -xz --strip 1 85 | 86 | [Alternative (i.e. non-Homebrew) installation methods][opt-install] are documented on the wiki. 87 | 88 | 89 | ## Setup 90 | 91 | > Add one of the following to your shell's initialization file: 92 | 93 | # Homebrew (recommended) 94 | source $(brew --prefix php-version)/php-version.sh 95 | php-version 5 96 | 97 | # non-Homebrew 98 | source $HOME/local/php-version/php-version.sh # or your place of choice 99 | php-version 5 100 | 101 | Type `php-version --help` for more configuration options such as how to add extra PHP installation paths or `php-config --version` to find out which `php` version is active. 102 | 103 | > If you have PHP versions in multiple directories, you can list them in the environment variable `PHP_VERSIONS` separated by spaces as depicted below: 104 | 105 | export PHP_VERSIONS="$HOME/local/php $HOME/php/versions" 106 | 107 | **NOTE**: do this before you source `php-version.sh`: 108 | 109 | ## Deactivate / Uninstall 110 | 111 | 1. Remove [setup](https://github.com/wilmoore/php-version#setup) configuration. 112 | 113 | 2. Enter one of the following commands listed below to remove associated files. 114 | 115 | # Homebrew (recommended) 116 | % brew remove --force php-version 117 | 118 | # non-Homebrew 119 | % rm -rf $HOME/local/php-version 120 | 121 | 122 | ## Having Issues? 123 | 124 | 1. Copy the bug report output to your clipboard (`pbcopy` works on Mac OSX; use your OS equivalent) 125 | 126 | % cd /tmp 127 | % git clone https://github.com/wilmoore/php-version.git 128 | % source php-version/bug-report.sh | pbcopy 129 | 130 | 2. File an [issue](https://github.com/wilmoore/php-version/issues?state=open). 131 | 132 | 133 | ## More Info 134 | 135 | - [Building PHP Versions][build-php-vers] 136 | - [Exploring PHP][exploring] 137 | - [Troubleshooting][trouble] 138 | 139 | 140 | ## Contributors 141 | > https://github.com/wilmoore/php-version/graphs/contributors 142 | 143 | 144 | ## Alternatives 145 | 146 | - [brew-php-switcher](https://github.com/philcook/brew-php-switcher) 147 | - [phpbrew](https://github.com/c9s/phpbrew) 148 | - [phpenv](https://github.com/CHH/phpenv) 149 | - [phpenv](https://github.com/humanshell/phpenv) 150 | - [php_version_solution](https://github.com/convissor/php_version_solution) 151 | - [phpfarm](https://sourceforge.net/p/phpfarm/wiki/Home/) 152 | - [GNU Stow](https://www.gnu.org/s/stow/) 153 | - [phpswitch](https://github.com/jubianchi/phpswitch) 154 | 155 | 156 | ## Inspiration 157 | 158 | - [n](https://github.com/visionmedia/n) 159 | - [nvm](https://github.com/creationix/nvm) 160 | - [rbenv](https://github.com/sstephenson/rbenv) 161 | - [rbfu](https://github.com/hmans/rbfu) 162 | - [ry](https://github.com/jayferd/ry) 163 | 164 | 165 | ## LICENSE 166 | 167 | MIT 168 | 169 | 170 | 171 | [bash]: https://www.gnu.org/software/bash/ 172 | [build-php-vers]: https://github.com/wilmoore/php-version/wiki/Building-PHP-Versions 173 | [exploring]: https://github.com/wilmoore/php-version/wiki/Exploring-PHP 174 | [fish]: https://fishshell.com/ 175 | [homebrew-php]: https://github.com/josegonzalez/homebrew-php 176 | [hooks]: https://rvm.io/workflow/hooks 177 | [manual-build]: https://github.com/wilmoore/php-version#compilation-recommendations 178 | [opt-install]: https://github.com/wilmoore/php-version/wiki/Installing 179 | [php-build]: https://github.com/CHH/php-build 180 | [shims]: https://github.com/sstephenson/rbenv#understanding-shims 181 | [trouble]: https://github.com/wilmoore/php-version/wiki/Troubleshooting 182 | [windows-bin]: http://windows.php.net/download 183 | [windows-port]: https://github.com/wilmoore/php-version/issues/2 184 | [WSL]: https://docs.microsoft.com/en-us/windows/wsl/install-win10 185 | [zsh]: https://www.zsh.org/ 186 | -------------------------------------------------------------------------------- /bug-report.sh: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # bug-report: collect environment information for bug reports. 3 | # % source bug-report.sh 4 | ################################################################################ 5 | 6 | cat <<-EOF 7 | ## System 8 | 9 | $(uname -a) 10 | 11 | ## Script 12 | 13 | TYPE: $(command -v php-version 2>/dev/null || echo 'NOT FOUND') 14 | VERSION: $(php-version --version 2>/dev/null || echo 'NOT FOUND') 15 | 16 | ## Environment 17 | 18 | SHELL: $(echo $SHELL) 19 | PATH: $(echo $PATH) 20 | PHPS: $(find ~/.phps -maxdepth 1 -mindepth 1 -type d 2>/dev/null) 21 | PHP_VERSIONS: $(echo $PHP_VERSIONS) 22 | 23 | ## Homebrew 24 | 25 | VERSION: $(test -n `command -v brew` && brew --version) 26 | PATH: $(command -v brew) 27 | PHPS: $(find $(brew --cellar) -maxdepth 1 -type d | grep -E 'php[0-9]+$') 28 | 29 | ## PHP 30 | 31 | VERSION: $(php-config --version) 32 | 33 | ## INI 34 | 35 | $(php --ini | sed 's/^/ /') 36 | 37 | EOF 38 | -------------------------------------------------------------------------------- /changelog.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | All notable changes to this project will be documented in this file (keepachangelog.com). 3 | 4 | ## 0.13.0 - 2019-03-11 5 | ### Added 6 | - Added a changelog. 7 | - Added a note for windows users to get WSL (#74) 8 | - Added support for v7 PHP () 9 | ### Changed 10 | - Updated installation instructions to point to this tap: `wilmoore/formulae` (#71, #67) 11 | 12 | ## 0.12.1 - 2015-08-05 13 | ### Changed 14 | - Replace ~ with $HOME in PHP_VERSIONS example. 15 | -------------------------------------------------------------------------------- /php-version.sh: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # php-version: function allowing one to switch between PHP versions 3 | ################################################################################ 4 | 5 | function php-version { 6 | local PROGRAM_APPNAME='php-version' 7 | local PROGRAM_VERSION=0.13.0 8 | local PROGRAM_DISPLAY_VERSION="$PROGRAM_APPNAME v$PROGRAM_VERSION" 9 | 10 | # colors 11 | COLOR_NORMAL=$(tput sgr0) 12 | COLOR_REVERSE=$(tput smso) 13 | 14 | # target version 15 | local _TARGET_VERSION=$1 16 | 17 | # PHP installation paths 18 | local _PHP_VERSIONS="" 19 | 20 | # add ~/.phps if it exists (default) 21 | if [[ -d $HOME/.phps ]]; then 22 | export _PHP_VERSIONS="$_PHP_VERSIONS $HOME/.phps" 23 | fi 24 | 25 | # add default Homebrew directories if brew is installed 26 | if [[ -n $(command -v brew) ]]; then 27 | export _PHP_VERSIONS="$_PHP_VERSIONS $(echo $(find $(brew --cellar) -maxdepth 1 -type d | grep -E 'php$|php@[0-9\.]*$'))" 28 | fi 29 | 30 | # add extra directories if configured 31 | if [[ -n $PHP_VERSIONS ]]; then 32 | export _PHP_VERSIONS="$_PHP_VERSIONS $PHP_VERSIONS" 33 | fi 34 | 35 | # export the space-separated string as array 36 | _PHP_REPOSITORIES=() 37 | for _PHP_VERSION in $(echo $_PHP_VERSIONS | tr " " "\n"); do 38 | _PHP_REPOSITORIES=("${_PHP_REPOSITORIES[@]}" $_PHP_VERSION) 39 | done 40 | 41 | # unset the string 42 | _PHP_VERSIONS= 43 | 44 | # argument parsing 45 | case "$1" in 46 | 47 | -h|--help|-u|--usage) 48 | 49 | echo $PROGRAM_DISPLAY_VERSION 50 | 51 | cat <<-USAGE 52 | 53 | Usage: 54 | $PROGRAM_APPNAME --help Show this message 55 | $PROGRAM_APPNAME --version Print the version 56 | $PROGRAM_APPNAME Modify PATH to use 57 | $PROGRAM_APPNAME Show all available versions and denote the currently activated version 58 | 59 | Example: 60 | $PROGRAM_APPNAME 5 Activate the latest available 5.x version 61 | $PROGRAM_APPNAME 5.5 Activate the latest available 5.5.x version 62 | $PROGRAM_APPNAME 5.5.13 Activate version 5.5.13 specifically 63 | 64 | Configuration Options: 65 | https://github.com/wilmoore/php-version#setup 66 | 67 | Uninstall: 68 | https://github.com/wilmoore/php-version#deactivate--uninstall 69 | 70 | USAGE 71 | 72 | return 0 73 | ;; 74 | 75 | -v|--version) 76 | 77 | echo $PROGRAM_DISPLAY_VERSION 78 | 79 | return 0 80 | ;; 81 | 82 | -*) 83 | 84 | printf "\e[0;31m%s: %s: unrecognized option\e[0m\n\n" $0 $1 >&2 85 | php-version --help >&2 86 | 87 | return 1 88 | ;; 89 | 90 | "") 91 | 92 | # bail-out if _PHP_REPOSITORIES is an empty array 93 | if [ ${#_PHP_REPOSITORIES[@]} -eq 0 ]; then 94 | echo 'Sorry, but you do not seem to have any PHP versions installed.' >&2 95 | echo 'See https://github.com/wilmoore/php-version#install for assistance.' >&2 96 | return 1 97 | fi 98 | 99 | # Loop through all repositories and get every single php-version 100 | _PHP_VERSIONS=() 101 | for _PHP_REPOSITORY in "${_PHP_REPOSITORIES[@]}"; do 102 | for _dir in $(find -H $(echo $_PHP_REPOSITORY) -maxdepth 1 -mindepth 1 -type d 2>/dev/null); do 103 | _PHP_VERSIONS=("${_PHP_VERSIONS[@]}" "$($_dir/bin/php-config --version 2>/dev/null)") 104 | done 105 | done 106 | 107 | _PHP_VERSIONS=$(IFS=$'\n'; echo "${_PHP_VERSIONS[*]}" | sort -r -t . -k 1,1n -k 2,2n -k 3,3n) 108 | 109 | for version in $(echo $_PHP_VERSIONS | tr " " "\n"); do 110 | local selected=" " 111 | local color=$COLOR_NORMAL 112 | 113 | if [[ "$version" == "$(php-config --version 2>/dev/null)" ]]; then 114 | local selected="*" 115 | local color=$COLOR_REVERSE 116 | fi 117 | 118 | printf "${color}%s %s${COLOR_NORMAL}\n" "$selected" "$version" 119 | done 120 | 121 | return 0 122 | ;; 123 | 124 | esac 125 | 126 | # locate selected PHP version 127 | for _PHP_REPOSITORY in "${_PHP_REPOSITORIES[@]}"; do 128 | if [[ -d "$_PHP_REPOSITORY/$_TARGET_VERSION" && -z $_PHP_ROOT ]]; then 129 | local _PHP_ROOT=$_PHP_REPOSITORY/$_TARGET_VERSION 130 | break; 131 | fi 132 | done 133 | 134 | # try a fuzzy match since we were unable to find a PHP matching given version 135 | if [[ -z $_PHP_ROOT ]]; then 136 | _TARGET_VERSION_FUZZY=() 137 | 138 | for _PHP_REPOSITORY in "${_PHP_REPOSITORIES[@]}"; do 139 | for _dir in $(find -H $_PHP_REPOSITORY -maxdepth 1 -mindepth 1 -type d 2>/dev/null); do 140 | _TARGET_VERSION_FUZZY=("${_TARGET_VERSION_FUZZY[@]}" "$($_dir/bin/php-config --version 2>/dev/null)") 141 | done 142 | done 143 | 144 | _TARGET_VERSION_FUZZY=$(IFS=$'\n'; echo "${_TARGET_VERSION_FUZZY[*]}" | sort -r -t . -k 1,1n -k 2,2n -k 3,3n | grep -E "^$_TARGET_VERSION" 2>/dev/null | tail -1) 145 | 146 | for _PHP_REPOSITORY in "${_PHP_REPOSITORIES[@]}"; do 147 | for _dir in $(find -H $_PHP_REPOSITORY -maxdepth 1 -mindepth 1 -type d 2>/dev/null); do 148 | _PHP_VERSION="$($_dir/bin/php-config --version 2>/dev/null)" 149 | if [[ -n "$_TARGET_VERSION_FUZZY" && "$_PHP_VERSION" == "$_TARGET_VERSION_FUZZY" ]]; then 150 | local _PHP_ROOT=$_dir 151 | break; 152 | fi 153 | done 154 | done 155 | fi 156 | 157 | # bail-out if we were unable to find a PHP matching given version 158 | if [[ -z $_PHP_ROOT ]]; then 159 | echo "Sorry, but $PROGRAM_APPNAME was unable to find version '$1' under '${_PHP_PATHS[@]}'." >&2 160 | return 1 161 | fi 162 | 163 | # export current paths 164 | export PHPRC="" 165 | [[ -f $_PHP_ROOT/etc/php.ini ]] && export PHPRC=$_PHP_ROOT/etc/php.ini 166 | [[ -d $_PHP_ROOT/bin ]] && export PATH="$_PHP_ROOT/bin:$PATH" 167 | [[ -d $_PHP_ROOT/sbin ]] && export PATH="$_PHP_ROOT/sbin:$PATH" 168 | 169 | # use configured manpath if it exists, otherwise, use `$_PHP_ROOT/share/man` 170 | local _MANPATH=$(php-config --man-dir) 171 | [[ -z $_MANPATH ]] && _MANPATH=$_PHP_ROOT/share/man 172 | [[ -d $_MANPATH ]] && export MANPATH="$_MANPATH:$MANPATH" 173 | 174 | hash -r 175 | } 176 | --------------------------------------------------------------------------------