Last active
October 7, 2025 20:18
-
-
Save MouseEatsCat/dfc2090b68f2effa02c42a5b8d3f75fc to your computer and use it in GitHub Desktop.
Revisions
-
MouseEatsCat revised this gist
Jul 17, 2023 . 1 changed file with 146 additions and 79 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -8,22 +8,26 @@ namespace Valet\Drivers\Custom; use Illuminate\Support\Collection; use Valet\Drivers\BasicValetDriver; class LocomotiveBedrockValetDriver extends BasicValetDriver { public const PUBLIC_PATHS = [ 'www', 'web' ]; public const WP_PATHS = [ 'wp', 'wordpress' ]; public const STATIC_FILE_TYPES = [ 'jpe?g', 'gif', 'pdf', 'png', 'svg', 'mp4' ]; /** @var ?array<string, mixed> */ public $valetSiteConfig = null; /** @var ?string */ public $valetSiteName = null; /** @var ?string */ public $valetSitePath = null; /** @var array<string, mixed> */ public $valetSiteOptions = [ 'remote_assets_domain' => null, 'allow_indexphp_files' => false, 'redirect_trailing_slashes' => false, @@ -34,12 +38,17 @@ class LocomotiveBedrockValetDriver extends BasicValetDriver */ public function serves(string $sitePath, string $siteName, string $uri): bool { $hasBedrockAutoloader = $this->composerRequires($sitePath, 'roots/bedrock-autoloader'); foreach (static::PUBLIC_PATHS as $publicPath) { if ( $hasBedrockAutoloader || file_exists("{$sitePath}/{$publicPath}/mu-plugins/bedrock-autoloader.php") || ( is_dir("{$sitePath}/{$publicPath}/") && file_exists("{$sitePath}/{$publicPath}/wp-config.php") && file_exists("{$sitePath}/config/application.php") ) ) { return true; } @@ -55,52 +64,75 @@ public function serves(string $sitePath, string $siteName, string $uri): bool */ public function isStaticFile(string $sitePath, string $siteName, string $uri) { foreach (static::PUBLIC_PATHS as $publicPath) { $staticFilePath = "{$sitePath}/{$publicPath}{$uri}"; if ($this->isActualFile($staticFilePath)) { return $staticFilePath; } } // Use remote assets as fallback $this->isRemoteFile($sitePath, $siteName, $uri); return false; } /** * Determine if the incoming request is for a remote static file. * * @return void */ public function isRemoteFile(string $sitePath, string $siteName, string $uri): void { $removeAssetHost = $this->getValetConfig('remote_assets_domain'); $regexpEligibleFiles = '/^(.*)\.('.implode('|', static::STATIC_FILE_TYPES).')$/i'; if ($removeAssetHost && preg_match($regexpEligibleFiles, $uri)) { header("Location: {$removeAssetHost}{$uri}"); exit; } } /** * Get the fully resolved path to the application's front controller. */ public function frontControllerPath(string $sitePath, string $siteName, string $uri): string { // Do not allow direct use of "index.php" if (!$this->getValetConfig('allow_indexphp_files') && preg_match('!/index.php$!', $uri)) { $uri = str_replace('index.php', '', $uri); header('Location: ' . $uri); exit; } $uri = ( $this->getValetConfig('redirect_trailing_slashes') ? $uri : $this->forceTrailingSlash($uri) ); foreach (static::PUBLIC_PATHS as $publicPath) { if (!is_dir("{$sitePath}/{$publicPath}/")) { continue; } $path = parent::frontControllerPath( "{$sitePath}/{$publicPath}", $siteName, $uri ); foreach (static::WP_PATHS as $wpPath) { if (!is_dir("$sitePath/$publicPath/$wpPath")) { continue; } if (!str_contains($_SERVER['PHP_SELF'], "{$wpPath}/wp-admin")) { $_SERVER['PHP_SELF'] = '/index.php'; } } return $path; } } @@ -112,10 +144,10 @@ public function frontControllerPath(string $sitePath, string $siteName, string $ */ public function forceTrailingSlash($uri) { foreach (static::WP_PATHS as $wpPath) { $path = "/{$wpPath}/wp-admin"; if (substr($uri, -1 * strlen($path)) === $path) { header("Location: {$uri}/"); exit; } } @@ -124,32 +156,17 @@ public function forceTrailingSlash($uri) } /** * Load server environment variables if available. * Processes any '*' entries first, and then adds site-specific entries. */ public function loadServerEnvironmentVariables(string $sitePath, string $siteName): void { parent::loadServerEnvironmentVariables($sitePath, $siteName); $this->valetSitePath = $sitePath; $this->valetSiteName = $siteName; $this->loadValetConfig($sitePath, $siteName); } /** @@ -159,28 +176,78 @@ public function handleConfigRules($sitePath, $siteName, $uri) */ public function hasValetConfig() { return $this->valetSitePath && file_exists($this->valetSitePath . '/' . static::VALET_CONFIG_FILE); } /** * Get the driver configuration options or a single option. * * @param string|null $key The single driver configuration option. * @return mixed|null Returns null when no matching option is found. */ public function getValetConfig(?string $key = null) { if (is_null($this->valetSiteConfig)) { $this->loadValetConfig($this->valetSitePath, $this->valetSiteName); } if ($key) { return $this->valetSiteConfig[$key] ?? null; } return $this->valetSiteConfig; } /** * Load driver configuration if available. * Processes any '*' entries first, and then adds site-specific entries. */ public function loadValetConfig(string $sitePath, string $siteName): array { $this->valetSiteConfig = $this->valetSiteOptions; $confFilePath = $sitePath.'/.valet-config.php'; if (file_exists($confFilePath)) { // Load .valet-config.php $config = include $confFilePath; } else { $confFilePath = $sitePath.'/valet-config.json'; if (file_exists($confFilePath)) { // Load valet-config.json $config = json_decode( file_get_contents($confFilePath), true ); } else { // Fallback to global valet config.json $config = json_decode( file_get_contents(VALET_HOME_PATH.'/config.json'), true ); } } if (! isset($config) || ! is_array($config)) { return $this->valetSiteConfig; } if (isset($config['*']) && is_array($config['*'])) { $this->valetSiteConfig = array_merge($this->valetSiteConfig, $config['*']); } if (isset($config[$siteName]) && is_array($config[$siteName])) { $this->valetSiteConfig = array_merge($this->valetSiteConfig, $config[$siteName]); } // Backwards compatibility with initial "valet-config.json" file. if ($config = array_intersect_key($config, $this->valetSiteOptions)) { $this->valetSiteConfig = array_merge($this->valetSiteConfig, $config); } return $this->valetSiteConfig; } } if (!function_exists('str_contains')) { function str_contains($haystack, $needle) { return $needle !== '' && mb_strpos($haystack, $needle) !== false; } } -
MouseEatsCat revised this gist
May 16, 2023 . 1 changed file with 11 additions and 17 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,10 +1,17 @@ <?php /** * Locomotive Bedrock Valet Driver * * Supported Valet Version: 4 */ namespace Valet\Drivers\Custom; use Valet\Drivers\BasicValetDriver; class LocomotiveBedrockValetDriver extends BasicValetDriver { public $PUBLIC_PATHS = ['www', 'web']; public $WP_PATHS = ['wp', 'wordpress']; public $VALET_CONFIG_FILE = '/valet-config.json'; @@ -24,13 +31,8 @@ class LocomotiveBedrockValetDriver extends BasicValetDriver /** * Determine if the driver serves the request. */ public function serves(string $sitePath, string $siteName, string $uri): bool { foreach ($this->PUBLIC_PATHS as $publicPath) { if ( @@ -49,12 +51,9 @@ public function serves($sitePath, $siteName, $uri) /** * Determine if the incoming request is for a static file. * * @return string|false */ public function isStaticFile(string $sitePath, string $siteName, string $uri) { foreach ($this->PUBLIC_PATHS as $publicPath) { $staticFilePath = $sitePath . '/' . $publicPath . $uri; @@ -69,13 +68,8 @@ public function isStaticFile($sitePath, $siteName, $uri) /** * Get the fully resolved path to the application's front controller. */ public function frontControllerPath(string $sitePath, string $siteName, string $uri): string { $this->sitePath = $sitePath; -
MouseEatsCat revised this gist
May 16, 2023 . 1 changed file with 126 additions and 42 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -2,12 +2,27 @@ use Valet\Drivers\BasicValetDriver; class LocomotiveBedrockValetDriver extends BasicValetDriver { public $PUBLIC_PATHS = ['www', 'web']; public $WP_PATHS = ['wp', 'wordpress']; public $VALET_CONFIG_FILE = '/valet-config.json'; /** @var bool */ public $configFileLoaded = false; /** @var string */ public $sitePath; /** @var array */ public $valetConfig = [ 'remote_assets_domain' => null, 'allow_indexphp_files' => false, 'redirect_trailing_slashes' => false, ]; /** * Determine if the driver serves the request. * * @param string $sitePath @@ -17,18 +32,18 @@ class LocomotiveBedrockValetDriver extends BasicValetDriver { */ public function serves($sitePath, $siteName, $uri) { foreach ($this->PUBLIC_PATHS as $publicPath) { if ( file_exists("$sitePath/$publicPath/mu-plugins/bedrock-autoloader.php") || (is_dir("$sitePath/$publicPath/") && file_exists("$sitePath/$publicPath/wp-config.php") && file_exists("$sitePath/config/application.php")) ) { return true; } } return false; } /** @@ -41,13 +56,13 @@ public function serves($sitePath, $siteName, $uri) */ public function isStaticFile($sitePath, $siteName, $uri) { foreach ($this->PUBLIC_PATHS as $publicPath) { $staticFilePath = $sitePath . '/' . $publicPath . $uri; if ($this->isActualFile($staticFilePath)) { return $staticFilePath; } } return false; } @@ -62,29 +77,37 @@ public function isStaticFile($sitePath, $siteName, $uri) */ public function frontControllerPath($sitePath, $siteName, $uri) { $this->sitePath = $sitePath; $rules = $this->handleConfigRules($sitePath, $siteName, $uri); if (!empty($rules)) { return $rules; } $_SERVER['PHP_SELF'] = $uri; $_SERVER['SERVER_ADDR'] = '127.0.0.1'; $_SERVER['SERVER_NAME'] = $_SERVER['HTTP_HOST']; foreach ($this->PUBLIC_PATHS as $publicPath) { if (is_dir("$sitePath/$publicPath/")) { $path = parent::frontControllerPath( $sitePath . '/' . $publicPath, $siteName, (!$this->getValetConfig('redirect_trailing_slashes') ? $this->forceTrailingSlash($uri) : $uri) ); foreach ($this->WP_PATHS as $wpPath) { if (is_dir("$sitePath/$publicPath/$wpPath")) { if (!str_contains($_SERVER['PHP_SELF'], "$wpPath/wp-admin")) { $_SERVER['PHP_SELF'] = '/index.php'; } } } return $path; } } } /** @@ -93,16 +116,77 @@ public function frontControllerPath($sitePath, $siteName, $uri) * @param string $uri * @return string */ public function forceTrailingSlash($uri) { foreach ($this->WP_PATHS as $wpPath) { $substr = substr($uri, -1 * strlen("/$wpPath/wp-admin")); if ($substr == "/$wpPath/wp-admin") { header('Location: ' . $uri . '/'); exit; } } return $uri; } /** * Handle custom rules set in valet config file. * * @param string $sitePath * @param string $siteName * @param string $uri * @return string */ public function handleConfigRules($sitePath, $siteName, $uri) { // Use remote assets as fallback if ( $this->getValetConfig('remote_assets_domain') && preg_match('/^(.*)\.(jpe?g|gif|pdf|png|svg)$/', $uri) && !$this->isStaticFile($sitePath, $siteName, $uri) ) { $remoteAssetPath = $this->getValetConfig('remote_assets_domain') . $uri; header('Location: ' . $remoteAssetPath); return $remoteAssetPath; } // Do not allow direct use of "index.php" if (!$this->getValetConfig('allow_indexphp_files') && preg_match('/^(.*)index.php$/', $uri)) { $uri = str_replace('index.php', '', $uri); header('Location: ' . $uri); return $uri; } } /** * Determine if valet config file exists. * * @return bool */ public function hasValetConfig() { return file_exists($this->sitePath . $this->VALET_CONFIG_FILE); } /** * Get the valet config options or a single option. * * @param string|null $property Get single valet config option * @return mixed|null Returns null when no matching option is found */ public function getValetConfig(string $property = null) { if (!$this->configFileLoaded) { if ($this->hasValetConfig()) { $json = file_get_contents($this->sitePath . $this->VALET_CONFIG_FILE); $this->valetConfig = array_merge( $this->valetConfig, (json_decode($json, JSON_OBJECT_AS_ARRAY) ?? []) ); $this->configFileLoaded = true; } } return !empty($property) ? ($this->valetConfig[$property] ?? null) : $this->valetConfig; } } -
MouseEatsCat revised this gist
Feb 23, 2023 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -5,7 +5,7 @@ This adds Wordpress [Bedrock](https://github.com/roots/bedrock) project support The driver will match any Bedrock project with a public directory of `web` or `www`. It support having either `wp` or `wordpress` as the wordpress install directory. ## Setup - Run the following command to place the `LocomotiveBedrockValetDriver.php` driver file into `~/.config/valet/Drivers/`. ```shell curl https://gist.github.com/raw/dfc2090b68f2effa02c42a5b8d3f75fc/LocomotiveBedrockValetDriver.php -o ~/.config/valet/Drivers/LocomotiveBedrockValetDriver.php ``` -
MouseEatsCat revised this gist
Feb 23, 2023 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ # Locomotive Bedrock (Wordpress) Valet Driver This adds Wordpress [Bedrock](https://github.com/roots/bedrock) project support for [Laravel Valet](https://github.com/laravel/valet). The driver will match any Bedrock project with a public directory of `web` or `www`. It support having either `wp` or `wordpress` as the wordpress install directory. -
MouseEatsCat revised this gist
Feb 23, 2023 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -7,5 +7,5 @@ The driver will match any Bedrock project with a public directory of `web` or `w ## Setup - Run the following command to place the `CharcoalValetDriver.php` driver file into `~/.config/valet/Drivers/`. ```shell curl https://gist.github.com/raw/dfc2090b68f2effa02c42a5b8d3f75fc/LocomotiveBedrockValetDriver.php -o ~/.config/valet/Drivers/LocomotiveBedrockValetDriver.php ``` -
MouseEatsCat created this gist
Feb 23, 2023 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,108 @@ <?php use Valet\Drivers\BasicValetDriver; class LocomotiveBedrockValetDriver extends BasicValetDriver { const PUBLIC_PATHS = ['www', 'web']; const WP_PATHS = ['wp', 'wordpress']; /** * Determine if the driver serves the request. * * @param string $sitePath * @param string $siteName * @param string $uri * @return bool */ public function serves($sitePath, $siteName, $uri) { foreach (static::PUBLIC_PATHS as $publicPath) { if ( file_exists("$sitePath/$publicPath/mu-plugins/bedrock-autoloader.php") || (is_dir("$sitePath/$publicPath/") && file_exists("$sitePath/$publicPath/wp-config.php") && file_exists("$sitePath/config/application.php")) ) { return true; } } return false; } /** * Determine if the incoming request is for a static file. * * @param string $sitePath * @param string $siteName * @param string $uri * @return string|false */ public function isStaticFile($sitePath, $siteName, $uri) { foreach (static::PUBLIC_PATHS as $publicPath) { $staticFilePath = $sitePath.'/'.$publicPath.$uri; if ($this->isActualFile($staticFilePath)) { return $staticFilePath; } } return false; } /** * Get the fully resolved path to the application's front controller. * * @param string $sitePath * @param string $siteName * @param string $uri * @return string */ public function frontControllerPath($sitePath, $siteName, $uri) { $_SERVER['PHP_SELF'] = $uri; $_SERVER['SERVER_ADDR'] = '127.0.0.1'; $_SERVER['SERVER_NAME'] = $_SERVER['HTTP_HOST']; foreach (static::PUBLIC_PATHS as $publicPath) { if (is_dir("$sitePath/$publicPath/")) { $path = parent::frontControllerPath( $sitePath.'/'.$publicPath, $siteName, $this->forceTrailingSlash($uri) ); foreach (static::WP_PATHS as $wpPath) { if (is_dir("$sitePath/$publicPath/$wpPath")) { if (! str_contains($_SERVER['PHP_SELF'], "$wpPath/wp-admin")) { $_SERVER['PHP_SELF'] = '/index.php'; } } } return $path; } } } /** * Redirect to uri with trailing slash. * * @param string $uri * @return string */ private function forceTrailingSlash($uri) { foreach (static::WP_PATHS as $wpPath) { $substr = substr($uri, -1 * strlen("/$wpPath/wp-admin")); if ($substr == "/$wpPath/wp-admin") { header('Location: '.$uri.'/'); exit; } } return $uri; } } This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,11 @@ # Locomotive Bedrock (Wordpress) Valet Driver This adds [Charcoal](https://github.com/roots/bedrock) project support for [Laravel Valet](https://github.com/laravel/valet). The driver will match any Bedrock project with a public directory of `web` or `www`. It support having either `wp` or `wordpress` as the wordpress install directory. ## Setup - Run the following command to place the `CharcoalValetDriver.php` driver file into `~/.config/valet/Drivers/`. ```shell curl https://gist.github.com/raw/1383e9b3262090c55b445ca096d8cfa4/CharcoalValetDriver.php -o ~/.config/valet/Drivers/CharcoalValetDriver.php ```