mirror of
https://github.com/laravel/valet.git
synced 2026-02-06 08:40:09 +01:00
Move server helpers to their own file; add type hints
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
use RecursiveIteratorIterator;
|
||||
use RecursiveRegexIterator;
|
||||
use RegexIterator;
|
||||
use Throwable;
|
||||
|
||||
abstract class ValetDriver
|
||||
{
|
||||
@@ -17,7 +18,7 @@ abstract class ValetDriver
|
||||
* @param string $uri
|
||||
* @return bool
|
||||
*/
|
||||
abstract public function serves($sitePath, $siteName, $uri);
|
||||
abstract public function serves(string $sitePath, string $siteName, string $uri): bool;
|
||||
|
||||
/**
|
||||
* Determine if the incoming request is for a static file.
|
||||
@@ -27,7 +28,7 @@ abstract public function serves($sitePath, $siteName, $uri);
|
||||
* @param string $uri
|
||||
* @return string|false
|
||||
*/
|
||||
abstract public function isStaticFile($sitePath, $siteName, $uri);
|
||||
abstract public function isStaticFile(string $sitePath, string $siteName, string $uri): string|false;
|
||||
|
||||
/**
|
||||
* Get the fully resolved path to the application's front controller.
|
||||
@@ -37,7 +38,7 @@ abstract public function isStaticFile($sitePath, $siteName, $uri);
|
||||
* @param string $uri
|
||||
* @return string
|
||||
*/
|
||||
abstract public function frontControllerPath($sitePath, $siteName, $uri);
|
||||
abstract public function frontControllerPath(string $sitePath, string $siteName, string $uri): string;
|
||||
|
||||
/**
|
||||
* Find a driver that can serve the incoming request.
|
||||
@@ -47,7 +48,7 @@ abstract public function frontControllerPath($sitePath, $siteName, $uri);
|
||||
* @param string $uri
|
||||
* @return ValetDriver|null
|
||||
*/
|
||||
public static function assign($sitePath, $siteName, $uri)
|
||||
public static function assign(string $sitePath, string $siteName, string $uri): ?ValetDriver
|
||||
{
|
||||
$drivers = [];
|
||||
|
||||
@@ -84,8 +85,8 @@ public static function assign($sitePath, $siteName, $uri)
|
||||
try {
|
||||
// Try for old, un-namespaced drivers
|
||||
$driver = new $driver;
|
||||
} catch (\Throwable $e) {
|
||||
$className = "Valet\Drivers\\$driver";
|
||||
} catch (Throwable $e) {
|
||||
$className = "Valet\Drivers\\{$driver}";
|
||||
$driver = new $className;
|
||||
}
|
||||
|
||||
@@ -99,9 +100,9 @@ public static function assign($sitePath, $siteName, $uri)
|
||||
* Get the custom driver class from the site path, if one exists.
|
||||
*
|
||||
* @param string $sitePath
|
||||
* @return string
|
||||
* @return string|null
|
||||
*/
|
||||
public static function customSiteDriver($sitePath)
|
||||
public static function customSiteDriver(string $sitePath): ?string
|
||||
{
|
||||
if (! file_exists($sitePath.'/LocalValetDriver.php')) {
|
||||
return;
|
||||
@@ -118,7 +119,7 @@ public static function customSiteDriver($sitePath)
|
||||
* @param string $path
|
||||
* @return array
|
||||
*/
|
||||
public static function driversIn($path)
|
||||
public static function driversIn(string $path): array
|
||||
{
|
||||
if (! is_dir($path)) {
|
||||
return [];
|
||||
@@ -145,7 +146,7 @@ public static function driversIn($path)
|
||||
* @param string $uri
|
||||
* @return string
|
||||
*/
|
||||
public function mutateUri($uri)
|
||||
public function mutateUri(string $uri): string
|
||||
{
|
||||
return $uri;
|
||||
}
|
||||
@@ -159,7 +160,7 @@ public function mutateUri($uri)
|
||||
* @param string $uri
|
||||
* @return void
|
||||
*/
|
||||
public function serveStaticFile($staticFilePath, $sitePath, $siteName, $uri)
|
||||
public function serveStaticFile(string $staticFilePath, string $sitePath, string $siteName, string $uri): void
|
||||
{
|
||||
/**
|
||||
* Back story...
|
||||
@@ -201,7 +202,7 @@ protected function isActualFile($path)
|
||||
* @param string $siteName
|
||||
* @return void
|
||||
*/
|
||||
public function loadServerEnvironmentVariables($sitePath, $siteName)
|
||||
public function loadServerEnvironmentVariables(string $sitePath, string $siteName): void
|
||||
{
|
||||
$varFilePath = $sitePath.'/.valet-env.php';
|
||||
if (! file_exists($varFilePath)) {
|
||||
|
||||
144
cli/includes/server-helpers.php
Normal file
144
cli/includes/server-helpers.php
Normal file
@@ -0,0 +1,144 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Show the Valet 404 "Not Found" page.
|
||||
*/
|
||||
function show_valet_404()
|
||||
{
|
||||
http_response_code(404);
|
||||
require __DIR__ . '/cli/templates/404.html';
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Show directory listing or 404 if directory doesn't exist.
|
||||
*
|
||||
* @param string $valetSitePath
|
||||
* @param string $uri
|
||||
*/
|
||||
function show_directory_listing(string $valetSitePath, string $uri)
|
||||
{
|
||||
$is_root = ($uri == '/');
|
||||
$directory = ($is_root) ? $valetSitePath : $valetSitePath . $uri;
|
||||
|
||||
if (!file_exists($directory)) {
|
||||
show_valet_404();
|
||||
}
|
||||
|
||||
// Sort directories at the top
|
||||
$paths = glob("$directory/*");
|
||||
usort($paths, function ($a, $b) {
|
||||
return (is_dir($a) == is_dir($b)) ? strnatcasecmp($a, $b) : (is_dir($a) ? -1 : 1);
|
||||
});
|
||||
|
||||
// Output the HTML for the directory listing
|
||||
echo "<h1>Index of $uri</h1>";
|
||||
echo '<hr>';
|
||||
echo implode("<br>\n", array_map(function ($path) use ($uri, $is_root) {
|
||||
$file = basename($path);
|
||||
|
||||
return ($is_root) ? "<a href='/$file'>/$file</a>" : "<a href='$uri/$file'>$uri/$file/</a>";
|
||||
}, $paths));
|
||||
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* You may use wildcard DNS provider nip.io as a tool for testing your site via an IP address.
|
||||
* First, determine the IP address of your local computer (like 192.168.0.10).
|
||||
* Then, visit http://project.your-ip.nip.io - e.g.: http://laravel.192.168.0.10.nip.io.
|
||||
*
|
||||
* @param string $domain
|
||||
* @param array $valetConfig
|
||||
* @return string
|
||||
*/
|
||||
function valet_support_wildcard_dns(string $domain, array $valetConfig): string
|
||||
{
|
||||
$services = [
|
||||
'.*.*.*.*.nip.io',
|
||||
'-*-*-*-*.nip.io',
|
||||
];
|
||||
|
||||
if (isset($valetConfig['tunnel_services'])) {
|
||||
$services = array_merge($services, (array) $valetConfig['tunnel_services']);
|
||||
}
|
||||
|
||||
$patterns = [];
|
||||
foreach ($services as $service) {
|
||||
$pattern = preg_quote($service, '#');
|
||||
$pattern = str_replace('\*', '.*', $pattern);
|
||||
$patterns[] = '(.*)' . $pattern;
|
||||
}
|
||||
|
||||
$pattern = implode('|', $patterns);
|
||||
|
||||
if (preg_match('#(?:' . $pattern . ')\z#u', $domain, $matches)) {
|
||||
$domain = array_pop($matches);
|
||||
}
|
||||
|
||||
if (strpos($domain, ':') !== false) {
|
||||
$domain = explode(':', $domain)[0];
|
||||
}
|
||||
|
||||
return $domain;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $valetConfig Valet configuration array
|
||||
* @return string|null If set, default site path for uncaught urls
|
||||
**/
|
||||
function valet_default_site_path(array $valetConfig): ?string
|
||||
{
|
||||
if (isset($valetConfig['default']) && is_string($valetConfig['default']) && is_dir($valetConfig['default'])) {
|
||||
return $valetConfig['default'];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine the fully qualified path to the site.
|
||||
* Inspects registered path directories, case-sensitive.
|
||||
*
|
||||
* @param array $valetConfig
|
||||
* @param string $siteName
|
||||
* @param string $domain
|
||||
* @return string
|
||||
*/
|
||||
function get_valet_site_path(array $valetConfig, string $siteName, string $domain): string
|
||||
{
|
||||
$valetSitePath = null;
|
||||
|
||||
foreach ($valetConfig['paths'] as $path) {
|
||||
$handle = opendir($path);
|
||||
|
||||
if ($handle === false) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$dirs = [];
|
||||
|
||||
while (false !== ($file = readdir($handle))) {
|
||||
if (is_dir($path . '/' . $file) && !in_array($file, ['.', '..'])) {
|
||||
$dirs[] = $file;
|
||||
}
|
||||
}
|
||||
|
||||
closedir($handle);
|
||||
|
||||
// Note: strtolower used below because Nginx only tells us lowercase names
|
||||
foreach ($dirs as $dir) {
|
||||
if (strtolower($dir) === $siteName) {
|
||||
// early return when exact match for linked subdomain
|
||||
return $path . '/' . $dir;
|
||||
}
|
||||
|
||||
if (strtolower($dir) === $domain) {
|
||||
// no early return here because the foreach may still have some subdomains to process with higher priority
|
||||
$valetSitePath = $path . '/' . $dir;
|
||||
}
|
||||
}
|
||||
|
||||
if ($valetSitePath) {
|
||||
return $valetSitePath;
|
||||
}
|
||||
}
|
||||
}
|
||||
136
server.php
136
server.php
@@ -1,6 +1,7 @@
|
||||
<?php
|
||||
|
||||
require_once './cli/includes/require-drivers.php';
|
||||
require_once './cli/includes/server-helpers.php';
|
||||
|
||||
use Valet\Drivers\ValetDriver;
|
||||
|
||||
@@ -10,95 +11,6 @@
|
||||
define('VALET_HOME_PATH', posix_getpwuid(fileowner(__FILE__))['dir'].'/.config/valet');
|
||||
define('VALET_STATIC_PREFIX', '41c270e4-5535-4daa-b23e-c269744c2f45');
|
||||
|
||||
/**
|
||||
* Show the Valet 404 "Not Found" page.
|
||||
*/
|
||||
function show_valet_404()
|
||||
{
|
||||
http_response_code(404);
|
||||
require __DIR__.'/cli/templates/404.html';
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Show directory listing or 404 if directory doesn't exist.
|
||||
*/
|
||||
function show_directory_listing($valetSitePath, $uri)
|
||||
{
|
||||
$is_root = ($uri == '/');
|
||||
$directory = ($is_root) ? $valetSitePath : $valetSitePath.$uri;
|
||||
|
||||
if (! file_exists($directory)) {
|
||||
show_valet_404();
|
||||
}
|
||||
|
||||
// Sort directories at the top
|
||||
$paths = glob("$directory/*");
|
||||
usort($paths, function ($a, $b) {
|
||||
return (is_dir($a) == is_dir($b)) ? strnatcasecmp($a, $b) : (is_dir($a) ? -1 : 1);
|
||||
});
|
||||
|
||||
// Output the HTML for the directory listing
|
||||
echo "<h1>Index of $uri</h1>";
|
||||
echo '<hr>';
|
||||
echo implode("<br>\n", array_map(function ($path) use ($uri, $is_root) {
|
||||
$file = basename($path);
|
||||
|
||||
return ($is_root) ? "<a href='/$file'>/$file</a>" : "<a href='$uri/$file'>$uri/$file/</a>";
|
||||
}, $paths));
|
||||
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* You may use wildcard DNS provider nip.io as a tool for testing your site via an IP address.
|
||||
* It's simple to use: First determine the IP address of your local computer (like 192.168.0.10).
|
||||
* Then simply use http://project.your-ip.nip.io - ie: http://laravel.192.168.0.10.nip.io.
|
||||
*/
|
||||
function valet_support_wildcard_dns($domain, $config)
|
||||
{
|
||||
$services = [
|
||||
'.*.*.*.*.nip.io',
|
||||
'-*-*-*-*.nip.io',
|
||||
];
|
||||
|
||||
if (isset($config['tunnel_services'])) {
|
||||
$services = array_merge($services, (array) $config['tunnel_services']);
|
||||
}
|
||||
|
||||
$patterns = [];
|
||||
foreach ($services as $service) {
|
||||
$pattern = preg_quote($service, '#');
|
||||
$pattern = str_replace('\*', '.*', $pattern);
|
||||
$patterns[] = '(.*)'.$pattern;
|
||||
}
|
||||
|
||||
$pattern = implode('|', $patterns);
|
||||
|
||||
if (preg_match('#(?:'.$pattern.')\z#u', $domain, $matches)) {
|
||||
$domain = array_pop($matches);
|
||||
}
|
||||
|
||||
if (strpos($domain, ':') !== false) {
|
||||
$domain = explode(':', $domain)[0];
|
||||
}
|
||||
|
||||
return $domain;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $config Valet configuration array
|
||||
* @return string|null If set, default site path for uncaught urls
|
||||
* */
|
||||
function valet_default_site_path($config)
|
||||
{
|
||||
if (isset($config['default']) && is_string($config['default']) && is_dir($config['default'])) {
|
||||
return $config['default'];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the Valet configuration.
|
||||
*/
|
||||
@@ -123,50 +35,6 @@ function valet_default_site_path($config)
|
||||
$siteName = substr($siteName, 4);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine the fully qualified path to the site.
|
||||
* Inspects registered path directories, case-sensitive.
|
||||
*/
|
||||
function get_valet_site_path($valetConfig, $siteName, $domain)
|
||||
{
|
||||
$valetSitePath = null;
|
||||
|
||||
foreach ($valetConfig['paths'] as $path) {
|
||||
$handle = opendir($path);
|
||||
|
||||
if ($handle === false) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$dirs = [];
|
||||
|
||||
while (false !== ($file = readdir($handle))) {
|
||||
if (is_dir($path.'/'.$file) && ! in_array($file, ['.', '..'])) {
|
||||
$dirs[] = $file;
|
||||
}
|
||||
}
|
||||
|
||||
closedir($handle);
|
||||
|
||||
// Note: strtolower used below because Nginx only tells us lowercase names
|
||||
foreach ($dirs as $dir) {
|
||||
if (strtolower($dir) === $siteName) {
|
||||
// early return when exact match for linked subdomain
|
||||
return $path.'/'.$dir;
|
||||
}
|
||||
|
||||
if (strtolower($dir) === $domain) {
|
||||
// no early return here because the foreach may still have some subdomains to process with higher priority
|
||||
$valetSitePath = $path.'/'.$dir;
|
||||
}
|
||||
}
|
||||
|
||||
if ($valetSitePath) {
|
||||
return $valetSitePath;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$domain = array_slice(explode('.', $siteName), -1)[0];
|
||||
$valetSitePath = get_valet_site_path($valetConfig, $siteName, $domain);
|
||||
|
||||
@@ -179,8 +47,6 @@ function get_valet_site_path($valetConfig, $siteName, $domain)
|
||||
/**
|
||||
* Find the appropriate Valet driver for the request.
|
||||
*/
|
||||
$valetDriver = null;
|
||||
|
||||
$valetDriver = ValetDriver::assign($valetSitePath, $siteName, $uri);
|
||||
|
||||
if (! $valetDriver) {
|
||||
|
||||
5
upgrade.md
Normal file
5
upgrade.md
Normal file
@@ -0,0 +1,5 @@
|
||||
# Upgrading to v4
|
||||
|
||||
- Update custom drivers:
|
||||
- Match the new type hints of the base ValetDriver
|
||||
- Extend the new namespaced drivers instead of the old globally-namespaced drivers
|
||||
Reference in New Issue
Block a user