Skip to content

Instantly share code, notes, and snippets.

@LostKobrakai
Created October 15, 2016 10:25
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save LostKobrakai/5328d6f64e9dc06a8776d0231c6628c6 to your computer and use it in GitHub Desktop.
Save LostKobrakai/5328d6f64e9dc06a8776d0231c6628c6 to your computer and use it in GitHub Desktop.
ProcessWire Valet Driver
<?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
);
}
}
}
@lesaff
Copy link

lesaff commented Oct 15, 2016

Tested and it works, thanks! 👍

@trk
Copy link

trk commented Mar 14, 2018

$_SERVER['REQUEST_URI'] replacement on line 58 breaking TracyDebugger and $_SERVER['REQUEST_URI'] adrianbj/TracyDebugger#19 (comment)

@LostKobrakai
Copy link
Author

Iirc the str_replace() is mostly for the support of subfolders within the valet project root, while the substr(…, 10) is to remove the trailing /index.php from the REQUEST_URI.

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