diff --git a/cli/Valet/Drivers/ValetDriver.php b/cli/Valet/Drivers/ValetDriver.php
index 2fefa65..b6d0706 100644
--- a/cli/Valet/Drivers/ValetDriver.php
+++ b/cli/Valet/Drivers/ValetDriver.php
@@ -188,7 +188,7 @@ public function serveStaticFile(string $staticFilePath, string $sitePath, string
* @param string $path
* @return bool
*/
- protected function isActualFile($path)
+ protected function isActualFile(string $path): bool
{
return ! is_dir($path) && file_exists($path);
}
diff --git a/cli/Valet/Server.php b/cli/Valet/Server.php
index c6155ec..293e701 100644
--- a/cli/Valet/Server.php
+++ b/cli/Valet/Server.php
@@ -19,13 +19,67 @@ public function __construct(public array $config)
* @param string $requestUri $_SERVER['REQUEST_URI']
* @return string
*/
- public function uriFromRequestUri(string $requestUri): string
+ public static function uriFromRequestUri(string $requestUri): string
{
return rawurldecode(
explode('?', $requestUri)[0]
);
}
+ /**
+ * Extract the domain from the site name.
+ *
+ * @param string $siteName
+ * @return string
+ */
+ public static function domainFromSiteName(string $siteName): string
+ {
+ return array_slice(explode('.', $siteName), -1)[0];
+ }
+
+ /**
+ * Show the Valet 404 "Not Found" page.
+ */
+ public static function show404()
+ {
+ 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
+ */
+ public static function showDirectoryListing(string $valetSitePath, string $uri)
+ {
+ $is_root = ($uri == '/');
+ $directory = ($is_root) ? $valetSitePath : $valetSitePath . $uri;
+
+ if (!file_exists($directory)) {
+ static::show404();
+ }
+
+ // 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 "
Index of $uri
";
+ echo '
';
+ echo implode("
\n", array_map(function ($path) use ($uri, $is_root) {
+ $file = basename($path);
+
+ return ($is_root) ? "/$file" : "$uri/$file/";
+ }, $paths));
+
+ exit;
+ }
+
/**
* Extract site name from HTTP host, stripping www. and supporting wildcard DNS.
*
@@ -86,28 +140,17 @@ public function allowWildcardDnsDomains(string $domain): string
return $domain;
}
- /**
- * Extract the domain from the site name.
- *
- * @param string $siteName
- * @return string
- */
- public function domainFromSiteName(string $siteName): string
- {
- return array_slice(explode('.', $siteName), -1)[0];
- }
-
/**
* Determine the fully qualified path to the site.
* Inspects registered path directories, case-sensitive.
*
* @param string $siteName
- * @return string
+ * @return string|null
*/
- public function sitePath(string $siteName): string
+ public function sitePath(string $siteName): ?string
{
$valetSitePath = null;
- $domain = $this->domainFromSiteName($siteName);
+ $domain = static::domainFromSiteName($siteName);
foreach ($this->config['paths'] as $path) {
$handle = opendir($path);
@@ -143,58 +186,21 @@ public function sitePath(string $siteName): string
return $valetSitePath;
}
}
+
+ return null;
}
/**
- * Show the Valet 404 "Not Found" page.
- */
- public function show404()
- {
- http_response_code(404);
- require __DIR__.'/cli/templates/404.html';
- exit;
- }
-
- /**
- * @return string|null If set, default site path for uncaught urls
+ * Return the default site path for uncaught URLs, if it's set.
+ *
+ * @return string|null
**/
public function defaultSitePath(): ?string
{
if (isset($this->config['default']) && is_string($this->config['default']) && is_dir($this->config['default'])) {
return $this->config['default'];
}
- }
- /**
- * Show directory listing or 404 if directory doesn't exist.
- *
- * @param string $valetSitePath
- * @param string $uri
- */
- public function showDirectoryListing(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 "Index of $uri
";
- echo '
';
- echo implode("
\n", array_map(function ($path) use ($uri, $is_root) {
- $file = basename($path);
-
- return ($is_root) ? "/$file" : "$uri/$file/";
- }, $paths));
-
- exit;
+ return null;
}
}
diff --git a/cli/Valet/Site.php b/cli/Valet/Site.php
index da53739..7a2324c 100644
--- a/cli/Valet/Site.php
+++ b/cli/Valet/Site.php
@@ -66,7 +66,7 @@ private function getLinkNameByCurrentDir(): ?string
public function host(string $path): ?string
{
foreach ($this->files->scandir($this->sitesPath()) as $link) {
- if ($resolved = realpath($this->sitesPath($link)) === $path) {
+ if (realpath($this->sitesPath($link)) === $path) {
return $link;
}
}
diff --git a/server.php b/server.php
index 4e9b323..05ea5c6 100644
--- a/server.php
+++ b/server.php
@@ -24,12 +24,12 @@
/**
* Parse the URI and site / host for the incoming request.
*/
-$uri = $server->uriFromRequestUri($_SERVER['REQUEST_URI']);
+$uri = Server::uriFromRequestUri($_SERVER['REQUEST_URI']);
$siteName = $server->siteNameFromHttpHost($_SERVER['HTTP_HOST']);
$valetSitePath = $server->sitePath($siteName);
if (is_null($valetSitePath) && is_null($valetSitePath = $server->defaultSitePath())) {
- $server->show404();
+ Server::show404();
}
$valetSitePath = realpath($valetSitePath);
@@ -40,7 +40,7 @@
$valetDriver = ValetDriver::assign($valetSitePath, $siteName, $uri);
if (! $valetDriver) {
- $server->show404();
+ Server::show404();
}
/**
@@ -85,10 +85,10 @@
if (! $frontControllerPath) {
if (isset($valetConfig['directory-listing']) && $valetConfig['directory-listing'] == 'on') {
- $server->showDirectoryListing($valetSitePath, $uri);
+ Server::showDirectoryListing($valetSitePath, $uri);
}
- $server->show404();
+ Server::show404();
}
chdir(dirname($frontControllerPath));
diff --git a/tests/ServerTest.php b/tests/ServerTest.php
new file mode 100644
index 0000000..1d792dd
--- /dev/null
+++ b/tests/ServerTest.php
@@ -0,0 +1,92 @@
+setNullWriter();
+ }
+
+ public function tear_down()
+ {
+ Mockery::close();
+ }
+
+ public function test_it_extracts_uri_from_server_request_uri()
+ {
+ $this->assertEquals('/about/index.php', Server::uriFromRequestUri('/about/index.php?abc=def&qrs=tuv'));
+ $this->assertEquals('/', Server::uriFromRequestUri('/?abc=def&qrs=tuv'));
+ }
+
+ public function test_it_extracts_domain_from_site_name()
+ {
+ $this->assertEquals('tighten', Server::domainFromSiteName('subdomain.tighten'));
+ }
+
+ public function test_it_gets_site_name_from_http_host()
+ {
+ $server = new Server(['tld' => 'test']);
+
+ $httpHost = 'tighten.test';
+ $this->assertEquals('tighten', $server->siteNameFromHttpHost($httpHost));
+ }
+
+ public function test_it_gets_site_name_from_http_host_using_wildcard()
+ {
+ $server = new Server(['tld' => 'test']);
+
+ $httpHost = 'tighten.192.168.0.10.nip.io';
+ $this->assertEquals('tighten', $server->siteNameFromHttpHost($httpHost));
+ $httpHost = 'tighten-192-168-0-10.nip.io';
+ $this->assertEquals('tighten', $server->siteNameFromHttpHost($httpHost));
+ }
+
+ public function test_it_strips_www_dot_from_http_host()
+ {
+ $server = new Server(['tld' => 'test']);
+
+ $httpHost = 'www.tighten.test';
+ $this->assertEquals('tighten', $server->siteNameFromHttpHost($httpHost));
+ }
+
+ public function test_it_gets_site_path_from_site_name()
+ {
+ $server = new Server(['paths' => [__DIR__.'/files/sites']]);
+
+ $realPath = __DIR__.'/files/sites/tighten';
+ $this->assertEquals($realPath, $server->sitePath('tighten'));
+ $realPath = __DIR__ . '/files/sites/tighten';
+ $this->assertEquals($realPath, $server->sitePath('subdomain.tighten'));
+ }
+
+ public function test_it_returns_null_if_site_does_not_match()
+ {
+ $server = new Server(['paths' => []]);
+
+ $this->assertNull($server->sitePath('tighten'));
+ }
+
+ public function test_it_gets_default_site_path()
+ {
+ $server = new Server(['default' => __DIR__.'/files/sites/tighten']);
+
+ $this->assertEquals(__DIR__.'/files/sites/tighten', $server->defaultSitePath());
+ }
+
+ public function test_it_returns_null_default_site_path_if_not_set()
+ {
+ $server = new Server([]);
+
+ $this->assertNull($server->defaultSitePath());
+ }
+}
diff --git a/tests/files/sites/tighten/.gitkeep b/tests/files/sites/tighten/.gitkeep
new file mode 100644
index 0000000..e69de29