Skip to content

Instantly share code, notes, and snippets.

@kherge
Last active January 17, 2017 18:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kherge/e431f3fe0e5610b2366a92fc90bf0ce5 to your computer and use it in GitHub Desktop.
Save kherge/e431f3fe0e5610b2366a92fc90bf0ce5 to your computer and use it in GitHub Desktop.
A script for switching between multiple versions of PHP.

PHP Alias

This is a BASH shell script that will,

  1. Keep track of a list of PHP interpreters available.
  2. Allow you to switch quickly between them.
  3. Automatically switch to a different version when entering a directory.

Installation

  1. Create ~/.php
  2. Copy init.sh to ~/.php/init.sh
  3. Add . ~/.php/init.sh to the end of .bashrc.
  4. Reload your terminal.

Usage

You can get all the help information you need by running php-cfg without any arguments.

Usage: php-cfg COMMAND
Manages the `php` alias for the command line.

COMMAND

    default  Sets the default alias to use.
    define   Creates a new alias to an interpreter.
    remove   Removes an existing alias to an interpreter.
    show     Shows the real path of the alias to the interpreter.
    use      Switch to a different interpreter.

If you create a .php file in a directory, that directory will cause you to automatically switch to the interpreter defined in the file. When you leave the directory, the default interpreter is restored.

#!/usr/bin/env bash
###
# Manages the available links and the active alias.
##
function php-cfg()
{
# Get the command.
local COMMAND=${1}; shift
# Run the command.
case ${COMMAND} in
default) php-cfg_default "$@" ;;
define) php-cfg_define "$@" ;;
list) php-cfg_list ;;
remove) php-cfg_remove "$@" ;;
show) php-cfg_show "$@" ;;
use) php-cfg_use "$@" ;;
*) php-cfg_usage ;;
esac
return $?
}
###
# Sets the default link for the alias.
##
function php-cfg_default()
{
local NAME=${1}
if [ 'default' = ${NAME} ]; then
echo "The default alias can not be itself." >&2
return 1
fi
if [ ! -e ${PHP_CFG_LINK}/${NAME} ]; then
echo "The alias does not exist." >&2
return 1
fi
if [ -e ${PHP_CFG_LINK}/default ]; then
rm ${PHP_CFG_LINK}/default
fi
ln -s ${PHP_CFG_LINK}/${NAME} ${PHP_CFG_LINK}/default
return $?
}
###
# Defines a new link for the alias.
##
function php-cfg_define()
{
local NAME=${1}
local WHERE=${2}
if [ -z ${NAME} ] || [ -z ${WHERE} ]; then
cat <<'USAGE'
Usage: php-cfg define NAME PATH
Defines a new alias to a interpreter.
ARGUMENTS
NAME The name of the alias. (e.g. "5.6.29")
PATH The path to the interpreter.
USAGE
fi
if [ -e ${PHP_CFG_LINK}/${NAME} ]; then
local RESPONSE=
while [ 'y' != ${RESPONSE} ] && [ 'n' != ${RESPONSE} ]; do
read -p 'Replace existing alias? [y/N] ' RESPONSE
done
if [ 'n' = ${RESPONSE} ]; then
echo "The alias will not be replaced."
return 0
fi
fi
if [ ! -f ${WHERE} ]; then
echo "The interpreter does not exist or is not a file." >&2
return 1
fi
ln -s ${WHERE} ${PHP_CFG_LINK}/${NAME}
return $?
}
###
# Lists the links for the alias.
##
function php-cfg_list()
{
echo "Available interpreters:"
while read AVAILABLE; do
echo " - $AVAILABLE"
done < <(ls -1 ${PHP_CFG_LINK})
return 0
}
###
# Updates the alias on directory change.
##
function php-cfg_prompt()
{
if [ "$(pwd)" != "$PHP_CFG_LAST" ]; then
if [ -f ".php" ]; then
local NAME="$(cat .php)"
php-cfg_use ${NAME}
return $?
else
php-cfg_use
fi
fi
PHP_CFG_LAST="$(pwd)"
}
###
# Removes an existing link for the alias.
##
function php-cfg_remove()
{
local NAME=${1}
if [ -e ${PHP_CFG_LINK}/${NAME} ]; then
rm ${PHP_CFG_LINK}/${NAME}
return $?
fi
return 0
}
###
# Shows the real path for the alias link.
##
function php-cfg_show()
{
local NAME=${1}
if [ ! -e ${PHP_CFG_LINK}/${NAME} ]; then
echo "$NAME: No such alias" >&2
return 1
fi
echo -n "$NAME: "
readlink ${PHP_CFG_LINK}/${NAME}
return $?
}
###
# Displays the usage information.
##
function php-cfg_usage()
{
cat <<'USAGE'
Usage: php-cfg COMMAND
Manages the `php` alias for the command line.
COMMAND
default Sets the default alias to use.
define Creates a new alias to an interpreter.
remove Removes an existing alias to an interpreter.
show Shows the real path of the alias to the interpreter.
use Switch to a different interpreter.
USAGE
}
###
# Sets the alias to a different link.
##
function php-cfg_use()
{
local NAME=${1}
if [ '' = "$NAME" ]; then
unalias php 2> /dev/null
if [ -e ${PHP_CFG_LINK}/default ]; then
php-cfg_use default
fi
return 0
fi
if [ ! -e ${PHP_CFG_LINK}/${NAME} ]; then
echo "$NAME: No such interpreter" >&2
return 1
fi
alias php=${PHP_CFG_LINK}/${NAME}
}
################################################################################
# Declare constants.
readonly PHP_CFG_HOME=${HOME}/.php
readonly PHP_CFG_LINK=${PHP_CFG_HOME}/link
# Make sure the links directory exists.
if [ ! -d ${PHP_CFG_LINK} ]; then
mkdir -p ${PHP_CFG_LINK}
fi
# Set the default alias.
if [ -e ${PHP_CFG_LINK}/default ]; then
php-cfg_use default
fi
# Update alias on directory change.
PROMPT_COMMAND="php-cfg_prompt;$PROMPT_COMMAND"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment