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

PHP version isolation helper for command line #1216

Merged
merged 30 commits into from
Mar 31, 2022

Conversation

NasirNobin
Copy link
Contributor

@NasirNobin NasirNobin commented Mar 22, 2022

This PR introduces 3 new valet commands. These will help users to run PHP and Composer commands with isolated site's PHP version on the CLI.

  1. valet php ... will proxy PHP Commands with isolated PHP version
  2. valet composer ... will proxy Composer Commands with isolated PHP version
  3. valet which-php outputs the PHP executable path for a site. For isolated site it would output the isolated PHP executable path. But non-isolated site will just output the linked default PHP path. The other two commands are dependent on this one to find the PHP executable.
🔽 Video demonstration
CleanShot.2022-03-23.at.03.39.09.mp4

Syntax

Though the original idea was to create a command like valet run that would proxy PHP calls to an isolated PHP version. But I kinda like the laravel sail style, so kept it this way.

Quick note on which-php command

Currently valet which-php uses PHP to find the correct executable path. Initially, I thought of doing this with vanilla bash. But then thought if it would bring any benefit, also would make it hard to test it. Some of the codes are still here, in case if we want to explore that route.

Symlinking PHP executables to make it a bit fast (No longer using it)

It's using a combination of brew --cellar and brew info commands to determine the correct PHP executable path, So each time user runs valet php -v it would go through the process of extracting the PATH, which is very slow. Ended up adding a caching mechanism that creates a symlink at /opt/homebrew/bin/valetphp81 location, that directs toward the actual path for that version.
So next time a user runs valet php -v it would resolve the PHP path much faster from the symlink.
Please do suggest if you think there's an easier way to find the PHP executable for a given PHP version. I'll be happy to refactor.

Update:

Refactored it to directly look for BREW_PREFIX/opt/[email protected]/bin/php location to find the executable binary file. It's already fast and convenient. So we are not creating any additional symlinks now. Thanks to @nicoverbruggen for the suggestion.

Alternative Solution

Before starting to work on this PR, I was working on another external solution called zsh-valet to solve the very same problem in a more automated way, still in progress. I'll try to manage that as an external zsh package.

--

Thanks to my colleague @rana01645, @MishukAdhikari & @shifat160 for helping me test this PR properly on both M1 and Intel Mac.

@NasirNobin NasirNobin marked this pull request as ready for review March 23, 2022 11:45
cli/Valet/Brew.php Outdated Show resolved Hide resolved
@NasirNobin NasirNobin marked this pull request as draft March 23, 2022 16:21
cli/Valet/Brew.php Outdated Show resolved Hide resolved
@NasirNobin NasirNobin marked this pull request as ready for review March 24, 2022 12:23
Copy link
Collaborator

@mattstauffer mattstauffer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fantastic work, as always. A few notes/requests :)

cli/Valet/Brew.php Outdated Show resolved Hide resolved
cli/valet.php Outdated Show resolved Hide resolved
cli/valet.php Outdated Show resolved Hide resolved
cli/valet.php Outdated Show resolved Hide resolved
valet Show resolved Hide resolved
valet Show resolved Hide resolved
cli/Valet/Brew.php Outdated Show resolved Hide resolved
cli/Valet/Brew.php Outdated Show resolved Hide resolved
cli/Valet/Brew.php Outdated Show resolved Hide resolved
tests/BrewTest.php Outdated Show resolved Hide resolved
cli/Valet/PhpFpm.php Outdated Show resolved Hide resolved
cli/valet.php Outdated Show resolved Hide resolved
valet Show resolved Hide resolved
valet Show resolved Hide resolved
Copy link
Collaborator

@mattstauffer mattstauffer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two minor changes and then I think we're good to go!

cli/Valet/Site.php Show resolved Hide resolved
cli/valet.php Outdated Show resolved Hide resolved
cli/valet.php Outdated Show resolved Hide resolved
Co-authored-by: Matt Stauffer <[email protected]>
cli/valet.php Outdated Show resolved Hide resolved
@mattstauffer
Copy link
Collaborator

Thanks so much for your work here @NasirNobin!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants