-
-
Save fathurrohman26/740afa96feaa20cfe07d9e5dcfa08d62 to your computer and use it in GitHub Desktop.
Link the php installed by Brew to ~/.phpenv/versions
This file contains hidden or 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
#!/usr/bin/env bash | |
set -au | |
# Check cellar dir | |
cellarDir=$(brew --prefix)/Cellar | |
if [ ! -d "$cellarDir" ]; then | |
echo "Not found. brew package path $cellarDir :(" | |
echo | |
exit 1 | |
fi | |
# Unlink all link files | |
find ~/.phpenv/versions -type l -exec unlink {} \; | |
parentDirs=$(find $cellarDir/php* -type d -maxdepth 0) | |
echo "# Link the php installed by Brew to ~/.phpenv/versions" | |
for parentDir in $parentDirs | |
do | |
childDirs=$(find "$parentDir"/* -type d -maxdepth 0) | |
for childDir in $childDirs | |
do | |
phpPath=$childDir/bin/php | |
if [ ! -f "$phpPath" ]; then | |
continue | |
fi | |
major=$($phpPath -d error_reporting=0 -r 'echo PHP_MAJOR_VERSION;') | |
minor=$($phpPath -d error_reporting=0 -r 'echo PHP_MINOR_VERSION;') | |
patch=$($phpPath -d error_reporting=0 -r 'echo PHP_RELEASE_VERSION;') | |
# link major.minor | |
linkCommand="ln -s $childDir $HOME/.phpenv/versions/$major.$minor" | |
echo "$linkCommand" | |
$linkCommand | |
# link major.minor.patch | |
linkCommand="ln -s $childDir $HOME/.phpenv/versions/$major.$minor.$patch" | |
echo "$linkCommand" | |
$linkCommand | |
done | |
done |
Notes on brew upgrade
When php is upgraded by brew upgrade, symlink will be broken, so you need to re-run the link.
curl -fsSL https://gist.githubusercontent.com/fatfatcocofat/740afa96feaa20cfe07d9e5dcfa08d62/raw/2a2f0ff6b295c5075216b3e4cfc3e0b3b37af6af/link-phps.bash | bash
Tests
# set version
cd /tmp && phpenv global 8.1 # set 8.1 as a global
cd /tmp/php74 && phpenv local 7.4 # pin 7.4 in this dir
cd /tmp/php80 && phpenv local 8.0 # pin 8.0 in this dir
cd /tmp/php81 && phpenv local 8.1 # pin 8.1 in this dir
# test version
cd /tmp && php -v # => 8.1
cd /tmp/php74 && php -v # => 7.4
cd /tmp/php80 && php -v # => 8.0
cd /tmp/php81 && php -v # => 8.1
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
curl -fsSL https://gist.githubusercontent.com/fatfatcocofat/740afa96feaa20cfe07d9e5dcfa08d62/raw/2a2f0ff6b295c5075216b3e4cfc3e0b3b37af6af/link-phps.bash | bash