Skip to content

Instantly share code, notes, and snippets.

@MouseEatsCat
Last active October 7, 2025 20:18
Show Gist options
  • Save MouseEatsCat/dfc2090b68f2effa02c42a5b8d3f75fc to your computer and use it in GitHub Desktop.
Save MouseEatsCat/dfc2090b68f2effa02c42a5b8d3f75fc to your computer and use it in GitHub Desktop.

Revisions

  1. MouseEatsCat revised this gist Jul 17, 2023. 1 changed file with 146 additions and 79 deletions.
    225 changes: 146 additions & 79 deletions LocomotiveBedrockValetDriver.php
    Original 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 $PUBLIC_PATHS = ['www', 'web'];
    public $WP_PATHS = ['wp', 'wordpress'];
    public $VALET_CONFIG_FILE = '/valet-config.json';
    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 bool */
    public $configFileLoaded = false;
    /** @var ?array<string, mixed> */
    public $valetSiteConfig = null;

    /** @var string */
    public $sitePath;
    /** @var ?string */
    public $valetSiteName = null;

    /** @var array */
    public $valetConfig = [
    /** @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
    {
    foreach ($this->PUBLIC_PATHS as $publicPath) {
    $hasBedrockAutoloader = $this->composerRequires($sitePath, 'roots/bedrock-autoloader');

    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"))
    $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 ($this->PUBLIC_PATHS as $publicPath) {
    $staticFilePath = $sitePath . '/' . $publicPath . $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
    {
    $this->sitePath = $sitePath;

    $rules = $this->handleConfigRules($sitePath, $siteName, $uri);

    if (!empty($rules)) {
    return $rules;
    // 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;
    }

    $_SERVER['PHP_SELF'] = $uri;
    $_SERVER['SERVER_ADDR'] = '127.0.0.1';
    $_SERVER['SERVER_NAME'] = $_SERVER['HTTP_HOST'];
    $uri = (
    $this->getValetConfig('redirect_trailing_slashes')
    ? $uri
    : $this->forceTrailingSlash($uri)
    );

    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 (static::PUBLIC_PATHS as $publicPath) {
    if (!is_dir("{$sitePath}/{$publicPath}/")) {
    continue;
    }

    $path = parent::frontControllerPath(
    "{$sitePath}/{$publicPath}",
    $siteName,
    $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';
    }
    }
    foreach (static::WP_PATHS as $wpPath) {
    if (!is_dir("$sitePath/$publicPath/$wpPath")) {
    continue;
    }

    return $path;
    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 ($this->WP_PATHS as $wpPath) {
    $substr = substr($uri, -1 * strlen("/$wpPath/wp-admin"));
    if ($substr == "/$wpPath/wp-admin") {
    header('Location: ' . $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)
    }

    /**
    * Handle custom rules set in valet config file.
    *
    * @param string $sitePath
    * @param string $siteName
    * @param string $uri
    * @return string
    * Load server environment variables if available.
    * Processes any '*' entries first, and then adds site-specific entries.
    */
    public function handleConfigRules($sitePath, $siteName, $uri)
    public function loadServerEnvironmentVariables(string $sitePath, string $siteName): void
    {
    // 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;
    }
    parent::loadServerEnvironmentVariables($sitePath, $siteName);

    // 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;
    }
    $this->valetSitePath = $sitePath;
    $this->valetSiteName = $siteName;

    $this->loadValetConfig($sitePath, $siteName);
    }

    /**
    @@ -159,28 +176,78 @@ public function handleConfigRules($sitePath, $siteName, $uri)
    */
    public function hasValetConfig()
    {
    return file_exists($this->sitePath . $this->VALET_CONFIG_FILE);
    return $this->valetSitePath && file_exists($this->valetSitePath . '/' . static::VALET_CONFIG_FILE);
    }

    /**
    * Get the valet config options or a single option.
    * Get the driver configuration 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
    * @param string|null $key The single driver configuration option.
    * @return mixed|null Returns null when no matching option is found.
    */
    public function getValetConfig(string $property = null)
    public function getValetConfig(?string $key = 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) ?? [])
    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
    );
    $this->configFileLoaded = true;
    }
    }

    return !empty($property) ? ($this->valetConfig[$property] ?? null) : $this->valetConfig;
    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;
    }
    }
  2. MouseEatsCat revised this gist May 16, 2023. 1 changed file with 11 additions and 17 deletions.
    28 changes: 11 additions & 17 deletions LocomotiveBedrockValetDriver.php
    Original 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.
    *
    * @param string $sitePath
    * @param string $siteName
    * @param string $uri
    * @return bool
    */
    public function serves($sitePath, $siteName, $uri)
    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.
    *
    * @param string $sitePath
    * @param string $siteName
    * @param string $uri
    * @return string|false
    */
    public function isStaticFile($sitePath, $siteName, $uri)
    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.
    *
    * @param string $sitePath
    * @param string $siteName
    * @param string $uri
    * @return string
    */
    public function frontControllerPath($sitePath, $siteName, $uri)
    public function frontControllerPath(string $sitePath, string $siteName, string $uri): string
    {
    $this->sitePath = $sitePath;

  3. MouseEatsCat revised this gist May 16, 2023. 1 changed file with 126 additions and 42 deletions.
    168 changes: 126 additions & 42 deletions LocomotiveBedrockValetDriver.php
    Original file line number Diff line number Diff line change
    @@ -2,12 +2,27 @@

    use Valet\Drivers\BasicValetDriver;

    class LocomotiveBedrockValetDriver extends BasicValetDriver {
    class LocomotiveBedrockValetDriver extends BasicValetDriver
    {

    const PUBLIC_PATHS = ['www', 'web'];
    const WP_PATHS = ['wp', 'wordpress'];
    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 (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;
    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 (static::PUBLIC_PATHS as $publicPath) {
    $staticFilePath = $sitePath.'/'.$publicPath.$uri;
    foreach ($this->PUBLIC_PATHS as $publicPath) {
    $staticFilePath = $sitePath . '/' . $publicPath . $uri;

    if ($this->isActualFile($staticFilePath)) {
    return $staticFilePath;
    }
    }
    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 (static::PUBLIC_PATHS as $publicPath) {
    if (is_dir("$sitePath/$publicPath/")) {
    $path = parent::frontControllerPath(
    $sitePath.'/'.$publicPath,
    $siteName,
    $this->forceTrailingSlash($uri)
    );
    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 (static::WP_PATHS as $wpPath) {
    foreach ($this->WP_PATHS as $wpPath) {
    if (is_dir("$sitePath/$publicPath/$wpPath")) {
    if (! str_contains($_SERVER['PHP_SELF'], "$wpPath/wp-admin")) {
    if (!str_contains($_SERVER['PHP_SELF'], "$wpPath/wp-admin")) {
    $_SERVER['PHP_SELF'] = '/index.php';
    }
    }
    }

    return $path;
    }
    }
    return $path;
    }
    }
    }

    /**
    @@ -93,16 +116,77 @@ public function frontControllerPath($sitePath, $siteName, $uri)
    * @param string $uri
    * @return string
    */
    private function forceTrailingSlash($uri)
    public 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;
    }
    }
    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;
    }
    }
  4. MouseEatsCat revised this gist Feb 23, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original 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 `CharcoalValetDriver.php` driver file into `~/.config/valet/Drivers/`.
    - 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
    ```
  5. MouseEatsCat revised this gist Feb 23, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # Locomotive Bedrock (Wordpress) Valet Driver

    This adds [Charcoal](https://github.com/roots/bedrock) project support for [Laravel Valet](https://github.com/laravel/valet).
    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.

  6. MouseEatsCat revised this gist Feb 23, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original 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/1383e9b3262090c55b445ca096d8cfa4/CharcoalValetDriver.php -o ~/.config/valet/Drivers/CharcoalValetDriver.php
    curl https://gist.github.com/raw/dfc2090b68f2effa02c42a5b8d3f75fc/LocomotiveBedrockValetDriver.php -o ~/.config/valet/Drivers/LocomotiveBedrockValetDriver.php
    ```
  7. MouseEatsCat created this gist Feb 23, 2023.
    108 changes: 108 additions & 0 deletions LocomotiveBedrockValetDriver.php
    Original 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;
    }
    }
    11 changes: 11 additions & 0 deletions README.md
    Original 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
    ```