Skip to content

Instantly share code, notes, and snippets.

@firhatsungkar
Last active January 17, 2019 02:17
Show Gist options
  • Save firhatsungkar/1ebdc67be63c6284dfc5fde7678dcb7d to your computer and use it in GitHub Desktop.
Save firhatsungkar/1ebdc67be63c6284dfc5fde7678dcb7d to your computer and use it in GitHub Desktop.
<?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 file_exists($sitePath . "/$siteName" . '/library/Thms/Config/Environment.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)
{
if (file_exists($staticFilePath = $sitePath . '/$siteName' . $uri)) {
header('Content-Type:text/plain');
return $staticFilePath;
} elseif ($this->isActualFile($staticFilePath = $sitePath . "/$siteName" . $uri)) {
header('Content-Type:text/plain');
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)
{
if (strpos($uri, '/cms/') !== false) {
$dynamicCandidates = [
$this->asActualFile($sitePath . "/$siteName", $uri),
$this->asPhpIndexFileInDirectory($sitePath . "/$siteName", $uri),
$this->asHtmlIndexFileInDirectory($sitePath . "/$siteName", $uri),
];
foreach ($dynamicCandidates as $id => $candidate) {
if ($this->isActualFile($candidate)) {
$_SERVER['PHP_SELF'] = $uri;
$_SERVER['SCRIPT_FILENAME'] = $candidate;
$_SERVER['SCRIPT_NAME'] = str_replace($sitePath . "/$siteName", '', $candidate);
$_SERVER['DOCUMENT_ROOT'] = $sitePath . "/$siteName";
return $candidate;
}
}
}
return $sitePath . "/$siteName" . '/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;
}
/**
* 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