Closed aerni closed 1 year ago
Thanks for this @aerni!
@jasonvarga If we were to publish a Statamic v3 driver, is this the correct code to use? If so, do you want to make the PR so it's attributed to you? If not, I'll just create the PR and attribute you as co-author.
@mattstauffer Some more thoughts I had.
The driver should probably just be called StatamicValetDriver
as there isn't any difference between Statamic v3, Statamic v4, and any future releases. Statamic has been a Laravel package since v3.
You might also want the driver to override the serves()
method so that Valet will only use this driver when Statamic is installed. Otherwise, the StatamicValetDriver
will always be used in favor of the LaravelValetDriver
.
A Statamic app will have a please
file:
public function serves(string $sitePath, string $siteName, string $uri): bool
{
return file_exists($sitePath.'/public/index.php') &&
file_exists($sitePath.'/please');
}
I'm happy to make the PR, but the problem I'm running into is that the Laravel Driver's serves
method will return true
before the Statamic specific driver gets a chance to run. Statamic 3+ sites are literally just Laravel sites, so the Laravel driver picks it up.
I understand that you probably have the Laravel driver in the array before the specific ones so that Laravel sites will get recognized quickly, without having to loop through all customer drivers first.
@jasonvarga Apologies, I just saw this. This is interesting, and I'm not exactly sure how to address it. I'm trying to figure out if there's any other reason I put Laravel's driver up at the top, and I'm a bit tempted to just allow it to live in with the other drivers, or to put Statamic up above Laravel manually. If we did that, I assume it would solve the problem for Statamic?
@mattstauffer I think moving anything above/before the Laravel driver is gonna cause it to fail to load if it extends the Laravel driver.
Instead I think the solution is to extract the call to if($driver->serves....)
into a separate loop that first loops through Custom drivers, then Specific drivers, then Laravel driver, then basic driver, basically in a reverse cascade of the original loop. First loop identifies and instantiates available drivers, second loop goes semi-backwards through it, falling back to Laravel then Basic if no local/custom/specific (in that order) pass the serves() test.
EDIT: I was wrong. The require_drivers.php
takes care of loading those essentials first.
I think if you swap these two lines, you'll be sorted.
The downside being that if you are hitting a Laravel site, you end up looping through the serves
(and mutateUri
) method on all the specific drivers first. Maybe that slows things down. 🤷 Maybe not a big deal though, since they are mostly just file_exists()
checks.
or to put Statamic up above Laravel manually
That'll solve the issue for Statamic, but it'll remain for anyone with a Laravel site trying to use a custom driver. (Looks like there aren't any in Valet itself, but people could have custom ones in ~/.config/valet/Drivers
.) Also I don't know if people will appreciate Statamic-specific code there. 😊
Yah, I know I put Laravel first for a reason when I was re-writing, so I don't want to break that. I think custom putting Statamic above there is fine. I don't really care if anyone doesn't like it, there's a perfectly good explanation. :) If they want to build their CMSes on top of Laravel and then they need something custom, they can come talk to me then.
Alrighty I'll put together the PR then. Thanks!
After some further digging, and after correcting my simulation that I was testing with, I agree with the proposed change.
Thanks both!
Description
The StatamicValetDriver.php is outdated and doesn't work with static caching in Statamic 3 and 4. It was written for Statamic 2, and should probably be preserved as
StatamicV2ValetDriver
like the StatamicV1ValetDriver.Following is a working driver for Statamic 3 and 4. It was written by Statamic's lead developer and originally published here: https://gist.github.com/jasonvarga/b6f4b6027b22fce0e1a7f9498dda81ea.
Steps To Reproduce
public/static
.laravel.log
:[2023-05-26 15:17:03] local.DEBUG: Static cache loaded [http://statamic.test] If you are seeing this, your server rewrite rules have not been set up correctly.
Diagnosis
sw_vers
valet --version
cat ~/.config/valet/config.json
cat ~/.composer/composer.json
composer global diagnose
composer global outdated
ls -al /etc/sudoers.d/
brew config
brew services list
brew list --formula --versions | grep -E "(php|nginx|dnsmasq|mariadb|mysql|mailhog|openssl)(@\d\..*)?\s"
brew outdated
brew tap
php -v
which -a php
php --ini
nginx -v
curl --version
php --ri curl
/opt/homebrew/bin/ngrok version
ls -al ~/.ngrok2
brew info nginx
brew info php
brew info openssl
openssl version -a
openssl ciphers
sudo nginx -t
which -a php-fpm
/opt/homebrew/opt/php/sbin/php-fpm -v
sudo /opt/homebrew/opt/php/sbin/php-fpm -y /opt/homebrew/etc/php/8.2/php-fpm.conf --test
ls -al ~/Library/LaunchAgents | grep homebrew
ls -al /Library/LaunchAgents | grep homebrew
ls -al /Library/LaunchDaemons | grep homebrew
ls -al /Library/LaunchDaemons | grep "com.laravel.valet."
ls -aln /etc/resolv.conf
cat /etc/resolv.conf
ifconfig lo0
sh -c 'echo "------\n/opt/homebrew/etc/nginx/valet/valet.conf\n---\n"; cat /opt/homebrew/etc/nginx/valet/valet.conf | grep -n "# valet loopback"; echo "\n------\n"'
sh -c 'for file in ~/.config/valet/dnsmasq.d/*; do echo "------\n~/.config/valet/dnsmasq.d/$(basename $file)\n---\n"; cat $file; echo "\n------\n"; done'
sh -c 'for file in ~/.config/valet/nginx/*; do echo "------\n~/.config/valet/nginx/$(basename $file)\n---\n"; cat $file | grep -n "# valet loopback"; echo "\n------\n"; done'