Skip to content

Instantly share code, notes, and snippets.

@BenSampo
Last active March 28, 2023 13:52
Show Gist options
  • Save BenSampo/93a9012de21736ef878820a01352a0ee to your computer and use it in GitHub Desktop.
Save BenSampo/93a9012de21736ef878820a01352a0ee to your computer and use it in GitHub Desktop.
<?php
namespace Valet\Drivers\Custom;
use Valet\Drivers\ValetDriver;
class PerchRunwayValetDriver extends ValetDriver
{
/**
* Determine if the driver serves the request.
*
* @param string $sitePath
* @param string $siteName
* @param string $uri
* @return bool
*/
public function serves($sitePath, $siteName, $uri): bool
{
// If the site path contains a "perch" directory, we'll assume it's a Perch site.
return is_dir($sitePath.'/perch');
}
/**
* Determine if the incoming request is for a static file.
*/
public function isStaticFile(string $sitePath, string $siteName, string $uri)/*: string|false */
{
$staticFilePath = $sitePath.$uri;
if (file_exists($staticFilePath) && ! is_dir($staticFilePath) && is_file($staticFilePath)) {
return $staticFilePath;
}
return false;
}
/**
* Get the fully resolved path to the application's front controller.
*/
public function frontControllerPath(string $sitePath, string $siteName, string $uri): string
{
$_SERVER['SCRIPT_NAME'] = explode("?", $_SERVER['REQUEST_URI'])[0];
$_SERVER["DOCUMENT_ROOT"] = $sitePath;
// If the URI ends with .php or .html, we'll assume it's a file and serve it as-is.
if (substr($uri, -4) === '.php' || substr($uri, -5) === '.html') {
return $sitePath.$uri;
}
if (substr($uri, 0, 6) === '/perch') {
return $sitePath.rtrim($uri, '/').'/index.php';
}
return $sitePath.'/perch/core/runway/start.php';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment