Last active
May 24, 2024 14:10
-
-
Save yuki777/6244823b8aa8cf4457e97e6407ada5ad to your computer and use it in GitHub Desktop.
Link the php installed by Brew to ~/.phpenv/versions
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
#!/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 |
Author
yuki777
commented
Apr 12, 2022
•
# Tests
mkdir -p /tmp/php74 /tmp/php80 /tmp/php81
cd /tmp && phpenv global 8.1 && php -v # It should be 8.1
cd /tmp/php74 && phpenv local 7.4 && php -v # It should be 7.4
cd /tmp/php80 && phpenv local 8.0 && php -v # It should be 8.0
cd /tmp/php81 && phpenv local 8.1 && php -v # It should be 8.1
Very cool, thank you!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment