Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support PHP 7.0+ #1346

Merged
merged 6 commits into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cli/Valet/Brew.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ class Brew
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
];
const BREW_DISABLE_AUTO_CLEANUP = 'HOMEBREW_NO_INSTALL_CLEANUP=1';
const LATEST_PHP_VERSION = '[email protected]';
Expand Down
91 changes: 91 additions & 0 deletions find-usable-php.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

$minimumPhpVersion = '8.0';

// First, check if the system's linked "php" is 8+; if so, return that. This
// is the most likely, most ideal, and fastest possible case
$linkedPhpVersion = shell_exec('php -r "echo phpversion();"');

if (version_compare($linkedPhpVersion, $minimumPhpVersion) >= 0) {
echo exec('which php');

return;
}

// If not, let's find it whether we have a version of PHP installed that's 8+;
// all users that run through this code path will see Valet run more slowly
$phps = explode(PHP_EOL, trim(shell_exec('brew list --formula | grep php')));

// Normalize version numbers
$phps = array_reduce($phps, function ($carry, $php) {
$carry[$php] = presumePhpVersionFromBrewFormulaName($php);

return $carry;
}, []);

// Filter out older versions of PHP
$modernPhps = array_filter($phps, function ($php) use ($minimumPhpVersion) {
return version_compare($php, $minimumPhpVersion) >= 0;
});

// If we don't have any modern versions of PHP, throw an error
if (empty($modernPhps)) {
throw new Exception('Sorry, but you do not have a version of PHP installed that is compatible with Valet (8+).');
}

// Sort newest version to oldest
sort($modernPhps);
$modernPhps = array_reverse($modernPhps);

// Grab the highest, set as $foundVersion, and output its path
$foundVersion = reset($modernPhps);
echo getPhpExecutablePath(array_search($foundVersion, $phps));

/**
* Function definitions.
*/

/**
* Extract PHP executable path from PHP Version.
* Copied from Brew.php and modified.
*
* @param string|null $phpFormulaName For example, "[email protected]"
* @return string
*/
function getPhpExecutablePath(string $phpFormulaName = null)
{
$brewPrefix = exec('printf $(brew --prefix)');

// Check the default `/opt/homebrew/opt/[email protected]/bin/php` location first
if (file_exists($brewPrefix."/opt/{$phpFormulaName}/bin/php")) {
return $brewPrefix."/opt/{$phpFormulaName}/bin/php";
}

// Check the `/opt/homebrew/opt/php71/bin/php` location for older installations
$oldPhpFormulaName = str_replace(['@', '.'], '', $phpFormulaName); // [email protected] to php71
if (file_exists($brewPrefix."/opt/{$oldPhpFormulaName}/bin/php")) {
return $brewPrefix."/opt/{$oldPhpFormulaName}/bin/php";
}

throw new Exception('Cannot find an executable path for provided PHP version: '.$phpFormulaName);
}

function presumePhpVersionFromBrewFormulaName(string $formulaName)
{
if ($formulaName === 'php') {
// Figure out its link
$details = json_decode(shell_exec("brew info $formulaName --json"));

if (! empty($details[0]->aliases[0])) {
$formulaName = $details[0]->aliases[0];
} else {
return null;
}
}

if (strpos($formulaName, 'php@') === false) {
return null;
}

return substr($formulaName, strpos($formulaName, '@') + 1);
}
1 change: 1 addition & 0 deletions upgrade.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
- Match the new type hints of the base ValetDriver
- Extend the new namespaced drivers instead of the old globally-namespaced drivers
- Add namespace
- Probably a lot more, @todo forgot to flesh this out as i went
34 changes: 27 additions & 7 deletions valet
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,32 @@ then
DIR=$(php -r "echo realpath('$DIR/../laravel/valet');")
fi

# Get a command-line executable we can use for php that's 8+; if this
# is the inside loop (Valet runs itself 2x in some settings), skip
# checking and pulling again by reading the exported env var
if [[ $PHP_EXECUTABLE = "" ]]
then
PHP=$(php $DIR/find-usable-php.php)

# Validate output before running it on the CLI
if [[ ! -f $PHP ]]; then
echo "Error finding executable PHP. Quitting for safety."
echo "Provided output from find-usable-php.php:"
echo $PHP
exit
fi

export PHP_EXECUTABLE=$PHP
else
PHP=$PHP_EXECUTABLE
fi

# If the command is the "share" command we will need to resolve out any
# symbolic links for the site. Before starting Ngrok, we will fire a
# process to retrieve the live Ngrok tunnel URL in the background.
if [[ "$1" = "share" ]]
then
SHARETOOL="$(php "$DIR/cli/valet.php" share-tool)"
SHARETOOL="$($PHP "$DIR/cli/valet.php" share-tool)"

if [[ $SHARETOOL = "ngrok" ]]
then
Expand All @@ -35,7 +55,7 @@ then
for PARAM in ${PARAMS[@]}
do
if [[ ${PARAM:0:1} != '-' ]]; then
PARAMS=("${PARAMS[@]/$PARAM}") #Quotes when working with strings
PARAMS=("${PARAMS[@]/$PARAM}") # Quotes when working with strings
fi
done

Expand All @@ -52,7 +72,7 @@ then
fi
done

TLD=$(php "$DIR/cli/valet.php" tld)
TLD=$($PHP "$DIR/cli/valet.php" tld)

# Decide the correct PORT: uses 60 for secure, else 80
if grep --quiet --no-messages 443 ~/.config/valet/Nginx/$HOST*
Expand Down Expand Up @@ -96,7 +116,7 @@ then
fi
done

TLD=$(php "$DIR/cli/valet.php" tld)
TLD=$($PHP "$DIR/cli/valet.php" tld)

# Decide the correct PORT: uses 443 for secure, else 80
if grep --quiet --no-messages 443 ~/.config/valet/Nginx/$HOST*
Expand All @@ -123,14 +143,14 @@ then
# Proxy PHP commands to the "php" executable on the isolated site
elif [[ "$1" = "php" ]]
then
$(php "$DIR/cli/valet.php" which-php) "${@:2}"
$($PHP "$DIR/cli/valet.php" which-php) "${@:2}"

exit

# Proxy Composer commands with the "php" executable on the isolated site
elif [[ "$1" = "composer" ]]
then
$(php "$DIR/cli/valet.php" which-php) $(which composer) "${@:2}"
$($PHP "$DIR/cli/valet.php" which-php) $(which composer) "${@:2}"

exit

Expand All @@ -144,5 +164,5 @@ else
exit
fi

php "$DIR/cli/valet.php" "$@"
$PHP "$DIR/cli/valet.php" "$@"
fi