laravel / valet

A more enjoyable local development experience for Mac.
https://laravel.com/docs/valet
MIT License
2.52k stars 698 forks source link

Static files don't load in Statamic 3/4+ #1418

Closed aerni closed 1 year ago

aerni commented 1 year ago

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.

<?php

namespace Valet\Drivers\Custom;

use Valet\Drivers\LaravelValetDriver;

class StatamicValetDriver extends LaravelValetDriver
{
    /**
     * Get the fully resolved path to the application's front controller.
     */
    public function frontControllerPath(string $sitePath, string $siteName, string $uri): string
    {
        if ($this->isActualFile($staticPath = $this->getStaticPath($sitePath))) {
            return $staticPath;
        }

        return parent::frontControllerPath($sitePath, $siteName, $uri);
    }

    /**
     * Get the path to the static file.
     */
    protected function getStaticPath(string $sitePath): string
    {
        $parts = parse_url($_SERVER['REQUEST_URI']);
        $query = $parts['query'] ?? '';

        return $sitePath.'/public/static'.$parts['path'].'_'.$query.'.html';
    }
}

Steps To Reproduce

Diagnosis

sw_vers
ProductName:       macOS
ProductVersion:     13.3.1
ProductVersionExtra:    (a)
BuildVersion:       22E772610a
valet --version
Laravel Valet 4.1.0
cat ~/.config/valet/config.json
{
    "tld": "test",
    "loopback": "127.0.0.1",
    "paths": [
        "/Users/michael/.config/valet/Sites",
        "/Users/michael/Code"
    ]
}
cat ~/.composer/composer.json
{
    "require": {
        "laravel/valet": "^4.0",
        "laravel/installer": "^4.2",
        "statamic/cli": "^2.1",
        "spatie/global-ray": "^1.0",
        "beyondcode/expose": "^2.4",
        "schmidfelix/ploi-cli": "^1.4"
    }
}
composer global diagnose
Changed current directory to /Users/michael/.composer
Checking composer.json: WARNING
No license specified, it is recommended to do so. For closed-source software you may use "proprietary" as license.
Checking platform settings: OK
Checking git settings: OK git version 2.34.1
Checking http connectivity to packagist: OK
Checking https connectivity to packagist: OK
Checking github.com oauth access: OK
Checking disk free space: OK
Checking pubkeys: 
Tags Public Key Fingerprint: 57815BA2 7E54DC31 7ECC7CC5 573090D0  87719BA6 8F3BB723 4E5D42D0 84A14642
Dev Public Key Fingerprint: 4AC45767 E5EC2265 2F0C1167 CBBB8A2B  0C708369 153E328C AD90147D AFE50952
OK
Checking composer version: You are not running the latest stable version, run `composer self-update` to update (2.5.5 => 2.5.7)
Composer version: 2.5.5
PHP version: 8.2.5
PHP binary path: /opt/homebrew/Cellar/php/8.2.5/bin/php
OpenSSL version: OpenSSL 1.1.1t  7 Feb 2023
cURL version: 8.0.1 libz 1.2.11 ssl (SecureTransport) OpenSSL/1.1.1t
zip: extension present, unzip present, 7-Zip not available
composer global outdated
Changed current directory to /Users/michael/.composer
Legend:
! patch or minor release available - update recommended
~ major release available - update possible

Direct dependencies required in composer.json:
laravel/valet            v4.1.0   ! v4.1.1   A more enjoyable local developm...

Transitive dependencies not required in composer.json:
guzzlehttp/guzzle        7.5.1    ! 7.7.0    Guzzle is a PHP HTTP client lib...
guzzlehttp/promises      1.5.2    ~ 2.0.0    Guzzle promises library
illuminate/collections   v10.10.0 ! v10.12.0 The Illuminate Collections pack...
illuminate/conditionable v10.10.0 ! v10.12.0 The Illuminate Conditionable pa...
illuminate/container     v10.10.0 ! v10.12.0 The Illuminate Container package.
illuminate/contracts     v10.10.0 ! v10.12.0 The Illuminate Contracts package.
illuminate/macroable     v10.10.0 ! v10.12.0 The Illuminate Macroable package.
mnapoli/silly            1.8.1    ! 1.8.2    Silly CLI micro-framework based...
ls -al /etc/sudoers.d/
total 16
drwxr-xr-x   4 root  wheel   128 Apr 26 18:31 .
drwxr-xr-x  83 root  wheel  2656 May 25 18:08 ..
-rw-r--r--   1 root  wheel    83 Feb 15 17:26 brew
-rw-r--r--   1 root  wheel    86 Feb 15 17:26 valet
brew config
HOMEBREW_VERSION: 4.0.19
ORIGIN: https://github.com/Homebrew/brew
HEAD: 4446fe603cc19316036fd07e5df1124c4d15ead2
Last commit: 4 days ago
Core tap JSON: 26 May 15:19 UTC
HOMEBREW_PREFIX: /opt/homebrew
HOMEBREW_CASK_OPTS: []
HOMEBREW_MAKE_JOBS: 10
Homebrew Ruby: 2.6.10 => /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/bin/ruby
CPU: 10-core 64-bit arm_firestorm_icestorm
Clang: 14.0.3 build 1403
Git: 2.34.1 => /opt/homebrew/bin/git
Curl: 7.87.0 => /usr/bin/curl
macOS: 13.3.1-arm64
CLT: N/A
Xcode: 14.3
Rosetta 2: false
brew services list
Name        Status User    File
dnsmasq     none            root    
httpd       none                    
memcached   none                    
nginx       none            root    
php         none            root    
php@7.4     none            root    
php@8.0     none            root    
php@8.1     none            root    
redis       started         michael ~/Library/LaunchAgents/homebrew.mxcl.redis.plist
stripe-mock started         michael ~/Library/LaunchAgents/homebrew.mxcl.stripe-mock.plist
brew list --formula --versions | grep -E "(php|nginx|dnsmasq|mariadb|mysql|mailhog|openssl)(@\d\..*)?\s"
dnsmasq 2.86
nginx 1.23.2 1.23.4
openssl@1.1 1.1.1t
php 8.2.0 8.1.1 8.1.6 8.1.11 8.2.5
php@7.4 7.4.32 7.4.27 7.4.29
php@8.0 8.0.28 8.0.26
php@8.1 8.1.13 8.1.18
brew outdated
aom
bash
c-ares
composer
coreutils
curl
dnsmasq
ghostscript
git
glib
gnu-getopt
gnu-sed
grep
imagemagick
jasper
libde265
libffi
libheif
libnghttp2
libomp
libpq
libraw
libuv
mas
memcached
nginx
node
openjpeg
php
php@7.4
php@8.1
sqlite
stripe/stripe-cli/stripe
stripe/stripe-mock/stripe-mock
zlib
expressvpn
font-meslo-lg-nerd-font
brew tap
homebrew/bundle
homebrew/cask-fonts
homebrew/services
nicoverbruggen/cask
shivammathur/php
stripe/stripe-cli
stripe/stripe-mock
php -v
PHP 8.2.5 (cli) (built: Apr 13 2023 17:59:46) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.5, Copyright (c) Zend Technologies
    with Zend OPcache v8.2.5, Copyright (c), by Zend Technologies
which -a php
/opt/homebrew/bin/php
php --ini
Configuration File (php.ini) Path: /opt/homebrew/etc/php/8.2
Loaded Configuration File:         /opt/homebrew/etc/php/8.2/php.ini
Scan for additional .ini files in: /opt/homebrew/etc/php/8.2/conf.d
Additional .ini files parsed:      /opt/homebrew/etc/php/8.2/conf.d/error_log.ini,
/opt/homebrew/etc/php/8.2/conf.d/ext-opcache.ini,
/opt/homebrew/etc/php/8.2/conf.d/php-memory-limits.ini
nginx -v
nginx version: nginx/1.23.4
curl --version
curl 7.87.0 (x86_64-apple-darwin22.0) libcurl/7.87.0 (SecureTransport) LibreSSL/3.3.6 zlib/1.2.11 nghttp2/1.51.0
Release-Date: 2022-12-21
Protocols: dict file ftp ftps gopher gophers http https imap imaps ldap ldaps mqtt pop3 pop3s rtsp smb smbs smtp smtps telnet tftp
Features: alt-svc AsynchDNS GSS-API HSTS HTTP2 HTTPS-proxy IPv6 Kerberos Largefile libz MultiSSL NTLM NTLM_WB SPNEGO SSL threadsafe UnixSockets
php --ri curl
curl

cURL support => enabled
cURL Information => 8.0.1
Age => 10
Features
AsynchDNS => Yes
CharConv => No
Debug => No
GSS-Negotiate => No
IDN => Yes
IPv6 => Yes
krb4 => No
Largefile => Yes
libz => Yes
NTLM => Yes
NTLMWB => Yes
SPNEGO => Yes
SSL => Yes
SSPI => No
TLS-SRP => Yes
HTTP2 => Yes
GSSAPI => Yes
KERBEROS5 => Yes
UNIX_SOCKETS => Yes
PSL => No
HTTPS_PROXY => Yes
MULTI_SSL => Yes
BROTLI => Yes
ALTSVC => Yes
HTTP3 => No
UNICODE => No
ZSTD => Yes
HSTS => Yes
GSASL => No
Protocols => dict, file, ftp, ftps, gopher, gophers, http, https, imap, imaps, ldap, ldaps, mqtt, pop3, pop3s, rtmp, rtmpe, rtmps, rtmpt, rtmpte, rtmpts, rtsp, scp, sftp, smb, smbs, smtp, smtps, telnet, tftp
Host => aarch64-apple-darwin22.3.0
SSL Version => (SecureTransport) OpenSSL/1.1.1t
ZLib Version => 1.2.11
libSSH Version => libssh2/1.10.0

Directive => Local Value => Master Value
curl.cainfo => no value => no value
/opt/homebrew/bin/ngrok version
sudo: /opt/homebrew/bin/ngrok: command not found
ls -al ~/.ngrok2
ls: /Users/michael/.ngrok2: No such file or directory
brew info nginx
==> nginx: stable 1.25.0 (bottled), HEAD
HTTP(S) server and reverse proxy, and IMAP/POP3 proxy server
https://nginx.org/
/opt/homebrew/Cellar/nginx/1.23.2 (23 files, 2.2MB)
  Built from source
/opt/homebrew/Cellar/nginx/1.23.4 (26 files, 2.2MB) *
  Poured from bottle using the formulae.brew.sh API on 2023-05-09 at 14:19:28
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/nginx.rb
License: BSD-2-Clause
==> Dependencies
Required: openssl@1.1, pcre2
==> Options
--HEAD
    Install HEAD version
==> Caveats
Docroot is: /opt/homebrew/var/www

The default port has been set in /opt/homebrew/etc/nginx/nginx.conf to 8080 so that
nginx can run without sudo.

nginx will load all files in /opt/homebrew/etc/nginx/servers/.

To start nginx now and restart at login:
  brew services start nginx
Or, if you don't want/need a background service you can just run:
  /opt/homebrew/opt/nginx/bin/nginx -g daemon off;
==> Analytics
install: 999 (30 days), 4,302 (90 days), 359,006 (365 days)
install-on-request: 1,002 (30 days), 4,299 (90 days), 358,455 (365 days)
build-error: 0 (30 days)
brew info php
==> php: stable 8.2.6 (bottled), HEAD
General-purpose scripting language
https://www.php.net/
/opt/homebrew/Cellar/php/8.1.1 (509 files, 81.5MB)
  Built from source
/opt/homebrew/Cellar/php/8.1.6 (511 files, 81.6MB)
  Built from source
/opt/homebrew/Cellar/php/8.1.11 (510 files, 81.6MB)
  Built from source
/opt/homebrew/Cellar/php/8.2.0 (516 files, 83.3MB)
  Built from source
/opt/homebrew/Cellar/php/8.2.5 (520 files, 83.5MB) *
  Poured from bottle using the formulae.brew.sh API on 2023-05-09 at 14:19:14
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/php.rb
License: PHP-3.01
==> Dependencies
Build: httpd, pkg-config
Required: apr, apr-util, argon2, aspell, autoconf, curl, freetds, gd, gettext, gmp, icu4c, krb5, libpq, libsodium, libzip, oniguruma, openldap, openssl@1.1, pcre2, sqlite, tidy-html5, unixodbc
==> Options
--HEAD
    Install HEAD version
==> Caveats
To enable PHP in Apache add the following to httpd.conf and restart Apache:
    LoadModule php_module /opt/homebrew/opt/php/lib/httpd/modules/libphp.so

    
        SetHandler application/x-httpd-php
    

Finally, check DirectoryIndex includes index.php
    DirectoryIndex index.php index.html

The php.ini and php-fpm.ini file can be found in:
    /opt/homebrew/etc/php/8.2/

To start php now and restart at login:
  brew services start php
Or, if you don't want/need a background service you can just run:
  /opt/homebrew/opt/php/sbin/php-fpm --nodaemonize
==> Analytics
install: 1,380 (30 days), 5,717 (90 days), 1,148,711 (365 days)
install-on-request: 1,223 (30 days), 5,169 (90 days), 1,003,924 (365 days)
build-error: 0 (30 days)
brew info openssl
==> openssl@3: stable 3.1.0 (bottled) [keg-only]
Cryptography and SSL/TLS Toolkit
https://openssl.org/
Not installed
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/openssl@3.rb
License: Apache-2.0
==> Dependencies
Required: ca-certificates
==> Caveats
A CA file has been bootstrapped using certificates from the system
keychain. To add additional certificates, place .pem files in
  /opt/homebrew/etc/openssl@3/certs

and run
  /opt/homebrew/opt/openssl@3/bin/c_rehash

openssl@3 is keg-only, which means it was not symlinked into /opt/homebrew,
because macOS provides LibreSSL.
==> Analytics
install: 4,616 (30 days), 17,220 (90 days), 2,011,185 (365 days)
install-on-request: 2,306 (30 days), 8,004 (90 days), 996,942 (365 days)
build-error: 545 (30 days)
openssl version -a
LibreSSL 3.3.6
built on: date not available
platform: information not available
options:  bn(64,64) rc4(ptr,int) des(idx,cisc,16,int) blowfish(idx) 
compiler: information not available
OPENSSLDIR: "/private/etc/ssl"
openssl ciphers
AEAD-CHACHA20-POLY1305-SHA256:AEAD-AES256-GCM-SHA384:AEAD-AES128-GCM-SHA256:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-CHACHA20-POLY1305:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA:GOST2012256-GOST89-GOST89:DHE-RSA-CAMELLIA256-SHA256:DHE-RSA-CAMELLIA256-SHA:GOST2001-GOST89-GOST89:AES256-GCM-SHA384:AES256-SHA256:AES256-SHA:CAMELLIA256-SHA256:CAMELLIA256-SHA:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-CAMELLIA128-SHA256:DHE-RSA-CAMELLIA128-SHA:AES128-GCM-SHA256:AES128-SHA256:AES128-SHA:CAMELLIA128-SHA256:CAMELLIA128-SHA:ECDHE-RSA-RC4-SHA:ECDHE-ECDSA-RC4-SHA:RC4-SHA:RC4-MD5:ECDHE-RSA-DES-CBC3-SHA:ECDHE-ECDSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:DES-CBC3-SHA
sudo nginx -t
nginx: the configuration file /opt/homebrew/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /opt/homebrew/etc/nginx/nginx.conf test is successful
which -a php-fpm
/opt/homebrew/sbin/php-fpm
/opt/homebrew/opt/php/sbin/php-fpm -v
PHP 8.2.5 (fpm-fcgi) (built: Apr 13 2023 17:59:48)
Copyright (c) The PHP Group
Zend Engine v4.2.5, Copyright (c) Zend Technologies
    with Zend OPcache v8.2.5, Copyright (c), by Zend Technologies
sudo /opt/homebrew/opt/php/sbin/php-fpm -y /opt/homebrew/etc/php/8.2/php-fpm.conf --test
[26-May-2023 11:19:26] NOTICE: configuration file /opt/homebrew/etc/php/8.2/php-fpm.conf test is successful
ls -al ~/Library/LaunchAgents | grep homebrew
-rw-r--r--    1 michael  staff   880 May  9 14:50 homebrew.mxcl.redis.plist
-rw-r--r--    1 michael  staff   903 Apr 23  2022 homebrew.mxcl.stripe-mock.plist
ls -al /Library/LaunchAgents | grep homebrew

ls -al /Library/LaunchDaemons | grep homebrew
-rw-r--r--   1 root  admin   602 May 25 12:10 homebrew.mxcl.dnsmasq.plist
-rw-r--r--   1 root  admin   685 May 25 12:10 homebrew.mxcl.nginx.plist
-rw-r--r--   1 root  admin   781 May 25 12:10 homebrew.mxcl.php.plist
-rw-r--r--   1 root  admin   789 May  9 15:03 homebrew.mxcl.php@7.4.plist
-rw-r--r--   1 root  admin   789 May 10 20:26 homebrew.mxcl.php@8.0.plist
-rw-r--r--   1 root  admin   789 May  9 15:03 homebrew.mxcl.php@8.1.plist
ls -al /Library/LaunchDaemons | grep "com.laravel.valet."

ls -aln /etc/resolv.conf
lrwxr-xr-x  1 0  0  22 Apr  1 12:46 /etc/resolv.conf -> ../var/run/resolv.conf
cat /etc/resolv.conf
#
# macOS Notice
#
# This file is not consulted for DNS hostname resolution, address
# resolution, or the DNS query routing mechanism used by most
# processes on this system.
#
# To view the DNS configuration used by this system, use:
#   scutil --dns
#
# SEE ALSO
#   dns-sd(1), scutil(8)
#
# This file is automatically generated.
#
nameserver 100.64.0.2
ifconfig lo0
lo0: flags=8049 mtu 16384
    options=1203
    inet 127.0.0.1 netmask 0xff000000 
    inet6 ::1 prefixlen 128 
    inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1 
    nd6 options=201
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"'
------
/opt/homebrew/etc/nginx/valet/valet.conf
---

3:    #listen VALET_LOOPBACK:80; # valet loopback

------
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'
------
~/.config/valet/dnsmasq.d/tld-test.conf
---

address=/.test/127.0.0.1
listen-address=127.0.0.1

------
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'
------
~/.config/valet/nginx/alivingchange.test
---

5:    #listen VALET_LOOPBACK:80; # valet loopback

------

------
~/.config/valet/nginx/autonomiesuisse-v2.test
---

5:    #listen VALET_LOOPBACK:80; # valet loopback

------

------
~/.config/valet/nginx/autonomiesuisse.test
---

4:    #listen 127.0.0.1:80; # valet loopback
11:    #listen 127.0.0.1:443 ssl http2; # valet loopback
55:    #listen 127.0.0.1:60; # valet loopback

------

------
~/.config/valet/nginx/baerenmatte-v2.test
---

5:    #listen VALET_LOOPBACK:80; # valet loopback

------

------
~/.config/valet/nginx/baerenmatte.test
---

4:    #listen 127.0.0.1:80; # valet loopback
11:    #listen 127.0.0.1:443 ssl http2; # valet loopback
55:    #listen 127.0.0.1:60; # valet loopback

------

------
~/.config/valet/nginx/byroaarau.test
---

5:    #listen VALET_LOOPBACK:80; # valet loopback

------

------
~/.config/valet/nginx/equus.test
---

5:    #listen VALET_LOOPBACK:80; # valet loopback

------

------
~/.config/valet/nginx/gesund-und-frei.test
---

5:    #listen VALET_LOOPBACK:80; # valet loopback

------

------
~/.config/valet/nginx/gomagazin.test
---

4:    #listen 127.0.0.1:80; # valet loopback
11:    #listen 127.0.0.1:443 ssl http2; # valet loopback
55:    #listen 127.0.0.1:60; # valet loopback

------

------
~/.config/valet/nginx/hyve-no-assets.test
---

5:    #listen VALET_LOOPBACK:80; # valet loopback

------

------
~/.config/valet/nginx/hyve.test
---

5:    #listen VALET_LOOPBACK:80; # valet loopback

------

------
~/.config/valet/nginx/laravel-spotify.test
---

5:    #listen VALET_LOOPBACK:80; # valet loopback

------

------
~/.config/valet/nginx/michaelaerni.test
---

4:    #listen 127.0.0.1:80; # valet loopback
11:    #listen 127.0.0.1:443 ssl http2; # valet loopback
55:    #listen 127.0.0.1:60; # valet loopback

------

------
~/.config/valet/nginx/mickenbecker.test
---

5:    #listen VALET_LOOPBACK:80; # valet loopback

------

------
~/.config/valet/nginx/rewe-group.test
---

4:    #listen 127.0.0.1:80; # valet loopback
11:    #listen 127.0.0.1:443 ssl http2; # valet loopback
55:    #listen 127.0.0.1:60; # valet loopback

------

------
~/.config/valet/nginx/siloam-crowdfunding.test
---

5:    #listen VALET_LOOPBACK:80; # valet loopback

------

------
~/.config/valet/nginx/statamic-advanced-seo.test
---

4:    #listen 127.0.0.1:80; # valet loopback
11:    #listen 127.0.0.1:443 ssl http2; # valet loopback
55:    #listen 127.0.0.1:60; # valet loopback

------

------
~/.config/valet/nginx/statamic-font-awesome.test
---

3:    #listen 127.0.0.1:80; # valet loopback
10:    #listen 127.0.0.1:443 ssl http2; # valet loopback
54:    #listen 127.0.0.1:60; # valet loopback

------

------
~/.config/valet/nginx/statamic-livewire-forms.test
---

3:    #listen 127.0.0.1:80; # valet loopback
10:    #listen 127.0.0.1:443 ssl http2; # valet loopback
54:    #listen 127.0.0.1:60; # valet loopback

------

------
~/.config/valet/nginx/statamic-zipper.test
---

4:    #listen 127.0.0.1:80; # valet loopback
11:    #listen 127.0.0.1:443 ssl http2; # valet loopback
55:    #listen 127.0.0.1:60; # valet loopback

------

------
~/.config/valet/nginx/tdsaarau.test
---

4:    #listen 127.0.0.1:80; # valet loopback
11:    #listen 127.0.0.1:443 ssl http2; # valet loopback
55:    #listen 127.0.0.1:60; # valet loopback

------

------
~/.config/valet/nginx/viscom-print-drink.test
---

5:    #listen VALET_LOOPBACK:80; # valet loopback

------
mattstauffer commented 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.

aerni commented 1 year ago

@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');
}
jasonvarga commented 1 year ago

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.

https://github.com/laravel/valet/blob/a28d958afd2f3459d7629d51fb5dde2178ba3d3c/cli/Valet/Drivers/ValetDriver.php#L47-L53

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.

mattstauffer commented 1 year ago

@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?

drbyte commented 1 year ago

@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.

jasonvarga commented 1 year ago

I think if you swap these two lines, you'll be sorted.

https://github.com/laravel/valet/blob/a28d958afd2f3459d7629d51fb5dde2178ba3d3c/cli/Valet/Drivers/ValetDriver.php#L48-L49

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. 😊

mattstauffer commented 1 year ago

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.

jasonvarga commented 1 year ago

Alrighty I'll put together the PR then. Thanks!

drbyte commented 1 year ago

After some further digging, and after correcting my simulation that I was testing with, I agree with the proposed change.

mattstauffer commented 1 year ago

Thanks both!