Created
October 15, 2016 10:25
-
-
Save LostKobrakai/5328d6f64e9dc06a8776d0231c6628c6 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
Iirc the
str_replace()
is mostly for the support of subfolders within the valet project root, while thesubstr(…, 10)
is to remove the trailing/index.php
from theREQUEST_URI
.