Created
December 27, 2019 04:21
-
-
Save awcodes/03b46ab737e23b938393cfd657014966 to your computer and use it in GitHub Desktop.
Themosis Valet Driver
This file contains 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 characters
<?php | |
class ThemosisValetDriver 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 is_dir($sitePath.'/htdocs/cms'); | |
} | |
/** | |
* 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) | |
{ | |
if (strpos($uri, '/cms/') !== false) { | |
$dynamicCandidates = [ | |
$this->asActualFile($sitePath, $uri), | |
$this->asPhpIndexFileInDirectory($sitePath, $uri), | |
$this->asHtmlIndexFileInDirectory($sitePath, $uri), | |
]; | |
foreach ($dynamicCandidates as $id => $candidate) { | |
if ($this->isActualFile($candidate)) { | |
$_SERVER['PHP_SELF'] = $uri; | |
$_SERVER['SCRIPT_FILENAME'] = $candidate; | |
$_SERVER['SCRIPT_NAME'] = str_replace($sitePath, '', $candidate); | |
$_SERVER['DOCUMENT_ROOT'] = $sitePath; | |
return $candidate; | |
} | |
} | |
} | |
return $sitePath . '/htdocs/index.php'; | |
} | |
/** | |
* Redirect to uri with trailing slash. | |
* | |
* @param string $uri | |
* @return string | |
*/ | |
private function forceTrailingSlash($uri) | |
{ | |
if (substr($uri, -1 * strlen('/wp-admin')) == '/wp-admin') { | |
header('Location: '.$uri.'/'); die; | |
} | |
return $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) | |
{ | |
if (file_exists($staticFilePath = $sitePath.'/htdocs'.rtrim($uri, '/').'/index.html')) { | |
return $staticFilePath; | |
} elseif (file_exists($staticFilePath = $sitePath.'/htdocs'.rtrim($uri, '/').'/index.php')) { | |
return $staticFilePath; | |
} elseif (file_exists($staticFilePath = $sitePath.'/htdocs'.$uri)) { | |
return $staticFilePath; | |
} elseif ($this->isActualFile($staticFilePath = $sitePath.$uri)) { | |
return $staticFilePath; | |
} | |
return false; | |
} | |
/** | |
* Mutate the incoming URI. | |
* | |
* @param string $uri | |
* @return string | |
*/ | |
public function mutateUri($uri) | |
{ | |
return rtrim('/htdocs' . $uri, '/'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment