|
<?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; |
|
} |
|
} |