Skip to content

Instantly share code, notes, and snippets.

@KevinBatdorf
Created November 30, 2019 11:46
Show Gist options
  • Save KevinBatdorf/0da76f1dc37401d8378178f7c5de4d1c to your computer and use it in GitHub Desktop.
Save KevinBatdorf/0da76f1dc37401d8378178f7c5de4d1c to your computer and use it in GitHub Desktop.
Themosis version 2 Valet driver
<?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.'/htdocs/wp-config.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.'/htdocs'.$uri)
&& is_file($staticFilePath)) {
return $staticFilePath;
}
$storageUri = $uri;
if (strpos($uri, '/storage/') === 0) {
$storageUri = substr($uri, 8);
}
if ($this->isActualFile($storagePath = $sitePath.'/storage/app/public'.$storageUri)) {
return $storagePath;
}
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'];
$dynamicCandidates = [
$this->asActualFile($sitePath, $uri),
$this->asPhpIndexFileInDirectory($sitePath, $uri),
$this->asHtmlIndexFileInDirectory($sitePath, $uri),
];
foreach ($dynamicCandidates as $candidate) {
if ($this->isActualFile($candidate)) {
$_SERVER['SCRIPT_FILENAME'] = $candidate;
$_SERVER['SCRIPT_NAME'] = str_replace($sitePath, '', $candidate);
$_SERVER['DOCUMENT_ROOT'] = $sitePath;
return $candidate;
}
}
$fixedCandidatesAndDocroots = [
$this->asRootPhpIndexFile($sitePath) => $sitePath,
$this->asPublicPhpIndexFile($sitePath) => $sitePath . '/htdocs',
$this->asPublicHtmlIndexFile($sitePath) => $sitePath . '/htdocs',
];
foreach ($fixedCandidatesAndDocroots as $candidate => $docroot) {
if ($this->isActualFile($candidate)) {
$_SERVER['SCRIPT_FILENAME'] = $candidate;
$_SERVER['SCRIPT_NAME'] = '/index.php';
$_SERVER['DOCUMENT_ROOT'] = $docroot;
return $candidate;
}
}
}
/**
* 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;
}
/**
* Format the incoming site path as a "public/index.php" file path.
*
* @param string $sitePath
* @return string
*/
protected function asPublicPhpIndexFile($sitePath)
{
return $sitePath.'/htdocs/index.php';
}
/**
* Format the incoming site path as a "htdocs/index.php" file path.
*
* @param string $sitePath
* @return string
*/
protected function asPublicHtmlIndexFile($sitePath)
{
return $sitePath.'/htdocs/index.html';
}
}
@wearebattalion
Copy link

Hi Kevin, I'm just trying to implement a Themosis setup on Valet and have attempted to use your driver code, however it's redirecting me to xxx.test/cms/wp-admin/install.php however I'm receiving the error "xxx.test redirected you too many times.' - any ideas how to resolve this?

First check with valet which that it's using the correct driver.

If all good, I'd just start from index.php in your themosis project and dd() along the way until you figure out the issue. Maybe something in your .env file isn't correct?

Thanks - the project appears to be using the correct driver and I can't see any issues in my .env file (below). I may need to dd() to find the issue.

APP_ENV=local
APP_DEBUG=true
APP_KEY=base64:8VMFNWhXvdxaV+/6imQuzG53mFhEuXgerGQHqqfmlJ0=
APP_TD=themosis

APP_URL=https://test-project.test
WP_URL=https://test-project.test/cms

DATABASE_NAME=test-project
DATABASE_USER=root
DATABASE_PASSWORD=
DATABASE_HOST=127.0.0.1

@KevinBatdorf
Copy link
Author

Maybe use http instead of https?

Also, can you use a hyphen in the database name? Not sure tbh, but you might need to escape that too.

@wearebattalion
Copy link

Hmm I've tried with http and have underscored the db name but still receiving the same error. Even tried with a new fresh install of themosis and a fresh db install and still receiving the error. Very odd!

@KevinBatdorf
Copy link
Author

I haven't used Themosis 2 in awhile otherwise I'd be more set up to help out. I would try following the code and dd() until you get some clues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment