Skip to content

Instantly share code, notes, and snippets.

@thijsbouwes
Last active January 24, 2020 08:05
Show Gist options
  • Save thijsbouwes/114c86c9ddcc7535dd1ef265e69da447 to your computer and use it in GitHub Desktop.
Save thijsbouwes/114c86c9ddcc7535dd1ef265e69da447 to your computer and use it in GitHub Desktop.
Valet bedrock driver, serving from the public folder instead of the web folder. Also see: https://github.com/laravel/valet/blob/master/cli/drivers/BedrockValetDriver.php
<?php
class BedrockPublicValetDriver 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)
{
return file_exists($sitePath.'/public/app/mu-plugins/bedrock-autoloader.php') ||
(is_dir($sitePath.'/public/app/') &&
file_exists($sitePath.'/public/wp-config.php') &&
file_exists($sitePath.'/config/application.php'));
}
/**
* 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)
{
$staticFilePath = $sitePath.'/public'.$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_NAME'] = $_SERVER['HTTP_HOST'];
if (strpos($uri, '/wp/') === 0) {
return is_dir($sitePath.'/public'.$uri)
? $sitePath.'/public'.$this->forceTrailingSlash($uri).'/index.php'
: $sitePath.'/public'.$uri;
}
return $sitePath.'/public/index.php';
}
/**
* Redirect to uri with trailing slash.
*
* @param string $uri
* @return string
*/
private function forceTrailingSlash($uri)
{
if (substr($uri, -1 * strlen('/wp/wp-admin')) == '/wp/wp-admin') {
header('Location: '.$uri.'/'); die;
}
return $uri;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment