Forked from LostKobrakai/ProcessWireValetDriver.php
Created
February 10, 2021 11:47
-
-
Save sjardim/b60529ab378763ba69a6ef33e2cfb501 to your computer and use it in GitHub Desktop.
ProcessWire 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 ProcessWireValetDriver extends BasicValetDriver | |
{ | |
private $possibleDirectories = [ | |
'', // PW in root, do not remove except you're sure you never use it | |
'/dist', | |
'/public' | |
]; | |
/** | |
* 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 ($this->possibleDirectories as $dir) { | |
if(is_dir("{$sitePath}{$dir}/wire")) | |
return true; | |
} | |
return false; | |
} | |
public function isStaticFile($sitePath, $siteName, $uri) | |
{ | |
foreach ($this->possibleDirectories as $dir) { | |
if(is_file($staticFilePath = "{$sitePath}{$dir}{$uri}")) | |
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['SERVER_NAME'] = $_SERVER['HTTP_HOST']; | |
$_SERVER['SCRIPT_NAME'] = '/index.php'; | |
$_GET['it'] = $uri; | |
if ($uri === '') { | |
$uri = '/'; | |
} | |
foreach ($this->possibleDirectories as $dir) { | |
if(!file_exists($indexPath = "{$sitePath}{$dir}/index.php")) continue; | |
$_SERVER['REQUEST_URI'] = substr(str_replace($dir, '', $_SERVER['REQUEST_URI']), 10); | |
$_SERVER['SCRIPT_FILENAME'] = $indexPath; | |
if (file_exists($indexPath = "{$sitePath}{$dir}/install.php")) { | |
return $indexPath; | |
} | |
return parent::frontControllerPath( | |
"{$sitePath}{$dir}", $siteName, $uri | |
); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment