Created
September 14, 2018 17:53
-
-
Save robwozniak/862946d9fbbaaf3f77a3ed5a1d5968ed to your computer and use it in GitHub Desktop.
Basic Oh My Zsh plugin to switch between PHP versions on Debian-based systems.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function switch_php_to() { | |
if [ -z "$1" ]; then | |
echo "Please specify PHP version first!" | |
return 1 | |
else | |
command -v php || return 1 | |
local CURRENT_PHP_VERSION=$(get_current_php_version) | |
if [ $1 == $CURRENT_PHP_VERSION ]; then | |
echo "PHP $1 is already loaded." | |
return 0 | |
elif [ $(command -v php$1) ]; then | |
setup_php_version $1 | |
else | |
install_php $1 && setup_php_version $1 | |
fi | |
echo "All done!" | |
fi | |
} | |
function get_current_php_version() { | |
php -r "echo substr(PHP_VERSION, 0, 3);" | |
} | |
function install_php() { | |
if [ -z "$1" ]; then | |
echo "Please specify PHP version first!" | |
return 1 | |
else | |
echo "Installing php$1" | |
local PACKAGE_MANAGER_INTERFACE="apt-get" | |
if [ $(command -v aptitude) ]; then | |
echo -n "You have aptitude installed locally on your machine, use it instead of the apt-get? (y/n)? " | |
read answer | |
if [ "$answer" != "${answer#[Yy]}" ]; then | |
PACKAGE_MANAGER_INTERFACE="aptitude" | |
fi | |
fi | |
sudo $PACKAGE_MANAGER_INTERFACE update | |
sudo $PACKAGE_MANAGER_INTERFACE install php$1 || return 1 | |
fi | |
} | |
function setup_php_version() { | |
if [ -z "$1" ]; then | |
echo "Please specify PHP version first!" | |
return 1 | |
else | |
echo "Setup php to version $1." | |
sudo update-alternatives --set php /usr/bin/php$1 | |
sudo update-alternatives --set phar /usr/bin/phar$1 | |
sudo update-alternatives --set phar.phar /usr/bin/phar.phar$1 | |
sudo update-alternatives --set phpize /usr/bin/phpize$1 | |
sudo update-alternatives --set php-config /usr/bin/php-config$1 | |
fi | |
} |
Hi 👋 Ready as far as you have updated repository with access to older php versions. You can rely (sudo add-apt-repository ppa:ondrej/php
) on ppa:ondrej/php repository for instance. Now I am working on Ubuntu 20 and to solve issues with different php version I just use docker for it 😉 but on my latest 18.04 ubuntu version everything was ok with this plugin.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
is not ready to use, right?