Created
March 16, 2023 11:12
-
-
Save trk/16926ca076ebe536cde482e7973d8648 to your computer and use it in GitHub Desktop.
Valet 4 Driver for ProcessWire v3
This file contains hidden or 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 | |
namespace Valet\Drivers\Custom; | |
use Valet\Drivers\ValetDriver; | |
class ProcessWireValetDriver extends ValetDriver | |
{ | |
private array $possiblePaths = ['', '/public_html', '/web']; | |
protected string $path = ''; | |
/** | |
* Determine if the driver serves the request. | |
*/ | |
public function serves(string $sitePath, string $siteName, string $uri): bool | |
{ | |
foreach ($this->possiblePaths as $possiblePath) { | |
$filename = $sitePath . $possiblePath . '/wire/core/ProcessWire.php'; | |
if (file_exists($filename)) { | |
$this->path = $sitePath . $possiblePath; | |
return true; | |
} | |
} | |
return false; | |
} | |
/** | |
* Take any steps necessary before loading the front controller for this driver. | |
*/ | |
public function beforeLoading(string $sitePath, string $siteName, string $uri): void | |
{ | |
$_GET['it'] = $uri; | |
$_SERVER['PHP_SELF'] = $uri; | |
$_SERVER['SERVER_ADDR'] = $_SERVER['SERVER_ADDR'] ?? '127.0.0.1'; | |
$_SERVER['SERVER_NAME'] = $_SERVER['HTTP_HOST']; | |
$_SERVER['SCRIPT_NAME'] = '/index.php'; | |
$_SERVER['SCRIPT_FILENAME'] = $this->path . '/index.php'; | |
$_SERVER['DOCUMENT_URI'] = $_SERVER['SCRIPT_FILENAME']; | |
} | |
/** | |
* Determine if the incoming request is for a static file. | |
*/ | |
public function isStaticFile(string $sitePath, string $siteName, string $uri)/*: string|false */ | |
{ | |
if (is_file($this->path . $uri)) { | |
return $this->path . $uri; | |
} | |
return false; | |
} | |
/** | |
* Get the fully resolved path to the application's front controller. | |
*/ | |
public function frontControllerPath(string $sitePath, string $siteName, string $uri): ?string | |
{ | |
if ($uri === '') { | |
$uri = '/'; | |
} | |
$filename = null; | |
if (is_file($this->path . '/install.php')) { | |
$filename = $this->path . '/install.php'; | |
} else if (is_file($this->path . '/index.php')) { | |
$filename = $this->path . '/index.php'; | |
} | |
return $filename; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment