PHPENV revisited

By Erik, Fri, 07/02/2021 - 20:50

In my previous post, brew didn't have PHP as a core feature, so it needed to be tapped. Now they are core packages and they've changed the format for the versioning to use @ in the versions.

$ brew search /^php\(\@\\d\.\\d\)?$/
==> Formulae
php ✔                         php@7.2                       php@7.3                       php@7.4 ✔

So, installing two versions of PHP can be handled with the command:

$ brew install php php@7.4

The php recipe is currently an alias for php@8.0 which is also usable if you'd prefer.

To install phpenv, the repo has very good set of instructions. The basic premise is to check out the repo and add the path to your bash profile so that phpenv and phpenv shims can be found.

Then when it comes time for updating, you can simply run brew update normally and then run this simple script to make sure phpenv versions are in sync:

#!/usr/bin/env bash

echo -e "Removing old phpenv versions"
find "${HOME}/.phpenv/versions" -maxdepth 1 -mindepth 1 -type l -delete

echo -e "\nAdding new phpenv versions"
for version in $(find $(find /usr/local/Cellar -maxdepth 1 -mindepth 1 -name 'php@[0-9].[0-9]' -o -name 'php' ) -maxdepth 1 -mindepth 1 -type d 2>/dev/null); do
  echo -e "  Adding symlinks for ${version}"
  ln -s ${version} ${HOME}/.phpenv/versions/ 2>/dev/null
  ln -s ${version} ${HOME}/.phpenv/versions/$(echo "${version}" | perl -p -e "s/.*(\d+\.\d+)\.(.+)$/\1/") 2>/dev/null

echo -e "\nRehashing phpenv"
phpenv rehash