mirror of
https://github.com/laravel/valet.git
synced 2026-02-04 16:10:08 +01:00
128 lines
2.6 KiB
PHP
128 lines
2.6 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Define the user's "~/.valet" path.
|
|
*/
|
|
|
|
define('VALET_HOME_PATH', posix_getpwuid(fileowner(__FILE__))['dir'].'/.valet');
|
|
|
|
/**
|
|
* Show the Valet 404 "Not Found" page.
|
|
*/
|
|
function show_valet_404()
|
|
{
|
|
http_response_code(404);
|
|
require __DIR__.'/cli/templates/404.html';
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* @param $domain string Domain to filter
|
|
*
|
|
* @return string Filtered domain (without xip.io feature)
|
|
*/
|
|
function valet_support_xip_io($domain)
|
|
{
|
|
if (substr($domain, -7) === '.xip.io') {
|
|
// support only ip v4 for now
|
|
$domainPart = explode('.', $domain);
|
|
if (count($domainPart) > 6) {
|
|
$domain = implode('.', array_reverse(array_slice(array_reverse($domainPart), 6)));
|
|
}
|
|
}
|
|
|
|
return $domain;
|
|
}
|
|
|
|
/**
|
|
* Load the Valet configuration.
|
|
*/
|
|
$valetConfig = json_decode(
|
|
file_get_contents(VALET_HOME_PATH.'/config.json'), true
|
|
);
|
|
|
|
/**
|
|
* Parse the URI and site / host for the incoming request.
|
|
*/
|
|
$uri = urldecode(
|
|
explode("?", $_SERVER['REQUEST_URI'])[0]
|
|
);
|
|
|
|
$siteName = basename(
|
|
// Filter host to support xip.io feature
|
|
valet_support_xip_io($_SERVER['HTTP_HOST']),
|
|
'.'.$valetConfig['domain']
|
|
);
|
|
|
|
if (strpos($siteName, 'www.') === 0) {
|
|
$siteName = substr($siteName, 4);
|
|
}
|
|
|
|
/**
|
|
* Determine the fully qualified path to the site.
|
|
*/
|
|
$valetSitePath = null;
|
|
|
|
foreach ($valetConfig['paths'] as $path) {
|
|
if (is_dir($path.'/'.$siteName)) {
|
|
$valetSitePath = $path.'/'.$siteName;
|
|
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (is_null($valetSitePath)) {
|
|
show_valet_404();
|
|
}
|
|
|
|
$valetSitePath = realpath($valetSitePath);
|
|
|
|
/**
|
|
* Find the appropriate Valet driver for the request.
|
|
*/
|
|
$valetDriver = null;
|
|
|
|
require __DIR__.'/cli/drivers/require.php';
|
|
|
|
$valetDriver = ValetDriver::assign($valetSitePath, $siteName, $uri);
|
|
|
|
if (! $valetDriver) {
|
|
show_valet_404();
|
|
}
|
|
|
|
/**
|
|
* Overwrite the HTTP host for Ngrok.
|
|
*/
|
|
if (isset($_SERVER['HTTP_X_ORIGINAL_HOST'])) {
|
|
$_SERVER['HTTP_HOST'] = $_SERVER['HTTP_X_ORIGINAL_HOST'];
|
|
}
|
|
|
|
/**
|
|
* Allow driver to mutate incoming URL.
|
|
*/
|
|
$uri = $valetDriver->mutateUri($uri);
|
|
|
|
/**
|
|
* Determine if the incoming request is for a static file.
|
|
*/
|
|
$isPhpFile = pathinfo($uri, PATHINFO_EXTENSION) === 'php';
|
|
|
|
if ($uri !== '/' && ! $isPhpFile && $staticFilePath = $valetDriver->isStaticFile($valetSitePath, $siteName, $uri)) {
|
|
return $valetDriver->serveStaticFile($staticFilePath, $valetSitePath, $siteName, $uri);
|
|
}
|
|
|
|
/**
|
|
* Attempt to dispatch to a front controller.
|
|
*/
|
|
$frontControllerPath = $valetDriver->frontControllerPath(
|
|
$valetSitePath, $siteName, $uri
|
|
);
|
|
|
|
if (! $frontControllerPath) {
|
|
show_valet_404();
|
|
}
|
|
|
|
chdir(dirname($frontControllerPath));
|
|
|
|
require $frontControllerPath;
|